自定义图表
Contents
[
Hide
]
创建自定义图表
到目前为止,当我们讨论图表时,我们已经查看了具有标准格式设置的标准图表。我们只定义数据源,设置一些属性,并使用默认格式设置创建图表。但 Aspose.Cells API 还支持创建自定义图表,允许开发人员使用自己的格式设置创建图表。
开发人员可以使用 Aspose.Cells 在运行时创建自定义图表。
图表由数据系列组成。 Aspose.Cells 中的每个数据系列都由一个系列对象而系列合集对象作为集合系列对象。在创建自定义图表时,开发人员可以自由地为不同的数据系列(收集在系列合集目的)。
下面的示例代码演示了如何创建自定义图表。在此示例中,我们将为第一个数据系列使用柱形图,为第二个系列使用折线图。结果是我们向工作表添加了一个柱形图和一个折线图。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Adding a new worksheet to the Workbook object | |
int sheetIndex = workbook.Worksheets.Add(); | |
// Obtaining the reference of the newly added worksheet by passing its sheet index | |
Worksheet worksheet = workbook.Worksheets[sheetIndex]; | |
// Adding sample values to cells | |
worksheet.Cells["A1"].PutValue(50); | |
worksheet.Cells["A2"].PutValue(100); | |
worksheet.Cells["A3"].PutValue(150); | |
worksheet.Cells["A4"].PutValue(110); | |
worksheet.Cells["B1"].PutValue(260); | |
worksheet.Cells["B2"].PutValue(12); | |
worksheet.Cells["B3"].PutValue(50); | |
worksheet.Cells["B4"].PutValue(100); | |
// Adding a chart to the worksheet | |
int chartIndex = worksheet.Charts.Add(Aspose.Cells.Charts.ChartType.Column, 5, 0, 15, 5); | |
// Accessing the instance of the newly added chart | |
Aspose.Cells.Charts.Chart chart = worksheet.Charts[chartIndex]; | |
// Adding NSeries (chart data source) to the chart ranging from "A1" cell to "B4" | |
chart.NSeries.Add("A1:B4", true); | |
// Setting the chart type of 2nd NSeries to display as line chart | |
chart.NSeries[1].Type = Aspose.Cells.Charts.ChartType.Line; | |
// Saving the Excel file | |
workbook.Save(dataDir + "output.xls"); |
目前 Aspose.Cells 仅支持组合饼图、折线图、柱形图和柱形堆栈图的自定义图表,但未来版本将支持更多图表。