Create a Pie Chart in PowerPoint in C#

·

2 min read

A pie chart is a circular graph that represents data as slices of a whole. Each slice of the pie chart represents a specific category, and the size of each slice corresponds to the relative proportion of that category compared to the total. Sometimes, you may need to create pie charts in a PowerPoint slide to represent data visually. This article will share how to achieve the task in C# using a free .NET library.

Install the Free Library

First, it’s necessary to download the free library - Free Spire.Presentation for .NET via the link below or install it directly via Nuget.

https://www.e-iceblue.com/Download/download-presentation-for-net-free.html

Create a Pie Chart in PowerPoint Slide Using C#.

The free .NET PowerPoint library provides the ISlide.Shapes.AppendChart(ChartType type, RectangleF rectangle, bool init) method to add a chart at a specified location in a presentation slide. After the chart is created, you can then use the properties of the IChart class to set or format the chart title, chart data, chart series, etc.

Sample C# code.

using Spire.Presentation;
using Spire.Presentation.Charts;
using Spire.Presentation.Drawing;
using System.Drawing;


namespace CreatePieChartInPowerPoint
{
    class Program
    {
        static void Main(string[] args)
        {
            //Initialize an instance of Presentation class
            Presentation ppt = new Presentation();

            //Insert a pie chart at the specified position on the first slide 
            RectangleF rect1 = new RectangleF(40, 100, 550, 320);
            IChart chart = ppt.Slides[0].Shapes.AppendChart(ChartType.Pie, rect1, false);

            //Set and format chart title
            chart.ChartTitle.TextProperties.Text = "Sales by Quarter";
            chart.ChartTitle.TextProperties.IsCentered = true;
            chart.ChartTitle.Height = 30;
            chart.HasTitle = true;

            //Define some data
            string[] quarters = new string[] { "1st Qtr", "2nd Qtr", "3rd Qtr", "4th Qtr" };
            int[] sales = new int[] { 190, 350, 280, 300 };

            //Append data to ChartData, which represents a data table where the chart data is stored
            chart.ChartData[0, 0].Text = "Quarters";
            chart.ChartData[0, 1].Text = "Sales";
            for (int i = 0; i < quarters.Length; ++i)
            {
                chart.ChartData[i + 1, 0].Value = quarters[i];
                chart.ChartData[i + 1, 1].Value = sales[i];
            }

            //Set category labels, series label and series data
            chart.Series.SeriesLabel = chart.ChartData["B1", "B1"];
            chart.Categories.CategoryLabels = chart.ChartData["A2", "A5"];
            chart.Series[0].Values = chart.ChartData["B2", "B5"];

            //Add data points to series
            for (int i = 0; i < chart.Series[0].Values.Count; i++)
            {
                ChartDataPoint cdp = new ChartDataPoint(chart.Series[0]);
                cdp.Index = i;
                chart.Series[0].DataPoints.Add(cdp);

            }

            //Fill each data point with different color
            chart.Series[0].DataPoints[0].Fill.FillType = FillFormatType.Solid;
            chart.Series[0].DataPoints[0].Fill.SolidColor.Color = Color.LightBlue;
            chart.Series[0].DataPoints[1].Fill.FillType = FillFormatType.Solid;
            chart.Series[0].DataPoints[1].Fill.SolidColor.Color = Color.PaleGreen;
            chart.Series[0].DataPoints[2].Fill.FillType = FillFormatType.Solid;
            chart.Series[0].DataPoints[2].Fill.SolidColor.Color = Color.LightPink;
            chart.Series[0].DataPoints[3].Fill.FillType = FillFormatType.Solid;
            chart.Series[0].DataPoints[3].Fill.SolidColor.Color = Color.PaleVioletRed;

            //Show data labels
            chart.Series[0].DataLabels.LabelValueVisible = true;
            chart.Series[0].DataLabels.PercentValueVisible = true;

            //Save the result document
            ppt.SaveToFile("PieChart.pptx", FileFormat.Pptx2010);
            System.Diagnostics.Process.Start("PieChart.pptx");
        }
    }
}

If you want to create other types of charts in PowerPoint presentations and set formatting, you can click to learn more.

https://www.e-iceblue.com/Tutorials/Spire.Presentation/Program-Guide/Spire.Presentation-Program-Guide-Content.html

API references of the library:

https://www.e-iceblue.com/apireference/net/Spire.Presentation/html/N_Spire_Presentation.htm