How to create the PowerPoint clustered bar chart in C# and VB.NET
This article explains how to create a clustered bar chart in PowerPoint using Presentation library.
What is a clustered bar chart
A clustered bar chart plots the data in discrete horizontal rectangles.
Clustered Bar Chart Created using Presentation library
Steps to create the clustered bar chart using Presentation library
- Initialize chart
Create a chart object by calling the slide.Charts.AddChart method and specify the chart type to OfficeChartType.Bar_Clustered enum value.
//Create the chart IPresentationChart chart = slide.Charts.AddChart(100, 10, 700, 500); //Set chart type to Bar_Clustered chart.ChartType = OfficeChartType.Bar_Clustered;
- 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, data labels and legend with the following properties.
- ChartTitle of chart object.
- Set DataLabels via DefaultDataPoint.
- Set TRUE to chart’s HasLegend property to show the legend.
//Apply chart elements //Set chart title chart.ChartTitle = "Clustered Bar Chart"; //Set Datalabels IOfficeChartSerie serie1 = chart.Series[0]; serie1.DataPoints.DefaultDataPoint.DataLabels.IsValue = true; serie1.DataPoints.DefaultDataPoint.DataLabels.Position = OfficeDataLabelPosition.Center; //Set legend chart.HasLegend = true; chart.Legend.Position = OfficeLegendPosition.Bottom;
Applicable properties of clustered bar chart
Below is the list of other common properties that is applicable for a column/bar chart.
1. GapWidth (value should be between 0 and 500)
2. Overlap (value should be between -100 and 100)
3. IsVarycolor
NOTE: Applying other properties apart from the previous list may throw exception or the changes will not be reflected in the output document because, those properties are not related to column/bar chart.
To know 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 clustered bar 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.Bar_Clustered; //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 = "Clustered Bar Chart"; //Set Datalabels IOfficeChartSerie serie1 = chart.Series[0]; IOfficeChartSerie serie2 = chart.Series[1]; IOfficeChartSerie serie3 = chart.Series[2]; serie1.DataPoints.DefaultDataPoint.DataLabels.IsValue = true; serie2.DataPoints.DefaultDataPoint.DataLabels.IsValue = true; serie3.DataPoints.DefaultDataPoint.DataLabels.IsValue = true; serie1.DataPoints.DefaultDataPoint.DataLabels.Position = OfficeDataLabelPosition.Center; serie2.DataPoints.DefaultDataPoint.DataLabels.Position = OfficeDataLabelPosition.Center; serie3.DataPoints.DefaultDataPoint.DataLabels.Position = OfficeDataLabelPosition.Center; //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.Bar_Clustered '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 = "Clustered Bar Chart" 'Set Datalabels Dim serie1 As IOfficeChartSerie = chart.Series(0) Dim serie2 As IOfficeChartSerie = chart.Series(1) Dim serie3 As IOfficeChartSerie = chart.Series(2) serie1.DataPoints.DefaultDataPoint.DataLabels.IsValue = True serie2.DataPoints.DefaultDataPoint.DataLabels.IsValue = True serie3.DataPoints.DefaultDataPoint.DataLabels.IsValue = True serie1.DataPoints.DefaultDataPoint.DataLabels.Position = OfficeDataLabelPosition.Center serie2.DataPoints.DefaultDataPoint.DataLabels.Position = OfficeDataLabelPosition.Center serie3.DataPoints.DefaultDataPoint.DataLabels.Position = OfficeDataLabelPosition.Center '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