Articles in this section
Category / Section

How to create clustered column chart in Word document using C# and VB.NET

6 mins read

Syncfusion Essential DocIO is a .NET Word library used to create, read, and edit Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can create Clustered Column Chart in a Word document in C# and VB.NET.

What is a Clustered Column Chart?

A Clustered column chart plots the data in the discrete vertical rectangles.

Clustered column chart output

Clustered column chart created using the Word library (Essential DocIO)

Steps to create clustered column chart in a Word document

  1. Initialize chart

Create a chart object by calling the paragraph.AppendChart(446,270) method and specify the chart type to the OfficeChartType.Column_Clustered enum value.

//Create and Append chart to the paragraph.
WChart chart = paragraph.AppendChart(446, 270);
//Set chart type.
chart.ChartType = OfficeChartType.Column_Clustered;
  1. Assign data and chart elements

Add the basic elements like chart title, data labels, and legend.

  • ChartTitle of the chart object.
  • Set DataLabels using the DefaultDataPoint.
  • Add CategoryLabels and Values.
  • Set TRUE to the chart’s HasLegend property to show the legend, else False.
    //Apply the chart elements.
    //Set a chart title.
    chart.ChartTitle = "Sales Report in Clustered Column Chart";
     
    //Set legend.
    chart.HasLegend = true;
     
    //Set the 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 the Clustered Column Chart

 

Note:
  • Applying the properties apart from the mentioned list might throw an exception or the changes will not be reflected in the output document because those properties are not related to the column/bar chart.

 

The following C# and VB.NET complete code sample show the creation of clustered column chart using the Word library.

C#

using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.OfficeChart;
using System.IO;
 
namespace ChartSample
{
class Program
{
static void Main(string[] args)
{
//Load the template document.
WordDocument document = new WordDocument();
// Add a section to the document.
IWSection sec = document.AddSection();
//Add a paragraph to the section.
IWParagraph paragraph = sec.AddParagraph();
//Create and append the chart to the paragraph.
WChart chart = paragraph.AppendChart(446, 270);
chart.ChartType = OfficeChartType.Column_Clustered;
 
//Assign data.
AddChartData(chart);
chart.IsSeriesInRows = false;
 
//Apply the chart elements.
//Set a chart title.
chart.ChartTitle = "Sales Report in Clustered Column Chart";
 
//Set Datalabels.
IOfficeChartSerie serie1 = chart.Series.Add("Amount(in $)");
//Set the data range of chart series – start row, start column, end row and end column.
serie1.Values = chart.ChartData[2, 2, 6, 2];
 
IOfficeChartSerie serie2 = chart.Series.Add("Count");
//Set the data range of chart series start row, start column, end row and end column.
serie2.Values = chart.ChartData[2, 3, 6, 3];
 
//Set 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;
 
//Save and close the document.
document.Save("Sample.docx", FormatType.Docx);
document.Close();
}
 
/// <summary>
/// Set the values for the chart.
/// </summary>
private static void AddChartData(WChart 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 Syncfusion.DocIO
Imports Syncfusion.DocIO.DLS
Imports Syncfusion.OfficeChart
 
Namespace ChartSample
Class Program
Private Shared Sub Main(ByVal args As String())
'Load the template document.
Dim document As WordDocument = New WordDocument()
' Add a section to the document.
Dim sec As IWSection = document.AddSection()
'Add a paragraph to the section.
Dim paragraph As IWParagraph = sec.AddParagraph()
'Create and append the chart to the paragraph.
Dim chart As WChart = paragraph.AppendChart(446, 270)
'Set the chart type.
chart.ChartType = OfficeChartType.Column_Clustered;
'Assign data.
AddChartData(chart)
chart.IsSeriesInRows = False
 
'Apply the chart elements.
'Set a chart title.
chart.ChartTitle = "Sales Report in Clustered Column Chart"
 
'Set Datalabels.
Dim serie1 As IOfficeChartSerie = chart.Series.Add("Amount(in $)")
'Set the data range of chart series start row, start column, end row and end column.
serie1.Values = chart.ChartData(2, 2, 6, 2)
 
Dim serie2 As IOfficeChartSerie = chart.Series.Add("Count")
'Set the data range of chart series – start row, start column, end row and end column.
serie2.Values = chart.ChartData(2, 3, 6, 3)
 
'Set 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 Word document.
document.Save("Sample.docx", FormatType.Docx)
document.Close()
End Sub
 
''' <summary>
''' Set the values for the chart.
''' </summary>
Sub AddChartData(chart As WChart)
'Set the value for the 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

 

A complete working example of how to create clustered column chart in a Word document in C# can be downloaded from Create-clustered-column-chart-in-Word.zip.

To learn more about creating charts with various settings using the Syncfusion .NET Word library (Essential DocIO), please refer to the documentation.

An online example to create a chart in a Word document.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied