Charts are an excellent tool for concisely showing data. Moreover, they make it easier to consume large amounts of data by representing it visually. Adding charts to your presentations may prove helpful when presenting data such as the company’s growth trends or product adoption rate. To that end, this article will teach you how to create charts in PowerPoint presentations using C++.
- C++ API for Creating Charts in PowerPoint Presentations
- Create Column Chart in PowerPoint Presentations using C++
- Creating Pie Chart in PowerPoint Presentations using C++
- Create Scattered Chart in PowerPoint Presentation using C++
- Create Histogram Chart in PowerPoint Presentations
- Additional Supported Charts
- Get a Free License
C++ API for Creating Charts in PowerPoint Presentations
Aspose.Slides for C++ is a native C++ library that supports creating, reading, and manipulating PowerPoint files. The API also supports creating charts in PowerPoint presentations. You can either install the API through NuGet or download it directly from the Downloads section.
PM> Install-Package Aspose.Slides.Cpp
Create Column Chart in PowerPoint Presentations using C++
The following are the steps to create a column chart in PowerPoint presentations.
- Firstly, create an instance of the Presentation class.
- Access the slide on which you want to add the column chart using Presentation->get_Slides()->idx_get (int32_t index).
- Add a ClusteredColumn chart to the slide using ISlide->get_Shapes()->AddChart (Charts::ChartType type, float x, float y, float width, float height) method.
- Access the chart data workbook using IChart->get_ChartData()->get_ChartDataWorkbook() method.
- Set the title of the chart using the IChart->get_ChartTitle()->AddTextFrameForOverriding (System::String text) method.
- Clear the default series and categories from chart data using IChart->get_ChartData()->get_Series()->Clear() and IChart->get_ChartData()->get_Categories()->Clear() methods respectively.
- Add new series and categories using IChart->get_ChartData()->get_Series()->Add (System::SharedPtr<IChartDataCell> cellWithSeriesName, ChartType type) and IChart->get_ChartData()->get_Categories()->Add (System::SharedPtr<IChartDataCell> chartDataCell) methods respectively.
- Access each series using IChart->get_ChartData()->get_Series()->idx_get (int32_t index) method.
- Add data points, fill color and labels for each series.
- Finally, save the presentation containing the column chart using the Presentation->Save (System::String name, Export::SaveFormat format) method.
The following is the sample code to add a column chart in PowerPoint Presentation using C++.
The following is the image of the column chart generated by the sample code.
Creating Pie Chart in PowerPoint Presentations using C++
The following are the steps to add a pie chart to PowerPoint slides.
- Firstly, create an instance of the Presentation class.
- Access the slide on which you want to add the pie chart using Presentation->get_Slides()->idx_get (int32_t index).
- Add a Pie chart to the slide using ISlide->get_Shapes()->AddChart (Charts::ChartType type, float x, float y, float width, float height) method.
- Set the title of the chart using the IChart->get_ChartTitle()->AddTextFrameForOverriding (System::String text) method.
- Clear the default series and categories from chart data using IChart->get_ChartData()->get_Series()->Clear() and IChart->get_ChartData()->get_Categories()->Clear() methods respectively.
- Access the chart data workbook using IChart->get_ChartData()->get_ChartDataWorkbook() method.
- Add new series and categories using IChart->get_ChartData()->get_Series()->Add (System::SharedPtr<IChartDataCell> cellWithSeriesName, ChartType type) and IChart->get_ChartData()->get_Categories()->Add (System::SharedPtr<IChartDataCell> chartDataCell) methods respectively.
- Access each series using IChart->get_ChartData()->get_Series()->idx_get (int32_t index) method.
- Add data points using IChartSeries->get_DataPoints()->AddDataPointForPieSeries (System::SharedPtr<IChartDataCell> value) method.
- Format the data points, add leader lines, and set the rotation angle.
- Finally, save the presentation containing the pie chart using the Presentation->Save (System::String name, Export::SaveFormat format) method.
The following is the sample code for adding a pie chart in PowerPoint slides using C++.
The following is the image of the pie chart generated by the sample code.
Create Scattered Chart in PowerPoint Presentation using C++
The following are the steps to add a scattered chart to PowerPoint slides.
- Firstly, create an instance of the Presentation class.
- Access the slide on which you want to add the scattered chart using Presentation->get_Slides()->idx_get (int32_t index).
- Add a ScatterWithSmoothLines chart to the slide using ISlide->get_Shapes()->AddChart (Charts::ChartType type, float x, float y, float width, float height) method.
- Clear the default series from chart data using IChart->get_ChartData()->get_Series()->Clear() method.
- Access the chart data workbook using IChart->get_ChartData()->get_ChartDataWorkbook() method.
- Add new series using IChart->get_ChartData()->get_Series()->Add (System::SharedPtr<IChartDataCell> cellWithSeriesName, ChartType type) method.
- Access each series using IChart->get_ChartData()->get_Series()->idx_get (int32_t index) method.
- Add data points using IChartSeries->get_DataPoints()->AddDataPointForScatterSeries (System::SharedPtr<IChartDataCell> xValue, System::SharedPtr<IChartDataCell> yValue) method.
- Set the marker for the series.
- Finally, save the presentation containing the scattered chart using the Presentation->Save (System::String name, Export::SaveFormat format) method.
The following is the sample code to add a scattered chart to PowerPoint slides using C++.
The following is the image of the scattered chart generated by the sample code.
Create Histogram Chart in PowerPoint Presentations
The following are the steps to create a histogram chart in PowerPoint presentations.
- Firstly, create an instance of the Presentation class.
- Access the slide on which you want to add the histogram chart using Presentation->get_Slides()->idx_get (int32_t index).
- Add a Histogram chart to the slide using ISlide->get_Shapes()->AddChart (Charts::ChartType type, float x, float y, float width, float height) method.
- Clear the default series and categories from chart data using IChart->get_ChartData()->get_Series()->Clear() and IChart->get_ChartData()->get_Categories()->Clear() methods respectively.
- Access the chart data workbook using IChart->get_ChartData()->get_ChartDataWorkbook() method.
- Add new series using IChart->get_ChartData()->get_Series()->Add (ChartType type) method.
- Add data points using IChartSeries->get_DataPoints()->AddDataPointForHistogramSeries (System::SharedPtr<IChartDataCell> value) method.
- Set aggregation type of the chart axis using IChart->get_Axes()->get_HorizontalAxis()->set_AggregationType (AxisAggregationType value) method.
- Finally, save the presentation containing the histogram chart using the Presentation->Save (System::String name, Export::SaveFormat format) method.
The following is the sample code to create a histogram chart in PowerPoint presentations using C++.
The following is the image of the histogram chart generated by the sample code.
Additional Supported Charts
Other than the charts shown above, Aspose.Slides for C++ supports many more chart types. You can view the complete list of supported chart types with sample code by reading this documentation article.
Get a Free License
You can request a free temporary license to try the API without evaluation limitations.
Conclusion
In this article, you have learned how to add charts in PowerPoint slides using C++. Specifically, you have learned how to add Column, Scattered, Pie, and Histogram charts in your PowerPoint presentations. Additionally, you have seen that Aspose.Slides for C++ API provides a lot more chart types for you to use within your PowerPoint presentations. Besides charts, the API proves a bunch of features for enhancing your PowerPoint presentations. You can explore the API in detail by using the official documentation. In case of any questions, please feel free to contact us on the free support forum.