How to create Excel clustered bar chart in C#, VB.NET?
This article explains how to create a clustered bar chart in Excel using Syncfusion Excel (XlsIO) library.
What is a Clustered bar chart?
A clustered bar chart plots the data in discrete horizontal rectangles.
To create a clustered bar chart using XlsIO, you will need to do the following steps.
Clustered Bar Chart created using XlsIO
Steps to create the Clustered Bar Chart:
Step 1: Initialize chart
Create a chart object by calling the worksheet.Charts.Add method and specify the chart type to ExcelChartType.Bar_Clustered enum value.
C#
//Create the chart IChartShape chart = worksheet.Charts.Add(); //Set chart type to Bar_Clustered chart.ChartType = ExcelChartType.Bar_Clustered;
Step 2: 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.
C#
//Set region of Chart data chart.DataRange = worksheet["A1:C6"]; //Set chart series in column for assigned data region chart.IsSeriesInRows = false;
Step 3: Apply basic chart elements
Add the basic elements like chart title, data labels and legend with the below list of properties.
- ChartTitle of chart object.
- Set DataLabels via DefaultDataPoint.
- Set TRUE to chart’s HasLegend property, to show the legend.
C#
//Apply chart elements //Set Chart Title chart.ChartTitle = "Clustered Bar Chart"; //Set Datalabels IChartSerie serie1 = chart.Series[0]; serie1.DataPoints.DefaultDataPoint.DataLabels.IsValue = true; serie1.DataPoints.DefaultDataPoint.DataLabels.Position = ExcelDataLabelPosition.Center; //Set Legend chart.HasLegend = true; chart.Legend.Position = ExcelLegendPosition.Bottom;
Applicable Properties of Clustered Bar Chart
Below is the list of other common properties that is applicable for a column/bar chart.
- GapWidth (value should be between 0 and 500)
- Overlap (value should be between -100 and 100)
- IsVarycolorNote:
Applying other properties apart from the above list might 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 Syncfusion Excel (XlsIO) library, please refer the documentation.
The following C#/ VB.NET complete code snippet shows the creation of clustered bar chart using XlsIO.
C#
using System; using Syncfusion.XlsIO; using System.Drawing; using System.Reflection; using System.IO; namespace ChartSample { class Program { static void Main(string[] args) { using (ExcelEngine excelEngine = new ExcelEngine()) { IApplication application = excelEngine.Excel; application.DefaultVersion = ExcelVersion.Excel2016; //Open existing workbook with data entered Assembly assembly = typeof(Program).GetTypeInfo().Assembly; Stream fileStream = assembly.GetManifestResourceStream("ChartSample.InputTemplate.xlsx"); IWorkbook workbook = application.Workbooks.Open(fileStream); IWorksheet worksheet = workbook.Worksheets[0]; //Initialize chart IChartShape chart = worksheet.Charts.Add(); chart.ChartType = ExcelChartType.Bar_Clustered; //Assign data chart.DataRange = worksheet["A1:D6"]; chart.IsSeriesInRows = false; //Apply chart elements //Set Chart Title chart.ChartTitle = "Clustered Bar Chart"; //Set Datalabels IChartSerie serie1 = chart.Series[0]; IChartSerie serie2 = chart.Series[1]; IChartSerie 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 = ExcelDataLabelPosition.Center; serie2.DataPoints.DefaultDataPoint.DataLabels.Position = ExcelDataLabelPosition.Center; serie3.DataPoints.DefaultDataPoint.DataLabels.Position = ExcelDataLabelPosition.Center; //Set Legend chart.HasLegend = true; chart.Legend.Position = ExcelLegendPosition.Bottom; //Positioning the chart in the worksheet chart.TopRow = 8; chart.LeftColumn = 1; chart.BottomRow = 23; chart.RightColumn = 8; //Saving and closing the workbook Stream stream = File.Create("Output.xlsx"); workbook.SaveAs(stream); workbook.Close(); } } } }
VB.NET
Imports System Imports Syncfusion.XlsIO Imports System.Drawing Imports System.Reflection Imports System.IO Namespace ChartSample Class Program Public Shared Sub Main(ByVal args As String()) Using excelEngine As ExcelEngine = New ExcelEngine() Dim application As IApplication = excelEngine.Excel application.DefaultVersion = ExcelVersion.Excel2016 'Open existing workbook with data entered Dim assembly As Assembly = GetType(Program).GetTypeInfo().Assembly Dim fileStream As Stream = assembly.GetManifestResourceStream("ChartSample.InputTemplate.xlsx") Dim workbook As IWorkbook = application.Workbooks.Open(fileStream) Dim worksheet As IWorksheet = workbook.Worksheets(0) 'Initialize chart Dim chart As IChartShape = worksheet.Charts.Add() chart.ChartType = ExcelChartType.Bar_Clustered 'Assign data chart.DataRange = worksheet("A1:D6") chart.IsSeriesInRows = False 'Apply chart elements 'Set Chart Title chart.ChartTitle = "Clustered Bar Chart" 'Set Datalabels Dim serie1 As IChartSerie = chart.Series(0) Dim serie2 As IChartSerie = chart.Series(1) Dim serie3 As IChartSerie = 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 = ExcelDataLabelPosition.Center serie2.DataPoints.DefaultDataPoint.DataLabels.Position = ExcelDataLabelPosition.Center serie3.DataPoints.DefaultDataPoint.DataLabels.Position = ExcelDataLabelPosition.Center 'Set Legend chart.HasLegend = True chart.Legend.Position = ExcelLegendPosition.Bottom 'Positioning chart in the worksheet chart.TopRow = 8 chart.LeftColumn = 1 chart.BottomRow = 23 chart.RightColumn = 8 'Saving and closing the workbook Dim stream As Stream = File.Create("Output.xlsx") workbook.SaveAs(stream) workbook.Close() End Using End Sub End Class End Namespace