How to create the PowerPoint clustered column chart C# and VB.NET
This article explains how to create a clustered column chart in PowerPoint using Presentation library.
What is a clustered column chart
Clustered column chart plots the data in the discrete vertical rectangles.
Clustered Column Chart Created using Presentation library
Steps to create the clustered column chart
- Initialize chart
Create a chart object by calling the slide.Charts.AddChart method and specify the chart type to OfficeChartType.Column_Clustered enum value.
//Create the chart IPresentationChart chart = slide.Charts.AddChart(100, 10, 700, 500); //Set chart type to Column_Clustered chart.ChartType = OfficeChartType.Column_Clustered;
- 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 = " Sales Report in Clustered Column Chart "; //Set legend chart.HasLegend = true; //Set Datalabels, CategoryLabels, and Values IOfficeChartSerie serie = chart.Series.Add(); serie.CategoryLabels = chart.ChartData[2,1,11,1]; serie.Values = chart.ChartData[2, 2, 6, 2]; serie.Bubbles = chart.ChartData[2, 3, 11, 3]; serie.DataPoints.DefaultDataPoint.DataLabels.IsValue = true; serie.DataPoints.DefaultDataPoint.DataLabels.Position = OfficeDataLabelPosition.Bottom;
Applicable Properties of Clustered Column 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 above 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.
Applying other properties apart from the above 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#/ VB.NET complete code snippet shows the creation of clustered column 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.Column_Clustered; //Assign data AddChartData(chart); chart.IsSeriesInRows = false; //Apply chart elements //Set chart title chart.ChartTitle = "Sales Report in Clustered Column Chart"; //Set Datalabels IOfficeChartSerie serie1 = chart.Series.Add("Amount(in $)"); //Sets the data range of chart series – start row, start column, end row, end column serie1.Values = chart.ChartData[2, 2, 6, 2]; IOfficeChartSerie serie2 = chart.Series.Add("Count"); //Sets the data range of chart series start row, start column, end row, end column serie2.Values = chart.ChartData[2, 3, 6, 3]; //Sets the data range of the category axis chart.PrimaryCategoryAxis.CategoryLabels = chart.ChartData[2, 1, 6, 1]; serie1.DataPoints.DefaultDataPoint.DataLabels.IsValue = true; serie2.DataPoints.DefaultDataPoint.DataLabels.IsValue = true; serie1.DataPoints.DefaultDataPoint.DataLabels.Position = OfficeDataLabelPosition.Outside; serie2.DataPoints.DefaultDataPoint.DataLabels.Position = OfficeDataLabelPosition.Outside; //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 AddChartData(IPresentationChart chart) { //Set the value for chart data chart.ChartData.SetValue(1,1,"Items"); chart.ChartData.SetValue(1, 2, "Amount(in $)"); chart.ChartData.SetValue(1, 3, "Count"); chart.ChartData.SetValue(2, 1, "Beverages"); chart.ChartData.SetValue(2, 2, 2776); chart.ChartData.SetValue(2, 3, 925); chart.ChartData.SetValue(3, 1, "Condiments"); chart.ChartData.SetValue(3, 2, 1077); chart.ChartData.SetValue(3, 3, 378); chart.ChartData.SetValue(4, 1, "Confections"); chart.ChartData.SetValue(4, 2, 2287); chart.ChartData.SetValue(4, 3, 880); chart.ChartData.SetValue(5, 1, "Dairy Products"); chart.ChartData.SetValue(5, 2, 1368); chart.ChartData.SetValue(5, 3, 581); chart.ChartData.SetValue(6, 1, "Grains/Cereals"); chart.ChartData.SetValue(6, 2, 3325); chart.ChartData.SetValue(6, 3, 189); } } }
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.Column_Clustered 'Assign data AddChartData(chart) chart.IsSeriesInRows = False 'Apply chart elements 'Set chart title chart.ChartTitle = "Sales Report in Clustered Column Chart" 'Set Datalabels Dim serie1 As IOfficeChartSerie = chart.Series.Add("Amount(in $)") 'Sets the data range of chart series start row, start column, end row, end column serie1.Values = chart.ChartData(2, 2, 6, 2) Dim serie2 As IOfficeChartSerie = chart.Series.Add("Count") 'Sets the data range of chart series – start row, start column, end row, end column serie2.Values = chart.ChartData(2, 3, 6, 3) 'Sets the data range of the category axis chart.PrimaryCategoryAxis.CategoryLabels = chart.ChartData(2, 1, 6, 1) serie1.DataPoints.DefaultDataPoint.DataLabels.IsValue = True serie2.DataPoints.DefaultDataPoint.DataLabels.IsValue = True serie1.DataPoints.DefaultDataPoint.DataLabels.Position = OfficeDataLabelPosition.Outside serie2.DataPoints.DefaultDataPoint.DataLabels.Position = OfficeDataLabelPosition.Outside '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> Sub AddChartData(chart As IPresentationChart) 'Set the value for chart data chart.ChartData.SetValue(1, 1, "Items") chart.ChartData.SetValue(1, 2, "Amount(in $)") chart.ChartData.SetValue(1, 3, "Count") chart.ChartData.SetValue(2, 1, "Beverages") chart.ChartData.SetValue(2, 2, 2776) chart.ChartData.SetValue(2, 3, 925) chart.ChartData.SetValue(3, 1, "Condiments") chart.ChartData.SetValue(3, 2, 1077) chart.ChartData.SetValue(3, 3, 378) chart.ChartData.SetValue(4, 1, "Confections") chart.ChartData.SetValue(4, 2, 2287) chart.ChartData.SetValue(4, 3, 880) chart.ChartData.SetValue(5, 1, "Dairy Products") chart.ChartData.SetValue(5, 2, 1368) chart.ChartData.SetValue(5, 3, 581) chart.ChartData.SetValue(6, 1, "Grains/Cereals") chart.ChartData.SetValue(6, 2, 3325) chart.ChartData.SetValue(6, 3, 189) End Sub End Class End Namespace