Category / Section
How to create the PowerPoint line chart in C# and VB.NET
3 mins read
This article explains how to create a line chart in PowerPoint using Presentation library.
Line chart created using Presentation library
Steps to create line chart in PowerPoint using Presentation library:
- Initialize chart
Create a chart object by calling the slide.Charts.AddChart method and specify the chart type to OfficeChartType.Line enum value.
//Create the chart IPresentationChart chart = slide.Charts.AddChart(100, 10, 700, 500); //Set chart type to line chart.ChartType = OfficeChartType.Line;
- 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 = " Line Chart "; //Set legend chart.HasLegend = true; //Set Datalabels, CategoryLabels, and Values IOfficeChartSerie series1 = chart.Series.Add("Joey"); series1.Values = chart.ChartData[2, 2, 6, 2]; series1.SerieType = OfficeChartType.Line; IOfficeChartSerie series2 = chart.Series.Add("Mathew"); series2.Values = chart.ChartData[2, 3, 6, 3]; series2.SerieType = OfficeChartType.Line; IOfficeChartSerie series3 = chart.Series.Add("Peter"); series3.Values = chart.ChartData[2, 4, 6, 4]; series3.SerieType = OfficeChartType.Line;
To learn more about creating charts with various settings using Presentation library, please refer the documentation.
The following C# and VB.NET complete code snippet shows the creation of line chart using Presentation library.
C#
using Syncfusion.OfficeChart; using Syncfusion.Presentation; using System.IO; namespace ChartSample { class Program { static void Main(string[] args) { //Creates an instance of the IPresentation using (IPresentation pptxDoc = Presentation.Create()) { //Adds a blank slide to the 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.Line; //Assign data AddChartData(chart); chart.IsSeriesInRows = false; //Apply chart elements //Set chart title chart.ChartTitle = "Line Chart"; //Set Datalabels IOfficeChartSerie series1 = chart.Series.Add("Joey")); series1.Values = chart.ChartData[2, 2, 6, 2]; series1.SerieType = OfficeChartType.Line; IOfficeChartSerie series2 = chart.Series.Add("Mathew"); series2.Values = chart.ChartData[2, 3, 6, 3]; series2.SerieType = OfficeChartType.Line; IOfficeChartSerie series3 = chart.Series.Add("Peter"); series3.Values = chart.ChartData[2, 4, 6, 4]; series3.SerieType = OfficeChartType.Line; //Set legend chart.HasLegend = true; chart.Legend.Position = OfficeLegendPosition.Bottom; //Saving and closing the presentation Stream stream = File.Create("Output.pptx"); pptxDoc.Save(stream); pptxDoc.Close(); } } /// <summary> /// Set the values for the chart /// </summary> /// <param name="chart">Represent the instance of the Presentation chart</param> private static void AddChartData(IPresentationChart chart) { chart.ChartData.SetValue(1, 1, "Fruits"); chart.ChartData.SetValue(1, 2, "Joey"); chart.ChartData.SetValue(1, 3, "Mathew"); chart.ChartData.SetValue(1, 4, "Peter"); chart.ChartData.SetValue(2, 1, "Apples"); chart.ChartData.SetValue(2, 2, 5); chart.ChartData.SetValue(2, 3, 3); chart.ChartData.SetValue(2, 4, 2); chart.ChartData.SetValue(3, 1, "Grapes"); chart.ChartData.SetValue(3, 2, 4); chart.ChartData.SetValue(3, 3, 5); chart.ChartData.SetValue(3, 4, 2); chart.ChartData.SetValue(4, 1, "Bananas"); chart.ChartData.SetValue(4, 2, 4); chart.ChartData.SetValue(4, 3, 4); chart.ChartData.SetValue(4, 4, 3); chart.ChartData.SetValue(5, 1, "Oranges"); chart.ChartData.SetValue(5, 2, 2); chart.ChartData.SetValue(5, 3, 1); chart.ChartData.SetValue(5, 4, 5); chart.ChartData.SetValue(6, 1, "Melons"); chart.ChartData.SetValue(6, 2, 2); chart.ChartData.SetValue(6, 3, 7); chart.ChartData.SetValue(6, 4, 6); } } }
VB
Imports System.IO Imports Syncfusion.OfficeChart Imports Syncfusion.Presentation Namespace ChartSample Class Program Public Shared Sub Main(ByVal args As String()) 'Creates an instance of the IPresentation Dim pptxDoc As IPresentation = Presentation.Create() 'Adds a blank slide to the 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.Line 'Assign data SetChartData (chart) chart.IsSeriesInRows = False 'Apply chart elements 'Set chart title chart.ChartTitle = "Line Chart" 'Set Datalabels Dim series1 As IOfficeChartSerie = chart.Series.Add("Joey") series1.Values = chart.ChartData(2, 2, 6, 2) series1.SerieType = OfficeChartType.Line Dim series2 As IOfficeChartSerie = chart.Series.Add("Mathew") series2.Values = chart.ChartData(2, 3, 6, 3) series2.SerieType = OfficeChartType.Line Dim series3 As IOfficeChartSerie = chart.Series.Add("Peter") series3.Values = chart.ChartData(2, 4, 6, 4) series3.SerieType = OfficeChartType.Line 'Set legend chart.HasLegend = True chart.Legend.Position = OfficeLegendPosition.Bottom 'Saving and closing the presentation Dim stream As Stream = File.Create(”Output.pptx”) pptxDoc.Save(stream) pptxDoc.Close() 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) chart.ChartData.SetValue(1, 1, "Fruits") chart.ChartData.SetValue(1, 2, "Joey") chart.ChartData.SetValue(1, 3, "Mathew") chart.ChartData.SetValue(1, 4, "Peter") chart.ChartData.SetValue(2, 1, "Apples") chart.ChartData.SetValue(2, 2, 5) chart.ChartData.SetValue(2, 3, 3) chart.ChartData.SetValue(2, 4, 2) chart.ChartData.SetValue(3, 1, "Grapes") chart.ChartData.SetValue(3, 2, 4) chart.ChartData.SetValue(3, 3, 5) chart.ChartData.SetValue(3, 4, 2) chart.ChartData.SetValue(4, 1, "Bananas") chart.ChartData.SetValue(4, 2, 4) chart.ChartData.SetValue(4, 3, 4) chart.ChartData.SetValue(4, 4, 3) chart.ChartData.SetValue(5, 1, "Oranges") chart.ChartData.SetValue(5, 2, 2) chart.ChartData.SetValue(5, 3, 1) chart.ChartData.SetValue(5, 4, 5) chart.ChartData.SetValue(6, 1, "Melons") chart.ChartData.SetValue(6, 2, 2) chart.ChartData.SetValue(6, 3, 7) chart.ChartData.SetValue(6, 4, 6) End Sub End Class End Namespace