创建和定制图表
可能的使用场景
图表是信息的可视化显示。 Aspose.Cells允许开发人员像Microsoft Excel一样在图表中可视化信息。通过图表呈现信息总是有助于决策者能够快速和及时地做出决策。通过图表而不是原始数字快速查看数据的比较、模式和趋势更容易。在运行时基于电子表格中的数据创建图表是Aspose.Cells的一个有用功能。 Aspose.Cells支持创建标准和定制图表。接下来,我们将展示一些使用Aspose.Cells API创建一些常见的MS-Excel图表类型的示例文件。
金字塔图
执行示例代码后,金字塔图将添加到工作表中。请查看以下示例代码的输出Excel文件(66519068.xlsx)。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
Aspose::Cells::Startup(); | |
// Output directory path | |
U16String outDir(u"..\\Data\\02_OutputDirectory\\"); | |
// Path of output excel file | |
U16String outputChartTypePyramid = outDir + u"outputChartTypePyramid.xlsx"; | |
// Create a new workbook | |
Workbook workbook; | |
// Get first worksheet which is created by default | |
Worksheet worksheet = workbook.GetWorksheets().Get(0); | |
// Adding sample values to cells | |
worksheet.GetCells().Get(u"A1").PutValue(50); | |
worksheet.GetCells().Get(u"A2").PutValue(100); | |
worksheet.GetCells().Get(u"A3").PutValue(150); | |
worksheet.GetCells().Get(u"B1").PutValue(4); | |
worksheet.GetCells().Get(u"B2").PutValue(20); | |
worksheet.GetCells().Get(u"B3").PutValue(50); | |
// Adding a chart to the worksheet | |
int chartIndex = worksheet.GetCharts().Add(Aspose::Cells::Charts::ChartType::Pyramid, 5, 0, 20, 8); | |
// Accessing the instance of the newly added chart | |
Chart chart = worksheet.GetCharts().Get(chartIndex); | |
// Adding SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B3" | |
chart.GetNSeries().Add(u"A1:B3", true); | |
// Saving the Excel file | |
workbook.Save(outputChartTypePyramid); | |
Aspose::Cells::Cleanup(); |
折线图
在上面的示例中,将ChartType更改为** ChartType::Line
**即可创建一幅线图。完整源代码如下。执行代码后,一幅折线图将添加到工作表中。请查看以下示例代码的输出Excel文件(66519069.xlsx)。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
Aspose::Cells::Startup(); | |
// Output directory path | |
U16String outDir(u"..\\Data\\02_OutputDirectory\\"); | |
// Path of output excel file | |
U16String outputChartTypeLine = testPath + u"outputChartTypeLine.xlsx"; | |
// Create a new workbook | |
Workbook workbook; | |
// Get first worksheet which is created by default | |
Worksheet worksheet = workbook.GetWorksheets().Get(0); | |
// Adding sample values to cells | |
worksheet.GetCells().Get(u"A1").PutValue(50); | |
worksheet.GetCells().Get(u"A2").PutValue(100); | |
worksheet.GetCells().Get(u"A3").PutValue(150); | |
worksheet.GetCells().Get(u"B1").PutValue(4); | |
worksheet.GetCells().Get(u"B2").PutValue(20); | |
worksheet.GetCells().Get(u"B3").PutValue(50); | |
// Adding a chart to the worksheet | |
int chartIndex = worksheet.GetCharts().Add(Aspose::Cells::Charts::ChartType::Line, 5, 0, 20, 8); | |
// Accessing the instance of the newly added chart | |
Chart chart = worksheet.GetCharts().Get(chartIndex); | |
// Adding SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B3" | |
chart.GetNSeries().Add(u"A1:B3", true); | |
// Saving the Excel file | |
workbook.Save(outputChartTypeLine); | |
Aspose::Cells::Cleanup(); |
气泡图
为创建气泡图,ChartType必须设置为** ChartType_Bubble
**,同时需要相应设置一些额外属性,如SetBubbleSizes和SetXValues。执行以下代码后,气泡图将添加到工作表中。请查看以下示例代码的输出Excel文件(66519070.xlsx)。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
Aspose::Cells::Startup(); | |
// Output directory path | |
U16String outDir(u"..\\Data\\02_OutputDirectory\\"); | |
// Path of output excel file | |
U16String outputChartTypeBubble = outDir + u"outputChartTypeBubble.xlsx"; | |
// Create a new workbook | |
Workbook workbook; | |
// Get first worksheet which is created by default | |
Worksheet worksheet = workbook.GetWorksheets().Get(0); | |
// Fill in data for chart's series | |
// Y Values | |
worksheet.GetCells().Get(0, 0).PutValue(u"Y Values"); | |
worksheet.GetCells().Get(0, 1).PutValue(2); | |
worksheet.GetCells().Get(0, 2).PutValue(4); | |
worksheet.GetCells().Get(0, 3).PutValue(6); | |
// Bubble Size | |
worksheet.GetCells().Get(1, 0).PutValue(u"Bubble Size"); | |
worksheet.GetCells().Get(1, 1).PutValue(2); | |
worksheet.GetCells().Get(1, 2).PutValue(3); | |
worksheet.GetCells().Get(1, 3).PutValue(1); | |
// X Values | |
worksheet.GetCells().Get(2, 0).PutValue(u"X Values"); | |
worksheet.GetCells().Get(2, 1).PutValue(1); | |
worksheet.GetCells().Get(2, 2).PutValue(2); | |
worksheet.GetCells().Get(2, 3).PutValue(3); | |
// Set first column width | |
worksheet.GetCells().SetColumnWidth(0, 12); | |
// Adding a chart to the worksheet | |
int chartIndex = worksheet.GetCharts().Add(Aspose::Cells::Charts::ChartType::Bubble, 5, 0, 20, 8); | |
// Accessing the instance of the newly added chart | |
Chart chart = worksheet.GetCharts().Get(chartIndex); | |
// Adding SeriesCollection (chart data source) to the chart ranging from B1 to D1 | |
chart.GetNSeries().Add(u"B1:D1", true); | |
// Set bubble sizes | |
chart.GetNSeries().Get(0).SetBubbleSizes(u"B2:D2"); | |
// Set X axis values | |
chart.GetNSeries().Get(0).SetXValues(u"B3:D3"); | |
// Set Y axis values | |
chart.GetNSeries().Get(0).SetValues(u"B1:D1"); | |
// Saving the Excel file | |
workbook.Save(outputChartTypeBubble); | |
Aspose::Cells::Cleanup(); |
创建自定义图表
到目前为止,当我们讨论图表时,我们看过具有自己标准格式设置的标准图表。我们只定义数据源,设置一些属性,图表将以其默认格式设置创建。但Aspose.Cells API还支持创建自定义图表,允许开发人员使用自己的格式设置创建图表。开发人员可以使用Aspose.Cells在运行时创建自定义图表。
图表由数据系列组成。当创建自定义图表时,开发人员可以自由地为不同的数据系列使用不同类型的图表。
下面的示例代码演示了如何创建自定义图表。在此示例中,我们将使用柱状图来展示第一个数据系列,并使用折线图来展示第二个系列。结果是我们在工作表中添加了一个柱状图,结合了一个折线图。请查看以下示例代码的 输出Excel文件。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
Aspose::Cells::Startup(); | |
// Output directory path | |
U16String outDir(u"..\\Data\\02_OutputDirectory\\"); | |
// Path of output excel file | |
U16String outputChartTypeCustom = outDir + u"outputChartTypeCustom.xlsx"; | |
// Create a new workbook | |
Workbook workbook; | |
// Get first worksheet which is created by default | |
Worksheet worksheet = workbook.GetWorksheets().Get(0); | |
// Adding sample values to cells | |
worksheet.GetCells().Get(u"A1").PutValue(50); | |
worksheet.GetCells().Get(u"A2").PutValue(100); | |
worksheet.GetCells().Get(u"A3").PutValue(150); | |
worksheet.GetCells().Get(u"A4").PutValue(110); | |
worksheet.GetCells().Get(u"B1").PutValue(260); | |
worksheet.GetCells().Get(u"B2").PutValue(12); | |
worksheet.GetCells().Get(u"B3").PutValue(50); | |
worksheet.GetCells().Get(u"B4").PutValue(100); | |
// Adding a chart to the worksheet | |
int chartIndex = worksheet.GetCharts().Add(Aspose::Cells::Charts::ChartType::Column, 5, 0, 20, 8); | |
// Accessing the instance of the newly added chart | |
Chart chart = worksheet.GetCharts().Get(chartIndex); | |
// Adding SeriesCollection (chart data source) to the chart ranging from A1 to B4 | |
chart.GetNSeries().Add(u"A1:B4", true); | |
// Setting the chart type of 2nd NSeries to display as line chart | |
chart.GetNSeries().Get(1).SetType(Aspose::Cells::Charts::ChartType::Line); | |
// Saving the Excel file | |
workbook.Save(outputChartTypeCustom); | |
Aspose::Cells::Cleanup(); |