位置、サイズ、およびデザインチャートを操作

チャートの位置とサイズ

時には、新しいまたは既存のチャートの位置やサイズを変更したいことがあります。Aspose.Cells for Python via .NETはChart.chart_objectプロパティを提供し、これを使って実現できます。このプロパティのサブプロパティを使って、チャートのサイズを新しい高さと幅に調整したり、XおよびYの座標を変更したりできます。

チャートの位置とサイズの制御

チャートの位置(X、Y座標)またはサイズ(高さ、幅)を変更するには、これらのプロパティを使用します。

  1. Chart.chart_object.x
  2. Chart.chart_object.y
  3. Chart.chart_object.height
  4. Chart.chart_object.width

以下の例では、上記のAPIの使用方法を説明します。既存のブックをロードし、その最初のワークシートにチャートを含めます。その後、Aspose.Cells for Python via .NETを使用してチャートのサイズと位置を再調整します。

from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
workbook = Workbook(dataDir + "chart.xls")
worksheet = workbook.worksheets[1]
# Load the chart from source worksheet
chart = worksheet.charts[0]
# Resize the chart
chart.chart_object.width = 400
chart.chart_object.height = 300
# Reposition the chart
chart.chart_object.x = 250
chart.chart_object.y = 150
# Output the file
workbook.save(dataDir + "chart.out.xls")

デザイナーチャートの操作

デザイナーテンプレートファイル内のチャートを操作または変更する必要がある場合があります。Aspose.Cells for Python via .NETは、デザイナーチャートの内容と要素の操作を完全にサポートしています。データ、チャート内容、背景画像、およびフォーマットを正確に保持できます。

テンプレートファイル内のデザイナーチャートを操作する

テンプレートファイル内のデザイナーチャートを操作するには、チャート関連のAPIを使用してください。たとえば、Worksheet.Chartsプロパティを使用して、テンプレートファイル内の既存のチャートコレクションを取得できます。

チャートの作成

次の例は、ピラミッドチャートの作成方法を示しています。このチャートを後で操作します。

from aspose.cells import Workbook
from aspose.cells.charts import ChartType
from os import os, path
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiating a Workbook object
workbook = Workbook()
# Adding a new worksheet to the Excel object
sheetIndex = workbook.worksheets.add()
# Obtaining the reference of the newly added worksheet by passing its sheet index
worksheet = workbook.worksheets[sheetIndex]
# Adding sample values to cells
worksheet.cells.get("A1").put_value(50)
worksheet.cells.get("A2").put_value(100)
worksheet.cells.get("A3").put_value(150)
worksheet.cells.get("B1").put_value(4)
worksheet.cells.get("B2").put_value(20)
worksheet.cells.get("B3").put_value(50)
# Adding a chart to the worksheet
chartIndex = worksheet.charts.add(ChartType.PYRAMID, 5, 0, 15, 5)
# Accessing the instance of the newly added chart
chart = worksheet.charts[chartIndex]
# Adding SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B3"
chart.n_series.add("A1:B3", True)
# Saving the Excel file
workbook.save(dataDir + "book1.out.xls")

チャートの操作

次の例は、既存のチャートの操作方法を示しています。この例では、上記で作成したチャートを変更します。生成された出力では、1つのデータポイントの日付ラベルが ‘United Kingdom, 30K’ に設定されていることに注意してください。

from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Open the existing file.
workbook = Workbook(dataDir + "piechart.xls")
# Get the designer chart in the second sheet.
sheet = workbook.worksheets[1]
chart = sheet.charts[0]
# Get the data labels in the data series of the third data point.
datalabels = chart.n_series[0].points[2].data_labels
# Change the text of the label.
datalabels.text = "Unided Kingdom, 400K "
# Save the excel file.
workbook.save(dataDir + "output.xls")

デザイナーテンプレート内の折れ線グラフの操作

この例では、折れ線グラフを操作します。既存のチャートにいくつかのデータシリーズを追加し、それらの線の色を変更します。

from aspose.cells import Workbook
from aspose.pydrawing import Color
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Open the existing file.
workbook = Workbook(dataDir + "Book1.xlsx")
# Get the designer chart in the first worksheet.
chart = workbook.worksheets[0].charts[0]
# Add the third data series to it.
chart.n_series.add("{60, 80, 10}", True)
# Add another data series (fourth) to it.
chart.n_series.add("{0.3, 0.7, 1.2}", True)
# Plot the fourth data series on the second axis.
chart.n_series[3].plot_on_second_axis = True
# Change the Border color of the second data series.
chart.n_series[1].border.color = Color.green
# Change the Border color of the third data series.
chart.n_series[2].border.color = Color.red
# Make the second value axis visible.
chart.second_value_axis.is_visible = True
# Save the excel file.
workbook.save(dataDir + "output.xls")