チャートのデータソースを設定

以前のトピックで、チャートのデータソースを設定する方法の多くの例を提供しましたが、このトピックではチャートに設定できるデータの種類について詳細を提供します。

チャートデータの設定

Aspose.Cells for Python via .NET を使用してチャートのデータラベルを管理する際に扱う2つのタイプのデータがあります:

  • チャートデータ。
  • カテゴリデータ。

チャートデータ

チャートデータは、チャートを作成するためのデータソースとして使用するデータです。 SeriesCollectionオブジェクトのaddメソッドを呼び出すことで、セルの範囲(チャートデータを含む)を追加できます。

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(170)
worksheet.cells.get("A4").put_value(300)
worksheet.cells.get("B1").put_value(160)
worksheet.cells.get("B2").put_value(32)
worksheet.cells.get("B3").put_value(50)
worksheet.cells.get("B4").put_value(40)
# Adding sample values to cells as category data
worksheet.cells.get("C1").put_value("Q1")
worksheet.cells.get("C2").put_value("Q2")
worksheet.cells.get("C3").put_value("Y1")
worksheet.cells.get("C4").put_value("Y2")
# Adding a chart to the worksheet
chartIndex = worksheet.charts.add(ChartType.COLUMN, 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 "B4"
chart.n_series.add("A1:B4", True)
# Saving the Excel file
workbook.save(dataDir + "output.xls")

カテゴリデータ

カテゴリデータは、チャートデータのラベリングに使用され、SeriesCollectioncategory_dataプロパティを使用して追加できます。以下に使用例を示します。上記のコードを実行すると、ワークシートに列グラフが追加されます。

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(10)
worksheet.cells.get("A2").put_value(100)
worksheet.cells.get("A3").put_value(170)
worksheet.cells.get("A4").put_value(200)
worksheet.cells.get("B1").put_value(120)
worksheet.cells.get("B2").put_value(320)
worksheet.cells.get("B3").put_value(50)
worksheet.cells.get("B4").put_value(40)
# Adding sample values to cells as category data
worksheet.cells.get("C1").put_value("Q1")
worksheet.cells.get("C2").put_value("Q2")
worksheet.cells.get("C3").put_value("Y1")
worksheet.cells.get("C4").put_value("Y2")
# Adding a chart to the worksheet
chartIndex = worksheet.charts.add(ChartType.COLUMN, 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 "B4"
chart.n_series.add("A1:B4", True)
# Setting the data source for the category data of SeriesCollection
chart.n_series.category_data = "C1:C4"
# Saving the Excel file
workbook.save(dataDir + "output.xls")

高度なトピック