Set Data source for the chart
In our previous topics, we have already provided many examples to demonstrate that how can you set a data source for your chart but in this topic, we are going to provide more details about the types of data that can be set for a chart.
Setting Chart Data
There are two types of data to deal with while working on charts using Aspose.Cells for Python via .NET as follows:
- Chart data.
- Category data.
Chart Data
Chart data is the data that we use as a data source to build our charts. We can add a range of the cells (containing chart data) by calling the SeriesCollection object’s add method.
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") |
Category Data
Category data is used for the labeling of chart data and can be added to SeriesCollection by using its category_data property. A complete example is given below to demonstrate the use of chart and category data. After executing the above example code, a column chart will be added to the worksheet as shown below.
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") |