Chart Rendering

Creating Charts

Aspose.Cells APIs support to create a verity of Excel charts as detailed under the topic Creating & Customizing Excel Charts. In order to demonstrate the usage of Aspose.Cells APIs to render the charts in image & PDF format, we will create a chart of type Column as per following snippet.

     // Create a new workbook

	intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook();

	// Get first worksheet which is created by default

	intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0);

	// Adding sample values to cells

	worksheet->GetICells()->GetObjectByIndex(new String("A1"))->PutValue(50);

	worksheet->GetICells()->GetObjectByIndex(new String("A2"))->PutValue(100);

	worksheet->GetICells()->GetObjectByIndex(new String("A3"))->PutValue(150);

	worksheet->GetICells()->GetObjectByIndex(new String("B1"))->PutValue(4);

	worksheet->GetICells()->GetObjectByIndex(new String("B2"))->PutValue(20);

	worksheet->GetICells()->GetObjectByIndex(new String("B3"))->PutValue(50);

	// Adding a chart to the worksheet

	int chartIndex = worksheet->GetICharts()->Add(Aspose::Cells::Charts::ChartType::ChartType_Column, 5, 0, 20, 8);

	// Accessing the instance of the newly added chart

	intrusive_ptr<Aspose::Cells::Charts::IChart> chart = worksheet->GetICharts()->GetObjectByIndex(chartIndex);

	// Adding SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B3"

	chart->GetNISeries()->Add(new String("A1:B3"), true);

Rendering Charts

Aspose.Cells APIs support to convert the Excel Charts to images and PDF formats without requiring any additional tools or applications. In order to provide the rendering support, the Chart class has exposed ToImage & ToPdf methods with a verity of overloads to best suit the application requirements.

Rendering Charts to Images

The Chart.toImage method has a verity of overloads to support simple as well as advanced rendering. If the application requirement is to render the chart in its default dimensions, we suggest you to use the Chart.toImage method as follow.

 // Output directory path

StringPtr outDir = new String("..\\Data\\02_OutputDirectory\\");

// Path of output image file

StringPtr outputChartImage = outDir->StringAppend(new String("out1image.png"));

// Saving the chart to image file

chart->ToImage(outputChartImage, Aspose::Cells::System::Drawing::Imaging::ImageFormat::GetPng());

Rendering Chart to PDF

In order to render the chart to PDF format, the Aspose.Cells APIs have exposed the Chart.ToPdf method with ability to store the resultant PDF on disc path or Stream.

 // Path of output pdf file

StringPtr outputPdfFile = outDir->StringAppend(new String("out1pdf.pdf"));

// Saving chart to PDF

chart->ToPdf(outputPdfFile);

Supported Chart Types for Rendering

There are a few chart types that are currently not supported for rendering. Such chart types contain N in the Supported column of below table.

Chart typeChart sub-typeSupported
ColumnColumnY
 ColumnStackedY
 Column100PercentStackedY
 Column3DClusteredY
 Column3DStackedY
 Column3D100PercentStackedY
 Column3DY
BarBarY
 BarStackedY
 Bar100PercentStackedY
 Bar3DClusteredY
 Bar3DStackedY
 Bar3D100PercentStackedY
LineLineY
 LineStackedY
 Line100PercentStackedY
 LineWithDataMarkersY
 LineStackedWithDataMarkersY
 Line100PercentStackedWithDataMarkersY
 Line3DY
PiePieY
 Pie3DY
 PiePieY
 PieExplodedY
 Pie3DExplodedY
 PieBarY
ScatterScatterY
 ScatterConnectedByCurvesWithDataMarkerY
 ScatterConnectedByCurvesWithoutDataMarkerY
 ScatterConnectedByLinesWithDataMarkerY
 ScatterConnectedByLinesWithoutDataMarkerY
AreaAreaY
 AreaStackedY
 Area100PercentStackedY
 Area3DY
 Area3DStackedY
 Area3D100PercentStackedY
DoughnutDoughnutY
 DoughnutExplodedY
RadarRadarY
 RadarWithDataMarkersY
 RadarFilledY
SurfaceSurface3DN
 SurfaceWireframe3DN
 SurfaceContourN
 SurfaceContourWireframeN
BubbleBubbleY
 Bubble3DN
StockStockHighLowCloseY
 StockOpenHighLowCloseY
 StockVolumeHighLowCloseY
 StockVolumeOpenHighLowCloseY
CylinderCylinderY
 CylinderStackedY
 Cylinder100PercentStackedY
 CylindricalBarY
 CylindricalBarStackedY
 CylindricalBar100PercentStackedY
 CylindricalColumn3DY
ConeConeY
 ConeStackedY
 Cone100PercentStackedY
 ConicalBarY
 ConicalBarStackedY
 ConicalBar100PercentStackedY
 ConicalColumn3DY
PyramidPyramidY
 PyramidStackedY
 Pyramid100PercentStackedY
 PyramidBarY
 PyramidBarStackedY
 PyramidBar100PercentStackedY
 PyramidColumn3DY