Create Charts In PowerPoint Using C#

When it comes to PowerPoint presentation, charts are an import thing because charts can visualize large amount of complex data and are helpful for data comparison and analysis. This article focuses on how to create most commonly used chart types (including column chart, pie chart and combination chart) by using .NET Presentation library in your C# applications.

Introduction

When it comes to PowerPoint presentation, charts are an import thing because charts can visualize a large amount of complex data and are helpful for data comparison and analysis. This article focuses on how to create the most commonly used chart types (including column chart, pie chart, and combination chart) by using .NET Presentation library in your C# applications.

Before starting, you’re required to download the library from the link provided, add the DLL files as references in your project, and import the following necessary namespaces at the beginning.

  1. using System;  
  2. using Spire.Presentation;  
  3. using System.Drawing;  
  4. using Spire.Presentation.Charts;  
  5. using System.Data;  
  6. using Spire.Presentation.Drawing;   

Using the code,

How to create a column chart

A column chart displays a series as a set of vertical bars that are grouped by category.

  1. //create a PowerPoint document  
  2. Presentation presentation = new Presentation();  
  3.   
  4. //insert a column chart  
  5. RectangleF rect = new RectangleF(40, 100, 550, 320);  
  6. IChart chart = presentation.Slides[0].Shapes.AppendChart(ChartType.ColumnClustered, rect);  
  7.   
  8. //set title  
  9. chart.ChartTitle.TextProperties.Text = "Male/Female Ratio Per Dept.";  
  10. chart.ChartTitle.TextProperties.IsCentered = true;  
  11. chart.ChartTitle.Height = 30;  
  12. chart.HasTitle = true;  
  13.   
  14. //define a multi-dimensional array of strings  
  15. string[,] data = new string[,]  
  16. {  
  17.   {"Department","Male","Female" },  
  18.   {"Development","25","15"},  
  19.   {"Testing","5","10" },  
  20.   {"Sales","7","3" },  
  21.   {"Support","20","5" }  
  22. };  
  23.   
  24. //write data to chart data  
  25. for (int i = 0; i < data.GetLength(0); i++)  
  26. {  
  27.     for (int j = 0; j < data.GetLength(1); j++)  
  28.     {  
  29.         int number;  
  30.         bool result = Int32.TryParse(data[i, j], out number);  
  31.         if (result)  
  32.         {  
  33.             chart.ChartData[i, j].Value = number;  
  34.         }  
  35.         else  
  36.         {  
  37.             chart.ChartData[i, j].Value = data[i, j];  
  38.         }  
  39.     }  
  40. }  
  41.   
  42. //set series labels  
  43. chart.Series.SeriesLabel = chart.ChartData["B1""C1"];  
  44.   
  45. //set category labels  
  46. chart.Categories.CategoryLabels = chart.ChartData["A2""A5"];  
  47.   
  48. //set the series values  
  49. chart.Series[0].Values = chart.ChartData["B2""B5"];  
  50. chart.Series[1].Values = chart.ChartData["C2""C5"];  
  51.   
  52. //apply built-in chart style  
  53. chart.ChartStyle = ChartStyle.Style11;  
  54.   
  55. //set overlap  
  56. chart.OverLap = -50;  
  57.   
  58. //set gap width  
  59. chart.GapWidth = 200;  
  60.   
  61. //save to file  
  62. presentation.SaveToFile("ColumnChart.pptx", FileFormat.Pptx2010);  

Column Chart
Figure 1. Column Chart 

How to create a pie chart

A pie chart helps show the proportions and percentages between categories, by dividing a circle into proportional segments.

  1. //create a PowerPoint document  
  2. Presentation ppt = new Presentation();  
  3.   
  4. //insert a pie chart  
  5. RectangleF rect1 = new RectangleF(40, 100, 550, 320);  
  6. IChart chart = ppt.Slides[0].Shapes.AppendChart(ChartType.Pie, rect1, false);  
  7.   
  8. //set title  
  9. chart.ChartTitle.TextProperties.Text = "Sales by Quarter";  
  10. chart.ChartTitle.TextProperties.IsCentered = true;  
  11. chart.ChartTitle.Height = 30;  
  12. chart.HasTitle = true;  
  13.   
  14. //define sample data and assign the data to chart data  
  15. string[] quarters = new string[] { "1st Qtr""2nd Qtr""3rd Qtr""4th Qtr" };  
  16. int[] sales = new int[] { 210, 320, 180, 500 };  
  17. chart.ChartData[0, 0].Text = "Quarters";  
  18. chart.ChartData[0, 1].Text = "Sales";  
  19. for (int i = 0; i < quarters.Length; ++i)  
  20. {  
  21.     chart.ChartData[i + 1, 0].Value = quarters[i];  
  22.     chart.ChartData[i + 1, 1].Value = sales[i];  
  23. }  
  24.   
  25. //set series label  
  26. chart.Series.SeriesLabel = chart.ChartData["B1""B1"];  
  27.   
  28. //set category labels  
  29. chart.Categories.CategoryLabels = chart.ChartData["A2""A5"];  
  30.   
  31. //set series values  
  32. chart.Series[0].Values = chart.ChartData["B2""B5"];  
  33.   
  34. //ddd data points to series and fill each data point with different color  
  35. for (int i = 0; i < chart.Series[0].Values.Count; i++)  
  36. {  
  37.     ChartDataPoint cdp = new ChartDataPoint(chart.Series[0]);  
  38.     cdp.Index = i;  
  39.     chart.Series[0].DataPoints.Add(cdp);  
  40.   
  41. }  
  42. chart.Series[0].DataPoints[0].Fill.FillType = FillFormatType.Solid;  
  43. chart.Series[0].DataPoints[0].Fill.SolidColor.Color = Color.LightBlue;  
  44. chart.Series[0].DataPoints[1].Fill.FillType = FillFormatType.Solid;  
  45. chart.Series[0].DataPoints[1].Fill.SolidColor.Color = Color.DarkGray;  
  46. chart.Series[0].DataPoints[2].Fill.FillType = FillFormatType.Solid;  
  47. chart.Series[0].DataPoints[2].Fill.SolidColor.Color = Color.MediumPurple;  
  48. chart.Series[0].DataPoints[3].Fill.FillType = FillFormatType.Solid;  
  49. chart.Series[0].DataPoints[3].Fill.SolidColor.Color = Color.DarkOrange;  
  50.   
  51. //set labels to display label value and percentage value.  
  52. chart.Series[0].DataLabels.LabelValueVisible = true;  
  53. chart.Series[0].DataLabels.PercentValueVisible = true;  
  54.   
  55. //save to file  
  56. ppt.SaveToFile("PieChart.pptx", FileFormat.Pptx2010);  

 

Pie Chart
Figure 2. Pie Chart
 

How to create a combination chart

A combination chart is a chart that combines two or more chart types in a single chart.

  1. //create a PowerPoint document  
  2. Presentation presentation = new Presentation();  
  3.   
  4. //insert a column chart  
  5. RectangleF rect = new RectangleF(40, 100, 550, 320);  
  6. IChart chart = presentation.Slides[0].Shapes.AppendChart(ChartType.ColumnClustered, rect);  
  7.   
  8. //set chart title  
  9. chart.ChartTitle.TextProperties.Text = "Monthly Sales Report";  
  10. chart.ChartTitle.TextProperties.IsCentered = true;  
  11. chart.ChartTitle.Height = 30;  
  12. chart.HasTitle = true;  
  13.   
  14. //create a datatable  
  15. DataTable dataTable = new DataTable();  
  16. dataTable.Columns.Add(new DataColumn("Month", Type.GetType("System.String")));  
  17. dataTable.Columns.Add(new DataColumn("Sales", Type.GetType("System.Int32")));  
  18. dataTable.Columns.Add(new DataColumn("Growth rate", Type.GetType("System.Decimal")));  
  19. dataTable.Rows.Add("January", 200, 0.6);  
  20. dataTable.Rows.Add("February", 250, 0.8);  
  21. dataTable.Rows.Add("March", 300, 0.6);  
  22. dataTable.Rows.Add("April", 150, 0.2);  
  23. dataTable.Rows.Add("May", 200, 0.5);  
  24. dataTable.Rows.Add("June", 400, 0.9);  
  25.   
  26. //import data from datatable to chart data  
  27. for (int c = 0; c < dataTable.Columns.Count; c++)  
  28. {  
  29.     chart.ChartData[0, c].Text = dataTable.Columns[c].Caption;  
  30. }  
  31. for (int r = 0; r < dataTable.Rows.Count; r++)  
  32. {  
  33.     object[] datas = dataTable.Rows[r].ItemArray;  
  34.     for (int c = 0; c < datas.Length; c++)  
  35.     {  
  36.         chart.ChartData[r + 1, c].Value = datas[c];  
  37.   
  38.     }  
  39. }  
  40.   
  41. //set series labels  
  42. chart.Series.SeriesLabel = chart.ChartData["B1""C1"];  
  43.   
  44. //set categories labels      
  45. chart.Categories.CategoryLabels = chart.ChartData["A2""A7"];  
  46.   
  47. //assign data to series values  
  48. chart.Series[0].Values = chart.ChartData["B2""B7"];  
  49. chart.Series[1].Values = chart.ChartData["C2""C7"];  
  50.   
  51. //change the chart type of series 2 to line chart with markers  
  52. chart.Series[1].Type = ChartType.LineMarkers;  
  53.   
  54. //plot data of series 2 on the secondary axis  
  55. chart.Series[1].UseSecondAxis = true;  
  56.   
  57. //set the number format as percentage   
  58. chart.SecondaryValueAxis.NumberFormat = "0%";  
  59.   
  60. //hide grid lines of secondary axis  
  61. chart.SecondaryValueAxis.MajorGridTextLines.FillType = FillFormatType.None;  
  62.   
  63. //set overlap  
  64. chart.OverLap = -50;  
  65.   
  66. //set gap width  
  67. chart.GapWidth = 200;  
  68.   
  69. //save to file  
  70. presentation.SaveToFile("CombinationChart.pptx", FileFormat.Pptx2010);  

Combination Chart
Figure 3. Combination Chart