Introduction
When it comes to PowerPoint presentation, charts are an import thing because charts can visualize a large amount of complex data, which is 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.
- using System;
- using Spire.Presentation;
- using System.Drawing;
- using Spire.Presentation.Charts;
- using System.Data;
- 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.
-
- Presentation presentation = new Presentation();
-
-
- RectangleF rect = new RectangleF(40, 100, 550, 320);
- IChart chart = presentation.Slides[0].Shapes.AppendChart(ChartType.ColumnClustered, rect);
-
-
- chart.ChartTitle.TextProperties.Text = "Male/Female Ratio Per Dept.";
- chart.ChartTitle.TextProperties.IsCentered = true;
- chart.ChartTitle.Height = 30;
- chart.HasTitle = true;
-
-
- string[,] data = new string[,]
- {
- {"Department","Male","Female" },
- {"Development","25","15"},
- {"Testing","5","10" },
- {"Sales","7","3" },
- {"Support","20","5" }
- };
-
-
- for (int i = 0; i < data.GetLength(0); i++)
- {
- for (int j = 0; j < data.GetLength(1); j++)
- {
- int number;
- bool result = Int32.TryParse(data[i, j], out number);
- if (result)
- {
- chart.ChartData[i, j].Value = number;
- }
- else
- {
- chart.ChartData[i, j].Value = data[i, j];
- }
- }
- }
-
-
- chart.Series.SeriesLabel = chart.ChartData["B1", "C1"];
-
-
- chart.Categories.CategoryLabels = chart.ChartData["A2", "A5"];
-
-
- chart.Series[0].Values = chart.ChartData["B2", "B5"];
- chart.Series[1].Values = chart.ChartData["C2", "C5"];
-
-
- chart.ChartStyle = ChartStyle.Style11;
-
-
- chart.OverLap = -50;
-
-
- chart.GapWidth = 200;
-
-
- presentation.SaveToFile("ColumnChart.pptx", FileFormat.Pptx2010);
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.
-
- Presentation ppt = new Presentation();
-
-
- RectangleF rect1 = new RectangleF(40, 100, 550, 320);
- IChart chart = ppt.Slides[0].Shapes.AppendChart(ChartType.Pie, rect1, false);
-
-
- chart.ChartTitle.TextProperties.Text = "Sales by Quarter";
- chart.ChartTitle.TextProperties.IsCentered = true;
- chart.ChartTitle.Height = 30;
- chart.HasTitle = true;
-
-
- string[] quarters = new string[] { "1st Qtr", "2nd Qtr", "3rd Qtr", "4th Qtr" };
- int[] sales = new int[] { 210, 320, 180, 500 };
- 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];
- }
-
-
- chart.Series.SeriesLabel = chart.ChartData["B1", "B1"];
-
-
- chart.Categories.CategoryLabels = chart.ChartData["A2", "A5"];
-
-
- chart.Series[0].Values = chart.ChartData["B2", "B5"];
-
-
- 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.LightBlue;
- chart.Series[0].DataPoints[1].Fill.FillType = FillFormatType.Solid;
- chart.Series[0].DataPoints[1].Fill.SolidColor.Color = Color.DarkGray;
- chart.Series[0].DataPoints[2].Fill.FillType = FillFormatType.Solid;
- chart.Series[0].DataPoints[2].Fill.SolidColor.Color = Color.MediumPurple;
- chart.Series[0].DataPoints[3].Fill.FillType = FillFormatType.Solid;
- chart.Series[0].DataPoints[3].Fill.SolidColor.Color = Color.DarkOrange;
-
-
- chart.Series[0].DataLabels.LabelValueVisible = true;
- chart.Series[0].DataLabels.PercentValueVisible = true;
-
-
- ppt.SaveToFile("PieChart.pptx", FileFormat.Pptx2010);
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.
-
- Presentation presentation = new Presentation();
-
-
- RectangleF rect = new RectangleF(40, 100, 550, 320);
- IChart chart = presentation.Slides[0].Shapes.AppendChart(ChartType.ColumnClustered, rect);
-
-
- chart.ChartTitle.TextProperties.Text = "Monthly Sales Report";
- chart.ChartTitle.TextProperties.IsCentered = true;
- chart.ChartTitle.Height = 30;
- chart.HasTitle = true;
-
-
- DataTable dataTable = new DataTable();
- dataTable.Columns.Add(new DataColumn("Month", Type.GetType("System.String")));
- dataTable.Columns.Add(new DataColumn("Sales", Type.GetType("System.Int32")));
- dataTable.Columns.Add(new DataColumn("Growth rate", Type.GetType("System.Decimal")));
- dataTable.Rows.Add("January", 200, 0.6);
- dataTable.Rows.Add("February", 250, 0.8);
- dataTable.Rows.Add("March", 300, 0.6);
- dataTable.Rows.Add("April", 150, 0.2);
- dataTable.Rows.Add("May", 200, 0.5);
- dataTable.Rows.Add("June", 400, 0.9);
-
-
- for (int c = 0; c < dataTable.Columns.Count; c++)
- {
- chart.ChartData[0, c].Text = dataTable.Columns[c].Caption;
- }
- for (int r = 0; r < dataTable.Rows.Count; r++)
- {
- object[] datas = dataTable.Rows[r].ItemArray;
- for (int c = 0; c < datas.Length; c++)
- {
- chart.ChartData[r + 1, c].Value = datas[c];
-
- }
- }
-
-
- chart.Series.SeriesLabel = chart.ChartData["B1", "C1"];
-
-
- chart.Categories.CategoryLabels = chart.ChartData["A2", "A7"];
-
-
- chart.Series[0].Values = chart.ChartData["B2", "B7"];
- chart.Series[1].Values = chart.ChartData["C2", "C7"];
-
-
- chart.Series[1].Type = ChartType.LineMarkers;
-
-
- chart.Series[1].UseSecondAxis = true;
-
-
- chart.SecondaryValueAxis.NumberFormat = "0%";
-
-
- chart.SecondaryValueAxis.MajorGridTextLines.FillType = FillFormatType.None;
-
-
- chart.OverLap = -50;
-
-
- chart.GapWidth = 200;
-
-
- presentation.SaveToFile("CombinationChart.pptx", FileFormat.Pptx2010);
Figure 3. Combination Chart