How to Create Excel Pie Chart in C#

In this topic, we will explain easy-to-use steps on how to create Excel Pie Chart in C# .NET application. The steps below exhibit how to create dynamic Pie chart in C# windows application programmatically.

Commonly, a Pie chart denotes categorical data whereas each pie slice can show specific category. In MS Excel, you may utilize rich set of chart tools. So, you can make Excel Pie chart in C# project on the fly. Next, you may save to Excel XLSX format. You can simply open the output XLSX file into some Excel viewer to display the graph.

Steps to Create Excel Pie Chart in C#

  1. Download Aspose.Cells for .NET package from NuGet Gallery
  2. Import relevant Aspose.Cells and Aspose.Cells.Charts namespaces with using directive
  3. Instantiate License object and set the license file using SetLicense method
  4. Create a Workbook object to create a new Excel workbook
  5. Get the first worksheet in the workbook and add data to the worksheet
  6. Add new Pie chart using ChartType.Pie enum member and set its data series with other attributes
  7. Save the output Excel file that contains the Pie chart in it

In below example, we will be creating a Pie chart dynamically based on quarterly sales data. The source data for the chart will also be pasted via code. Moreover, we set chart’s title and specify data labels properties to show values and percentages on the pie slices. We can use ChartType enumeration to select or set other similar types to draw charts.

Sample code to Create Excel Pie Chart in C#

using System;
using System.Drawing;
using System.Windows.Forms;
//Add reference to following namespaces for Aspose.Cells for .NET API
using Aspose.Cells;
using Aspose.Cells.Charts;
namespace CreateExcelPieChart
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//Important: Initialize License object and set it first before using any other APIs
//Aspose.Cells for .NET
Aspose.Cells.License AsposeCellslic = new Aspose.Cells.License();
AsposeCellslic.SetLicense("Aspose.Cells.lic");
//Create a new Excel Workbook
Workbook ExcelChartWorkbook = new Workbook();
//Obtain first worksheet (i.e., default sheet)
Worksheet ExcelChartWorksheet = ExcelChartWorkbook.Worksheets[0];
//Set the name of the sheet
ExcelChartWorksheet.Name = "PieChart";
//Get worksheet cells collection from the sheet
Cells WorksheetCells = ExcelChartWorksheet.Cells;
//Input values into the cells, i.e., the source data for the pie chart
WorksheetCells["A1"].PutValue("Quarters");
WorksheetCells["A2"].PutValue("1st_Qtr");
WorksheetCells["A3"].PutValue("2nd_Qtr");
WorksheetCells["A4"].PutValue("3rd_Qtr");
WorksheetCells["A5"].PutValue("4th_Qtr");
WorksheetCells["B1"].PutValue("Sales");
WorksheetCells["B2"].PutValue(6.3);
WorksheetCells["B3"].PutValue(3.1);
WorksheetCells["B4"].PutValue(2.2);
WorksheetCells["B5"].PutValue(1.9);
//Create or add excel pie chart
int chart_Index = 0;
chart_Index = ExcelChartWorksheet.Charts.Add(ChartType.Pie, 10, 2, 34, 13);
Chart WorksheetChart = ExcelChartWorksheet.Charts[chart_Index];
//Set the data series and category data for the pie chart
WorksheetChart.NSeries.Add("B2:B5", true);
WorksheetChart.NSeries.CategoryData = "A2:A5";
//Set chart title properties
WorksheetChart.Title.Text = "Sales By Quarter";
WorksheetChart.Title.Font.Color = Color.Blue;
WorksheetChart.Title.Font.IsBold = true;
WorksheetChart.Title.Font.Size = 11;
//Set the data labels attributes of the pie chart slices
DataLabels data_labels;
for (int i = 0; i < WorksheetChart.NSeries.Count; i++)
{
data_labels = WorksheetChart.NSeries[i].DataLabels;
data_labels.ShowValue = true;
data_labels.ShowPercentage = true;
}
//Save the excel file containing the chart
ExcelChartWorkbook.Save("e:\\test2\\pie_chart.xlsx");
}
}
}

The above code snippet inserts some data into the first worksheet cells and then creates an Excel Pie graph in C# based on that data source. Finally, it saves an Excel XLSX file which will contain the Pie chart. Moreover, you can convert Excel chart to JPG in C# without Interop as well.

 English