Creating and Customizing Charts
Possible Usage Scenarios
A chart is a visual display of information. Aspose.Cells allows developers to visualize information in charts just as Microsoft Excel does. Presenting information in charts is always helpful to decision-makers for making quick and timely decisions. It’s easier to quickly see comparisons, patterns, and trends in data with charts rather than raw numbers. Creating charts at runtime, based on the data in a spreadsheet, is one of Aspose.Cells' useful feature. Aspose.Cells supports creating both Standard and Customized charts. Below, we will show a few examples with sample files on how to create some common MS-Excel chart types using Aspose.Cells API.
Pyramid Chart
When the example code is executed, a pyramid chart is added to the worksheet. Please see the output Excel file of the following sample code.
// 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(); |
Line Chart
In the above example, simply changing the ChartType to ChartType::Line
creates a line chart. The complete source is provided below. when the code is executed, a line chart is added to the worksheet. Please see the output Excel file of the following sample code.
// 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(); |
Bubble Chart
In order to create a bubble chart, the ChartType has to be set to ChartType_Bubble
and few extra properties such as SetBubbleSizes & SetXValues need to be set accordingly. Upon executing the following code, a bubble chart is added to the worksheet. Please see the output Excel file of the following sample code.
// 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(); |
Creating Custom Charts
So far, when we’ve discussed charts, we’ve looked at standard charts that have their own standard formatting settings. We only define the data source, set a few properties and the chart is created with its default format settings. But Aspose.Cells APIs also supports creating custom charts that allow developers to create charts with their own format settings. Developers can create custom charts at run-time using Aspose.Cells.
A chart is composed of a data series. When creating a custom chart, developers have the freedom to use different types of charts for different data series.
The example code below demonstrates how to create custom charts. In this example, we are going to use a column chart for the first data series and a line chart for the second series. The result is that we add a column chart, combined with a line chart, to the worksheet. Please see the output Excel file of the following sample code.
// 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(); |