Create Pie Chart in PowerPoint using Spire.Presenation in C#

Pie charts are a popular way to show how much individual amounts – such as quarterly sales figures – contribute to a total amount—such as annual sales. In this post, I am going to introduce how to create a pie chart in PowerPoint in C#.

2016-06-16_105733

Using Namespaces

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

Step 1: Create a new object of Presentation class.

Presentation ppt = new Presentation();

Step 2: Insert a Pie chart to the first slide.

RectangleF rect1 = new RectangleF(40, 40, 550, 320);
IChart chart = ppt.Slides[0].Shapes.AppendChart(ChartType.Pie3D, rect1, false);
chart.ChartTitle.TextProperties.Text = "Sales by Quarter";
chart.ChartTitle.TextProperties.IsCentered = true;
chart.ChartTitle.Height = 30;
chart.HasTitle = true;

Step 3: Define the data.

string[] quarters = new string[] { "1st Qtr", "2nd Qtr", "3rd Qtr", "4th Qtr" };
int[] sales = new int[] { 5480, 6540, 4780, 8500 };

Step 4: Append data to ChartData, which is the data table associated with chart.

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];
}

Step 5: 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"];

Step 6: Add data points to series and format each data point.

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);

}
chart.Series[0].DataPoints[0].Fill.FillType = FillFormatType.Solid;
chart.Series[0].DataPoints[0].Fill.SolidColor.Color = Color.RosyBrown;

chart.Series[0].DataPoints[1].Fill.FillType = FillFormatType.Solid;
chart.Series[0].DataPoints[1].Fill.SolidColor.Color = Color.LightBlue;

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.MediumPurple;

Step 7: Show label value and percentage value in data labels.

chart.Series[0].DataLabels.LabelValueVisible = true;
chart.Series[0].DataLabels.PercentValueVisible = true;

Step 8: Save the file.

ppt.SaveToFile("PieChart.pptx", FileFormat.Pptx2010);
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s