create charts in powerpoint ppt C#

Charts are used to summarize and visually represent the data within the PowerPoint presentations. Therefore, PowerPoint provides a wide range of chart types to visualize the data. Among all, the most commonly used chart types include pie charts, line charts, bar charts, histograms, stock charts, and etc. In this article, you will learn how to create these charts in PowerPoint presentations using C#.

C# API to Create Charts in PowerPoint

Aspose.Slides for .NET is a C# class library that lets you create and manipulate PowerPoint presentations from within .NET applications. Furthermore, the API allows you to create and add charts to the presentations seamlessly. Aspose.Slides for .NET can be downloaded as DLL or installed via NuGet.

PM> Install-Package Aspose.Slides

Create Column Chart in PowerPoint PPT using C#

In this section, you will learn how to create a column chart and add categories and series to populate that chart. The following are the steps to perform this operation.

For demonstration, the following code sample shows how to create a column chart in PowerPoint presentation using C#.

// Instantiate Presentation class that represents PPTX file
Presentation pres = new Presentation();
// Access first slide
ISlide sld = pres.Slides[0];
// Add chart with default data
IChart chart = sld.Shapes.AddChart(ChartType.ClusteredColumn, 0, 0, 500, 500);
// Setting chart Title
chart.ChartTitle.AddTextFrameForOverriding("Sample Title");
chart.ChartTitle.TextFrameForOverriding.TextFrameFormat.CenterText = NullableBool.True;
chart.ChartTitle.Height = 20;
chart.HasTitle = true;
// Set first series to Show Values
chart.ChartData.Series[0].Labels.DefaultDataLabelFormat.ShowValue = true;
// Setting the index of chart data sheet
int defaultWorksheetIndex = 0;
// Getting the chart data worksheet
IChartDataWorkbook fact = chart.ChartData.ChartDataWorkbook;
// Delete default generated series and categories
chart.ChartData.Series.Clear();
chart.ChartData.Categories.Clear();
int s = chart.ChartData.Series.Count;
s = chart.ChartData.Categories.Count;
// Adding new series
chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 0, 1, "Series 1"), chart.Type);
chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 0, 2, "Series 2"), chart.Type);
// Adding new categories
chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 1, 0, "Caetegoty 1"));
chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 2, 0, "Caetegoty 2"));
chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 3, 0, "Caetegoty 3"));
// Take first chart series
IChartSeries series = chart.ChartData.Series[0];
// Now populating series data
series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 1, 1, 20));
series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 2, 1, 50));
series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 3, 1, 30));
// Setting fill color for series
series.Format.Fill.FillType = FillType.Solid;
series.Format.Fill.SolidFillColor.Color = System.Drawing.Color.Blue;
// Take second chart series
series = chart.ChartData.Series[1];
// Now populating series data
series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 1, 2, 30));
series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 2, 2, 10));
series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 3, 2, 60));
// Setting fill color for series
series.Format.Fill.FillType = FillType.Solid;
series.Format.Fill.SolidFillColor.Color = Color.Orange;
// First label will be show Category name
IDataLabel lbl = series.DataPoints[0].Label;
lbl.DataLabelFormat.ShowCategoryName = true;
lbl = series.DataPoints[1].Label;
lbl.DataLabelFormat.ShowSeriesName = true;
// Show value for third label
lbl = series.DataPoints[2].Label;
lbl.DataLabelFormat.ShowValue = true;
lbl.DataLabelFormat.ShowSeriesName = true;
lbl.DataLabelFormat.Separator = "/";
// Save presentation with chart
pres.Save("column-chart.pptx", SaveFormat.Pptx);

The following is the screenshot of the resultant column chart.

create column chart in powerpoint in C#

Create Scatter Chart in PowerPoint PPT using C#

The following are the steps to create a scatter chart in the PowerPoint presentation using C#.

  • Create a new presentation using the Presentation class.
  • Get the reference of the slides in the ISlide object.
  • Add a ScatterWithSmoothLines chart type with default data and get its reference in IChart object.
  • Access the chart data workbook into the IChartDataWorkbook object and clear the default series.
  • Add new series to the chart data.
  • Access each series into the IChartSeries object and add data points to the series.
  • Set the marker for the series using IChartSeries.Marker property.
  • Save the presentation using Presentation.Save(String, SaveFormat) method.

The following code sample shows how to create a scatter chart in PowerPoint presentations using C#.

// Instantiate Presentation class that represents PPTX file
Presentation pres = new Presentation();
// Access first slide
ISlide sld = pres.Slides[0];
// Add chart with default data
IChart chart = sld.Shapes.AddChart(ChartType.ScatterWithSmoothLines, 0, 0, 400, 400);
// Getting the default chart data worksheet index
int defaultWorksheetIndex = 0;
// Getting the chart data worksheet
IChartDataWorkbook fact = chart.ChartData.ChartDataWorkbook;
// Delete demo series
chart.ChartData.Series.Clear();
// Add new series
chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 1, 1, "Series 1"), chart.Type);
chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 1, 3, "Series 2"), chart.Type);
// Take first chart series
IChartSeries series = chart.ChartData.Series[0];
// Add new point (1:3) there.
series.DataPoints.AddDataPointForScatterSeries(fact.GetCell(defaultWorksheetIndex, 2, 1, 1), fact.GetCell(defaultWorksheetIndex, 2, 2, 3));
// Add new point (2:10)
series.DataPoints.AddDataPointForScatterSeries(fact.GetCell(defaultWorksheetIndex, 3, 1, 2), fact.GetCell(defaultWorksheetIndex, 3, 2, 10));
// Edit the type of series
series.Type = ChartType.ScatterWithStraightLinesAndMarkers;
// Changing the chart series marker
series.Marker.Size = 10;
series.Marker.Symbol = MarkerStyleType.Star;
// Take second chart series
series = chart.ChartData.Series[1];
// Add new point (5:2) there.
series.DataPoints.AddDataPointForScatterSeries(fact.GetCell(defaultWorksheetIndex, 2, 3, 5), fact.GetCell(defaultWorksheetIndex, 2, 4, 2));
// Add new point (3:1)
series.DataPoints.AddDataPointForScatterSeries(fact.GetCell(defaultWorksheetIndex, 3, 3, 3), fact.GetCell(defaultWorksheetIndex, 3, 4, 1));
// Add new point (2:2)
series.DataPoints.AddDataPointForScatterSeries(fact.GetCell(defaultWorksheetIndex, 4, 3, 2), fact.GetCell(defaultWorksheetIndex, 4, 4, 2));
// Add new point (5:1)
series.DataPoints.AddDataPointForScatterSeries(fact.GetCell(defaultWorksheetIndex, 5, 3, 5), fact.GetCell(defaultWorksheetIndex, 5, 4, 1));
// Changing the chart series marker
series.Marker.Size = 10;
series.Marker.Symbol = MarkerStyleType.Circle;
// Save presentation with chart
pres.Save("scattered-chart.pptx", SaveFormat.Pptx);

The following screenshot shows the resultant scatter chart.

create scattered chart in powerpoint in C#

Create Pie Chart in PowerPoint PPT using C#

The following are the steps to create a pie chart in a PowerPoint presentation using C#.

The following code sample shows how to create a pie chart in PowerPoint presentation using C#.

// Instantiate Presentation class that represents PPTX file
Presentation presentation = new Presentation();
// Access first slide
ISlide slides = presentation.Slides[0];
// Add chart with default data
IChart chart = slides.Shapes.AddChart(ChartType.Pie, 100, 100, 400, 400);
// Setting chart Title
chart.ChartTitle.AddTextFrameForOverriding("Sample Title");
chart.ChartTitle.TextFrameForOverriding.TextFrameFormat.CenterText = NullableBool.True;
chart.ChartTitle.Height = 20;
chart.HasTitle = true;
// Set first series to Show Values
chart.ChartData.Series[0].Labels.DefaultDataLabelFormat.ShowValue = true;
// Setting the index of chart data sheet
int defaultWorksheetIndex = 0;
// Getting the chart data worksheet
IChartDataWorkbook fact = chart.ChartData.ChartDataWorkbook;
// Delete default generated series and categories
chart.ChartData.Series.Clear();
chart.ChartData.Categories.Clear();
// Adding new categories
chart.ChartData.Categories.Add(fact.GetCell(0, 1, 0, "First Qtr"));
chart.ChartData.Categories.Add(fact.GetCell(0, 2, 0, "2nd Qtr"));
chart.ChartData.Categories.Add(fact.GetCell(0, 3, 0, "3rd Qtr"));
// Adding new series
IChartSeries series = chart.ChartData.Series.Add(fact.GetCell(0, 0, 1, "Series 1"), chart.Type);
// Now populating series data
series.DataPoints.AddDataPointForPieSeries(fact.GetCell(defaultWorksheetIndex, 1, 1, 20));
series.DataPoints.AddDataPointForPieSeries(fact.GetCell(defaultWorksheetIndex, 2, 1, 50));
series.DataPoints.AddDataPointForPieSeries(fact.GetCell(defaultWorksheetIndex, 3, 1, 30));
// Not working in new version
// Adding new points and setting sector color
// series.IsColorVaried = true;
chart.ChartData.SeriesGroups[0].IsColorVaried = true;
IChartDataPoint point = series.DataPoints[0];
point.Format.Fill.FillType = FillType.Solid;
point.Format.Fill.SolidFillColor.Color = Color.Orange;
// Setting Sector border
point.Format.Line.FillFormat.FillType = FillType.Solid;
point.Format.Line.FillFormat.SolidFillColor.Color = Color.Gray;
point.Format.Line.Width = 3.0;
//point.Format.Line.Style = LineStyle.ThinThick;
//point.Format.Line.DashStyle = LineDashStyle.DashDot;
IChartDataPoint point1 = series.DataPoints[1];
point1.Format.Fill.FillType = FillType.Solid;
point1.Format.Fill.SolidFillColor.Color = Color.BlueViolet;
// Setting Sector border
point1.Format.Line.FillFormat.FillType = FillType.Solid;
point1.Format.Line.FillFormat.SolidFillColor.Color = Color.Blue;
point1.Format.Line.Width = 3.0;
//point1.Format.Line.Style = LineStyle.Single;
//point1.Format.Line.DashStyle = LineDashStyle.LargeDashDot;
IChartDataPoint point2 = series.DataPoints[2];
point2.Format.Fill.FillType = FillType.Solid;
point2.Format.Fill.SolidFillColor.Color = Color.YellowGreen;
// Setting Sector border
point2.Format.Line.FillFormat.FillType = FillType.Solid;
point2.Format.Line.FillFormat.SolidFillColor.Color = Color.Red;
point2.Format.Line.Width = 2.0;
//point2.Format.Line.Style = LineStyle.ThinThin;
//point2.Format.Line.DashStyle = LineDashStyle.LargeDashDotDot;
// Create custom labels for each of categories for new series
IDataLabel lbl1 = series.DataPoints[0].Label;
// lbl.ShowCategoryName = true;
lbl1.DataLabelFormat.ShowValue = true;
IDataLabel lbl2 = series.DataPoints[1].Label;
lbl2.DataLabelFormat.ShowValue = true;
lbl2.DataLabelFormat.ShowLegendKey = true;
lbl2.DataLabelFormat.ShowPercentage = true;
IDataLabel lbl3 = series.DataPoints[2].Label;
lbl3.DataLabelFormat.ShowSeriesName = true;
lbl3.DataLabelFormat.ShowPercentage = true;
// Showing Leader Lines for Chart
//series.Labels.DefaultDataLabelFormat.ShowLeaderLines = true;
// Setting Rotation Angle for Pie Chart Sectors
chart.ChartData.SeriesGroups[0].FirstSliceAngle = 180;
// Save presentation with chart
presentation.Save("pie-chart.pptx", SaveFormat.Pptx);

The following is the screenshot of the generated pie chart.

create pie chart in powerpoint in C#

Add Histogram Chart in PowerPoint PPTX using C#

The following are the steps to create a histogram chart in PowerPoint presentations using Aspose.Slides for .NET.

  • Create an instance of the Presentation class.
  • Obtain a slide’s reference in the ISlide object by its index.
  • Add a Histogram chart with default data.
  • Clear the default series and categories.
  • Access the chart data workbook in the IChartDataWorkbook object.
  • Add new series and get its reference in the IChartSeries object.
  • Add data points to the series.
  • Set aggregation type of the chart axis.
  • Save the presentation using Presentation.Save(String, SaveFormat) method.

The following code sample shows how to create a histogram chart using C#.

// Load or create presentation
using (Presentation pres = new Presentation())
{
// Add histogram chart
IChart chart = pres.Slides[0].Shapes.AddChart(ChartType.Histogram, 50, 50, 500, 400);
chart.ChartData.Categories.Clear();
chart.ChartData.Series.Clear();
// Access chart data workbook
IChartDataWorkbook wb = chart.ChartData.ChartDataWorkbook;
// Clear workbook
wb.Clear(0);
// Add chart series
IChartSeries series = chart.ChartData.Series.Add(ChartType.Histogram);
series.DataPoints.AddDataPointForHistogramSeries(wb.GetCell(0, "A1", 15));
series.DataPoints.AddDataPointForHistogramSeries(wb.GetCell(0, "A2", -41));
series.DataPoints.AddDataPointForHistogramSeries(wb.GetCell(0, "A3", 16));
series.DataPoints.AddDataPointForHistogramSeries(wb.GetCell(0, "A4", 10));
series.DataPoints.AddDataPointForHistogramSeries(wb.GetCell(0, "A5", -23));
series.DataPoints.AddDataPointForHistogramSeries(wb.GetCell(0, "A6", 16));
chart.Axes.HorizontalAxis.AggregationType = AxisAggregationType.Automatic;
// Save presentation
pres.Save("histogram-chart.pptx", SaveFormat.Pptx);
}

The following is the screenshot of the created histogram chart.

create histogram chart in powerpoint in C#

Create a Stock Chart in PowerPoint using C#

Stock chart is also one of the commonly used chart types within PowerPoint presentations. The following are the steps to create a stock chart.

  • Create a new PowerPoint presentation using Presentation class.
  • Obtain slide’s reference in an ISlide object using the slide’s index.
  • Add OpenHighLowClose chart to the slide and get its reference in the IChart object.
  • Clear the default series and categories.
  • Access the chart data in IChartDataWorkbook object.
  • Add new series and categories to the chart.
  • Access each chart series and add data points.
  • Specify HiLowLines format.
  • Save the presentation using Presentation.Save(String, SaveFormat) method.

The following code sample shows how to add a stock chart to PowerPoint presentation using C#.

// Load or create presentation
using (Presentation pres = new Presentation())
{
// Add chart
IChart chart = pres.Slides[0].Shapes.AddChart(ChartType.OpenHighLowClose, 50, 50, 600, 400, false);
// Clear categories and series
chart.ChartData.Series.Clear();
chart.ChartData.Categories.Clear();
// Access chart data workbook
IChartDataWorkbook wb = chart.ChartData.ChartDataWorkbook;
// Add categories
chart.ChartData.Categories.Add(wb.GetCell(0, 1, 0, "A"));
chart.ChartData.Categories.Add(wb.GetCell(0, 2, 0, "B"));
chart.ChartData.Categories.Add(wb.GetCell(0, 3, 0, "C"));
// Add series
chart.ChartData.Series.Add(wb.GetCell(0, 0, 1, "Open"), chart.Type);
chart.ChartData.Series.Add(wb.GetCell(0, 0, 2, "High"), chart.Type);
chart.ChartData.Series.Add(wb.GetCell(0, 0, 3, "Low"), chart.Type);
chart.ChartData.Series.Add(wb.GetCell(0, 0, 4, "Close"), chart.Type);
// Add data points
IChartSeries series = chart.ChartData.Series[0];
series.DataPoints.AddDataPointForStockSeries(wb.GetCell(0, 1, 1, 72));
series.DataPoints.AddDataPointForStockSeries(wb.GetCell(0, 2, 1, 25));
series.DataPoints.AddDataPointForStockSeries(wb.GetCell(0, 3, 1, 38));
series = chart.ChartData.Series[1];
series.DataPoints.AddDataPointForStockSeries(wb.GetCell(0, 1, 2, 172));
series.DataPoints.AddDataPointForStockSeries(wb.GetCell(0, 2, 2, 57));
series.DataPoints.AddDataPointForStockSeries(wb.GetCell(0, 3, 2, 57));
series = chart.ChartData.Series[2];
series.DataPoints.AddDataPointForStockSeries(wb.GetCell(0, 1, 3, 12));
series.DataPoints.AddDataPointForStockSeries(wb.GetCell(0, 2, 3, 12));
series.DataPoints.AddDataPointForStockSeries(wb.GetCell(0, 3, 3, 13));
series = chart.ChartData.Series[3];
series.DataPoints.AddDataPointForStockSeries(wb.GetCell(0, 1, 4, 25));
series.DataPoints.AddDataPointForStockSeries(wb.GetCell(0, 2, 4, 38));
series.DataPoints.AddDataPointForStockSeries(wb.GetCell(0, 3, 4, 50));
// Set whether chart has up/down bars
chart.ChartData.SeriesGroups[0].UpDownBars.HasUpDownBars = true;
// Specify hi/low line format
chart.ChartData.SeriesGroups[0].HiLowLinesFormat.Line.FillFormat.FillType = FillType.Solid;
foreach (IChartSeries ser in chart.ChartData.Series)
{
ser.Format.Line.FillFormat.FillType = FillType.NoFill;
}
// Save presentation
pres.Save("stock-chart.pptx", SaveFormat.Pptx);
}

The following is the screenshot of the created stock chart.

create stock chart in powerpoint in C#

More Chart Types

In addition to the above-mentioned charts, there are other types of charts as well that you can add to the PowerPoint presentations. In order to read more about the supported chart types, you can visit this documentation article.

Get a Free API License

You can get a free temporary license in order to try the API without evaluation limitations.

Conclusion

In this article, you have learned how to create charts in PowerPoint presentations using C#. Furthermore, the step-by-step guide and code samples have shown how to add column charts, scatter charts, pie charts, histograms, and stock charts. You can explore more about Aspose.Slides for .NET using documentation.

See Also