Articles in this section
Category / Section

How to create the PowerPoint pie chart in C# and VB.NET?

6 mins read

This article explains how to create a pie chart in .NET PowerPoint using Presentation library.

 

What is a pie chart

 

A pie chart (or a circle chart) is a circular statistical graphic that is divided into slices to illustrate the numerical proportion. In a pie chart, the arc length of each slice is proportional to the quantity it represents.

 

Create Pie Chart in Excel

        Pie chart created using Presentation library

 

 

Steps to create pie chart in PowerPoint using Presentation library:

 

  1. Initialize chart

 

Create a chart object by calling the slide.Charts.AddChart method and specify the chart type OfficeChartType.Pie enum value.

 

//Create the chart
IPresentationChart chart = slide.Charts.AddChart(100, 10, 700, 500);
 
//Set chart type to pie
chart.ChartType = OfficeChartType.Pie;

 

 

  1. Assign data and chart elements

 

Add the basic elements like chart title, data labels, and legend.

 

  • ChartTitle of chart object.
  • Set DataLabels via DefaultDataPoint.
  • Add CategoryLabels and Values.
  • Set TRUE to chart’s HasLegend property to show the legend, else FALSE.

 

//Apply chart elements
//Set chart title
chart.ChartTitle = " Pie Chart ";
 
//Set legend
chart.HasLegend = true;
              
//Set Datalabels, CategoryLabels, and Values
IOfficeChartSerie serie = chart.Series.Add("Percentage");
serie.CategoryLabels = chart.ChartData[3, 1, 7, 1];
serie.Values = chart.ChartData[3, 2, 7, 2];
serie.DataPoints.DefaultDataPoint.DataLabels.IsValue = true;
serie.DataPoints.DefaultDataPoint.DataLabels.Position = OfficeDataLabelPosition.Bottom;

 

 

Applicable properties of pie chart

 

Below is the list of other properties that is applicable for a pie chart.

 

1.  Percent (Pie Explosion)

2.  First Slice Angle

3.  IsVaryColor (All slices will be represented with same color if IsVaryColor is FALSE)

 

NOTE: Applying properties apart from the mentioned list may throw exception or the changes will not be reflected in the output document because, those properties are not related to pie chart.

 

Download Complete Sample

 

To learn more about creating charts with various settings using Presentation library, please refer to the documentation.

 

The following C#/ VB.NET complete code snippet shows the creation of pie chart using Presentation library.

 

C#

 

using Syncfusion.Presentation;
using Syncfusion.OfficeChart;
using System.IO;
 
namespace ChartSample
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create an instance for PowerPoint
            using (IPresentation pptxDoc = Presentation.Create())
            {
                //Add a blank slide to Presentation
                ISlide slide = pptxDoc.Slides.Add(SlideLayoutType.Blank);
 
                //Adds chart to the slide with position and size
                IPresentationChart chart = slide.Charts.AddChart(100, 10, 700, 500);
                chart.ChartType = OfficeChartType.Pie;
 
                //Assign data
                chart.DataRange = chart.ChartData[2, 1, 6, 2];
                chart.IsSeriesInRows = false;
 
                //Set data to the chart- RowIndex, columnIndex, and data
                SetChartData(chart);
 
                //Apply chart elements
                //Set Chart Title
                chart.ChartTitle = "Pie Chart";
 
                //Set Legend
                chart.HasLegend = true;
                chart.Legend.Position = OfficeLegendPosition.Bottom;
 
                //Set Datalabels
                IOfficeChartSerie serie = chart.Series[0];
                serie.DataPoints.DefaultDataPoint.DataLabels.IsValue = true;
                serie.DataPoints.DefaultDataPoint.DataLabels.Position = OfficeDataLabelPosition.BestFit;
 
                //Saving and closing the presentation
                Stream stream = File.Create("Output.pptx");
                pptxDoc.Save(stream);
            }
        }
 
        /// <summary>
        /// Set the values for the chart
        /// </summary>
        /// <param name="chart">Represent the instance of the Presentation chart</param>
        private static void SetChartData(IPresentationChart chart)
        {
            //Set the value for chart data
            chart.ChartData.SetValue(1, 1, "Food");
            chart.ChartData.SetValue(2, 1, "Fruits");
            chart.ChartData.SetValue(3, 1, "Vegetables");
            chart.ChartData.SetValue(4, 1, "Dairy");
            chart.ChartData.SetValue(5, 1, "Protein");
            chart.ChartData.SetValue(6, 1, "Grains");
            chart.ChartData.SetValue(1, 2, "Percentage");            
            chart.ChartData.SetValue(2, 2, 36);            
            chart.ChartData.SetValue(3, 2, 14);            
            chart.ChartData.SetValue(4, 2, 13);            
            chart.ChartData.SetValue(5, 2, 28);            
            chart.ChartData.SetValue(6, 2, 9);
        }
    }
}
 

 

VB

Imports Syncfusion.Presentation
Imports Syncfusion.OfficeChart
Imports System.IO
Namespace ChartSample
    Class Program
        Public Shared Sub Main(ByVal args() As String)
            'Create an instance for PowerPoint
            Dim pptxDoc As IPresentation = Presentation.Create()
            'Add a blank slide to Presentation
            Dim slide As ISlide = pptxDoc.Slides.Add(SlideLayoutType.Blank)
            'Adds chart to the slide with position and size
            Dim chart As IPresentationChart = slide.Charts.AddChart(100, 10, 700, 500)
            chart.ChartType = OfficeChartType.Pie
            'Assign data
            chart.DataRange = chart.ChartData(2, 1, 6, 2)
            chart.IsSeriesInRows = false
            'Set data to the chart RowIndex, columnIndex, and data
            Program.SetChartData(chart)
            'Apply chart elements
            'Set Chart Title
            chart.ChartTitle = "Pie Chart"
            'Set Legend
            chart.HasLegend = true
            chart.Legend.Position = OfficeLegendPosition.Bottom
            'Set Datalabels
            Dim serie As IOfficeChartSerie = chart.Series(0)
            serie.DataPoints.DefaultDataPoint.DataLabels.IsValue = true
            serie.DataPoints.DefaultDataPoint.DataLabels.Position = OfficeDataLabelPosition.BestFit
            'Saving and closing the presentation
            Dim stream As Stream = File.Create("Output.pptx")
            pptxDoc.Save(stream)
        End Sub
         
''' <summary>
        ''' Set the values for the chart
        ''' </summary>
        ''' <param name="chart">Represent the instance of the Presentation chart</param>
        Private Shared Sub SetChartData(ByVal chart As IPresentationChart)
            'Set the value for chart data
            chart.ChartData.SetValue(1, 1, "Food")
            chart.ChartData.SetValue(2, 1, "Fruits")
            chart.ChartData.SetValue(3, 1, "Vegetables")
            chart.ChartData.SetValue(4, 1, "Dairy")
            chart.ChartData.SetValue(5, 1, "Protein")
            chart.ChartData.SetValue(6, 1, "Grains")
            chart.ChartData.SetValue(1, 2, "Percentage")
            chart.ChartData.SetValue(2, 2, 36)
            chart.ChartData.SetValue(3, 2, 14)
            chart.ChartData.SetValue(4, 2, 13)
            chart.ChartData.SetValue(5, 2, 28)
            chart.ChartData.SetValue(6, 2, 9)
        End Sub
    End Class
End Namespace

 

Conclusion

I hope you enjoyed learning about how to create the PowerPoint pie chart in C# and VB.NET.

You can refer to our .NET Presentation feature tour page to know about its other groundbreaking feature representations. You can also explore our documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied