自定义图表
创建自定义图表
到目前为止,在讨论图表时,我们已经看到了具有其标准格式设置的标准图表。我们只需定义数据源,设置几个属性,图表就会以其默认格式创建好。不过,Aspose.Cells for Python via .NET 的API也支持创建具有自己格式设置的自定义图表,允许开发者按照自己的喜好定制图表外观。
开发者可以在运行时使用Aspose.Cells for Python via .NET创建自定义图表。
图表由数据系列组成。在Aspose.Cells for Python via .NET中,每个数据系列由一个Series对象表示,而SeriesCollection对象作为Series对象的集合。当创建自定义图表时,开发者可以自由为不同的数据系列(集合在SeriesCollection对象中)使用不同类型的图表。
下面的示例代码演示了如何创建自定义图表。在此示例中,我们将为第一个数据系列使用柱形图,为第二个系列使用折线图。结果是,我们在工作表中添加了一个柱形图,结合了一条折线图。
from aspose.cells import Workbook | |
from aspose.cells.charts import ChartType | |
# 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(".") | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Adding a new worksheet to the Workbook 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("A4").put_value(110) | |
worksheet.cells.get("B1").put_value(260) | |
worksheet.cells.get("B2").put_value(12) | |
worksheet.cells.get("B3").put_value(50) | |
worksheet.cells.get("B4").put_value(100) | |
# 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 NSeries (chart data source) to the chart ranging from "A1" cell to "B4" | |
chart.n_series.add("A1:B4", True) | |
# Setting the chart type of 2nd NSeries to display as line chart | |
chart.n_series[1].type = ChartType.LINE | |
# Saving the Excel file | |
workbook.save(dataDir + "output.xls") |
目前,Aspose.Cells for Python via .NET只支持结合饼图、折线图、柱状图和簇状柱状图的自定义图表,但未来版本将支持更多类型的图表。