In this example, we will understand how to create Pie chart in Excel file using C++. You can insert Pie chart in Excel worksheet on Microsoft Windows, Linux, etc. programmatically in C++.
Steps to Create Pie Chart in Excel File using C++
- Configure the Aspose.Cells.Cpp package with NuGet package Manager
- Add reference to the Aspose::Cells namespace
- Initialize the Workbook class object to create new Excel file
- Add sample values to the cells
- Add series collection and set category data
- Format the datalabels by using the IDataLabels interface
- Save the output Excel file after inserting a Pie chart in C++
In the example below, you will learn how to insert Pie chart in Excel with C++. Initialize a blank workbook and make pie chart in Excel file using C++ simply by using a few API calls.
Code to Insert Pie Chart in Excel with C++
#pragma once | |
#include "Aspose.Cells.h" | |
class ExcelPieChart | |
{ | |
void CreateExcelPieChart() | |
{ | |
// Set the license for Aspose.Cells API for creating pie chart | |
intrusive_ptr<License> CellChartLicense = new License(); | |
CellChartLicense->SetLicense(new String("Aspose.Total.lic")); | |
// Instantiate the Workbook object to load Excel file for inserting pie chart | |
intrusive_ptr<IWorkbook> ChartWorkbook = Factory::CreateIWorkbook(); | |
//Accessing a worksheet using its index | |
intrusive_ptr<IWorksheet> ChartWorksheet = ChartWorkbook->GetIWorksheets()->GetObjectByIndex(0); | |
// Adding sample values to cells | |
ChartWorksheet->GetICells()->GetObjectByIndex(new String("A1"))->PutValue("Quarters"); | |
ChartWorksheet->GetICells()->GetObjectByIndex(new String("A2"))->PutValue("1st_Qtr"); | |
ChartWorksheet->GetICells()->GetObjectByIndex(new String("A3"))->PutValue("2nd_Qtr"); | |
ChartWorksheet->GetICells()->GetObjectByIndex(new String("A4"))->PutValue("3rd_Qtr"); | |
ChartWorksheet->GetICells()->GetObjectByIndex(new String("A5"))->PutValue("4th_Qtr"); | |
ChartWorksheet->GetICells()->GetObjectByIndex(new String("B1"))->PutValue("Sales"); | |
ChartWorksheet->GetICells()->GetObjectByIndex(new String("B2"))->PutValue("6.3"); | |
ChartWorksheet->GetICells()->GetObjectByIndex(new String("B3"))->PutValue("3.1"); | |
ChartWorksheet->GetICells()->GetObjectByIndex(new String("B4"))->PutValue("2.2"); | |
ChartWorksheet->GetICells()->GetObjectByIndex(new String("B5"))->PutValue("1.9"); | |
// Adding a Pie chart to the worksheet in the workbook | |
int chartIndex = ChartWorksheet->GetICharts()->Add(ChartType::ChartType_Pie, 10, 2, 34, 13); | |
// Accessing the instance of the newly created Pie chart | |
intrusive_ptr<Aspose::Cells::Charts::IChart> PieChart = ChartWorksheet->GetICharts()->GetObjectByIndex(chartIndex); | |
// Adding SeriesCollection to the chart from "A2" to "B5" | |
PieChart->GetNISeries()->Add(new String("A2:B5"), true); | |
PieChart->GetNISeries()->SetCategoryData(new String("A2:A5")); | |
PieChart->GetITitle()->SetText(new String("Sales By Quarter")); | |
PieChart->GetITitle()->GetIFont()->SetColor(Systems::Drawing::Color::GetBlue()); | |
PieChart->GetITitle()->GetIFont()->SetBold(true); | |
PieChart->GetITitle()->GetIFont()->SetSize(11); | |
// Format the datalebels of all series | |
for (int i = 0; i < PieChart->GetNISeries()->GetCount(); i++) | |
{ | |
intrusive_ptr<IDataLabels> dataLabels = PieChart->GetNISeries()->GetObjectByIndex(i)->GetIDataLabels(); | |
dataLabels->SetShowValue(true); | |
dataLabels->SetShowPercentage(true); | |
} | |
// Save the output Excel file with newly created pie chart | |
ChartWorkbook->Save(new String ("output.xlsx")); | |
} | |
}; |
You can add pie chart in Excel file with C++ by inserting the chart values and formatting the datalabels programmatically. You can easily create the pie chart without needing to install MS Excel or any other tool. In previous example, we explored How to Remove Formula from Excel File in C++ which contains the example for removing formula from Excel file.