How to create the PowerPoint area chart in C# and VB.NET
This article explains how to create an area chart in PowerPoint using Presentation library.
What is an area chart
Area charts visually represent the change in one or more quantities over time or other category data.
Area Chart Created using Presentation library
Steps to create area 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.Area enum value.
//Create the chart IPresentationChart chart = slide.Charts.AddChart(100, 10, 700, 500); //Set chart type to area chart.ChartType = OfficeChartType.Area;
- Assign data
Set a range of data from the worksheet to chart’s DataRange property. To plot the series values in column and categories in row, set chart’s IsSeriesInRows property to false.
//Set region of chart data chart.DataRange = chart.ChartData[1, 1, 6, 4]; //Set chart series in column for assigned data region chart.IsSeriesInRows = false;
- Apply basic chart elements
Add the basic elements like chart title and legend.
- ChartTitle of chart object.
- Set TRUE to chart’s HasLegend property, to show the legend.
//Apply chart elements //Set chart title chart.ChartTitle = "Area Chart"; //Set legend chart.HasLegend = true; chart.Legend.Position = OfficeLegendPosition.Bottom;
Common property applicable for area charts is
IsVarycolor (This property holds good for single series chart and is available in IOfficeChartFormat ).
NOTE: Applying other properties apart from the mentioned property might throw exception or the changes will not be reflected in the output document because those properties are not related to area chart.
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 area chart using Presentation library.
C#
using Syncfusion.Presentation; using Syncfusion.OfficeChart; using System.IO; namespace ChartSample { class Program { static void Main(string[] args) { //Create an instance for PowerPoint using (IPresentation pptxDoc = Presentation.Create()) { //Add a blank slide to 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.Area; //Assign data chart.DataRange = chart.ChartData[1, 1, 6, 4]; chart.IsSeriesInRows = false; //Set data to the chart RowIndex, columnIndex, and data SetChartData(chart); //Apply chart elements //Set chart title chart.ChartTitle = "Area Chart"; //Set legend chart.HasLegend = true; chart.Legend.Position = OfficeLegendPosition.Bottom; //Saving and closing the presentation Stream stream = File.Create("Output.pptx"); pptxDoc.Save(stream); } } /// <summary> /// Set the values for the chart /// </summary> /// <param name="chart">Represent the instance of the Presentation chart</param> private static void SetChartData(IPresentationChart chart) { chart.ChartData.SetValue(1, 1, "Fruits"); chart.ChartData.SetValue(2, 1, "Apples"); chart.ChartData.SetValue(3, 1, "Grapes"); chart.ChartData.SetValue(4, 1, "Bananas"); chart.ChartData.SetValue(5, 1, "Oranges"); chart.ChartData.SetValue(6, 1, "Melons"); chart.ChartData.SetValue(1, 2, "Joey"); chart.ChartData.SetValue(2, 2, 5); chart.ChartData.SetValue(3, 2, 4); chart.ChartData.SetValue(4, 2, 4); chart.ChartData.SetValue(5, 2, 2); chart.ChartData.SetValue(6, 2, 2); chart.ChartData.SetValue(1, 3, "Mathew"); chart.ChartData.SetValue(2, 3, 3); chart.ChartData.SetValue(3, 3, 5); chart.ChartData.SetValue(4, 3, 4); chart.ChartData.SetValue(5, 3, 1); chart.ChartData.SetValue(6, 3, 7); chart.ChartData.SetValue(1, 4, "Peter"); chart.ChartData.SetValue(2, 4, 2); chart.ChartData.SetValue(3, 4, 2); chart.ChartData.SetValue(4, 4, 3); chart.ChartData.SetValue(5, 4, 5); chart.ChartData.SetValue(6, 4, 6); } } }
VB
Imports Syncfusion.Presentation Imports Syncfusion.OfficeChart Imports System.IO Namespace ChartSample Class Program Public Shared Sub Main(ByVal args() As String) 'Create an instance for PowerPoint Dim pptxDoc As IPresentation = Presentation.Create() 'Add a blank slide to 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.Area 'Assign data chart.DataRange = chart.ChartData(1, 1, 6, 4) chart.IsSeriesInRows = False 'Set data to the chart- RowIndex, columnIndex and data Program.SetChartData(chart) 'Apply chart elements 'Set chart title chart.ChartTitle = "Area Chart" '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) 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(2, 1, "Apples") chart.ChartData.SetValue(3, 1, "Grapes") chart.ChartData.SetValue(4, 1, "Bananas") chart.ChartData.SetValue(5, 1, "Oranges") chart.ChartData.SetValue(6, 1, "Melons") chart.ChartData.SetValue(1, 2, "Joey") chart.ChartData.SetValue(2, 2, 5) chart.ChartData.SetValue(3, 2, 4) chart.ChartData.SetValue(4, 2, 4) chart.ChartData.SetValue(5, 2, 2) chart.ChartData.SetValue(6, 2, 2) chart.ChartData.SetValue(1, 3, "Mathew") chart.ChartData.SetValue(2, 3, 3) chart.ChartData.SetValue(3, 3, 5) chart.ChartData.SetValue(4, 3, 4) chart.ChartData.SetValue(5, 3, 1) chart.ChartData.SetValue(6, 3, 7) chart.ChartData.SetValue(1, 4, "Peter") chart.ChartData.SetValue(2, 4, 2) chart.ChartData.SetValue(3, 4, 2) chart.ChartData.SetValue(4, 4, 3) chart.ChartData.SetValue(5, 4, 5) chart.ChartData.SetValue(6, 4, 6) End Sub End Class End Namespace