How to create a histogram chart in a Word document using C#?
Syncfusion® Essential® DocIO is a .NET Core Word library used to create, read, and edit Word documents programmatically without Microsoft Word or Interop dependencies. Using this library, you can create a histogram chart in a Word document in C#.
What is a histogram chart?
A histogram is a type of chart that shows how frequently values appear in a dataset. It helps you understand the distribution of data - how values are spread across different ranges. Unlike a bar chart (which compares categories), a histogram groups numerical data into intervals (called bins) and shows how many values fall into each bin.
Histogram chart in Word document
Steps to create a histogram chart in a Word document:
Step 1: Initialize the chart
Create a chart object by calling the paragraph.AppendChart(446, 270) method and specify the chart type to the OfficeChartType.Histogram enum value.
C#
// Create and append the chart to the paragraph. WChart chart = paragraph.AppendChart(446, 270); // Set chart type. chart.ChartType = OfficeChartType.Histogram;
Step 2: Assign data and chart elements
Add the basic elements like the chart title, data labels and legend.
- Assign data.
- ChartTitle of the chart object.
- Set DataLabels using the DefaultDataPoint.
- Set TRUE to the chart's HasLegend property to show the legend; else FALSE.
C#
// Set region of chart data. chart.DataRange = chart.ChartData[2, 1, 7, 2]; // Apply the chart elements. //Set a chart title. chart.ChartTitle = "Histogram Chart"; // Set DataLabels. IOfficeChartSerie series = chart.Series[0];
series.DataPoints.DefaultDataPoint.DataLabels.IsValue = true;
series.Name = "Score";
series.SerieFormat.CommonSerieOptions.GapWidth = 3; // Set legend. chart.HasLegend = true; chart.Legend.Position = OfficeLegendPosition.Bottom;
Applicable properties for histogram chart
Below is a list of other common properties applicable for pie chart:
- Note:
- Applying 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 histogram charts.
- As histogram chart is introduced in MS Word 2016, it can’t be viewed in MS Word 2013 and its earlier versions.
The following C# code sample shows the creation of a pie chart using the Word library:
C#
// Create a new Word document.
using (WordDocument document = new WordDocument())
{
IWSection sec = document.AddSection();
IWParagraph paragraph = sec.AddParagraph();// Create and append chart
WChart chart = paragraph.AppendChart(446, 270);
chart.ChartType = OfficeChartType.Histogram;// Set chart title
chart.ChartTitle = "Test Scores - Histogram Chart";
chart.ChartTitleArea.FontName = "Calibri";
chart.ChartTitleArea.Size = 14;// Set chart data
chart.ChartData.SetValue(1, 1, "Test Score");
int[] scores = { 20, 35, 40, 55, 80, 60, 61, 85, 80, 64, 80, 75 };
for (int i = 0; i < scores.Length; i++)
{
chart.ChartData.SetValue(i + 2, 1, scores[i]);
}
// Set region of chart data.
chart.DataRange = chart.ChartData[2, 1, 13, 1];// Set DataLabels.
IOfficeChartSerie series = chart.Series[0];
series.DataPoints.DefaultDataPoint.DataLabels.IsValue = true;
series.Name = "Score";
series.SerieFormat.CommonSerieOptions.GapWidth = 3;// Set legend.
chart.HasLegend = true;
chart.Legend.Position = OfficeLegendPosition.Bottom;//Set x-axis and y-axis title
chart.PrimaryCategoryAxis.Title = "Score Range";
chart.PrimaryValueAxis.Title = "Number Of Candidates";// Create a file stream.
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Sample.docx"), FileMode.Create, FileAccess.ReadWrite))
{
// Save the Word document to the file stream.
document.Save(outputFileStream, FormatType.Docx);
}
}
A complete working sample of how to create a histogram chart in a Word document in C# can be downloaded from GitHub.
Take a moment to peruse the documentation, where you can find basic Word document processing options along with features like mail merge, merge and split documents, find and replace text in the Word document, protect the Word documents, and most importantly, PDF and image conversions with code examples.
Explore more about the rich set of Syncfusion® Word Framework features and an online example to create a chart in a Word document.
See Also:
How to create a stacked bar cone chart in a Word document using C#?
Conclusion
I hope you enjoyed learning about how to create a histogram chart in a Word document using C#.
You can refer to our .NET Core Word library feature tour page to learn about its other groundbreaking features. You can also explore our .NET Core Word library examples to understand how to present and manipulate data.
For current customers, you can check out our .NET Core components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our .NET Core Word and other .NET Core components.
If you have any queries or require clarifications, please let us know in the comments below. You can also contact us through our Direct-Trac, support forums or feedback portal. We are always happy to assist you!