How to create a treemap 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 treemap chart in a Word document in C#.
What is a treemap chart?
A treemap chart is a type of hierarchical chart that displays data as a set of nested rectangles. Each branch of the hierarchy is represented by a rectangle, which is then subdivided into smaller rectangles representing sub-branches.
Treemap chart in Word document
Steps to create a treemap 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.TreeMap enum value.
C#
// Create and append the chart to the paragraph. WChart chart = paragraph.AppendChart(446, 270); // Set chart type. chart.ChartType = OfficeChartType.TreeMap;
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, 13, 4]; // Apply the chart elements. //cSet a chart title. chart.ChartTitle = "Food Sales - Treemap Chart"; // Set DataLabels. IOfficeChartSerie serie = chart.Series[0]; serie.DataPoints.DefaultDataPoint.DataLabels.IsValue = true; // Set legend. chart.HasLegend = true; chart.Legend.Position = OfficeLegendPosition.Bottom;
Applicable properties for treemap chart
Below is a list of other common properties applicable for treemap 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 treemap charts.
The following C# code sample shows the creation of a treemap chart using the Word library:
C#
// Create a new Word document.
using (WordDocument document = new WordDocument())
{
// Add a section & a paragraph to the document.
IWSection section = document.AddSection();
IWParagraph paragraph = section.AddParagraph(); // Create and append TreeMap chart to the paragraph.
WChart chart = paragraph.AppendChart(446, 270);
chart.ChartType = OfficeChartType.TreeMap;
// Set chart title.
chart.ChartTitle = "Food Sales - Treemap Chart";
// Set headers.
chart.ChartData.SetValue(1, 1, "Meal");
chart.ChartData.SetValue(1, 2, "Category");
chart.ChartData.SetValue(1, 3, "Item");
chart.ChartData.SetValue(1, 4, "Sales");
// Add data rows.
string[,] data = {
{"Breakfast", "Beverage", "coffee", "20"},
{"Breakfast", "Beverage", "tea", "9"},
{"Breakfast", "Food", "waffles", "12"},
{"Breakfast", "Food", "pancakes", "35"},
{"Breakfast", "Food", "eggs", "24"},
{"Lunch", "Beverage", "coffee", "10"},
{"Lunch", "Beverage", "iced tea", "45"},
{"Lunch", "Food", "soup", "16"},
{"Lunch", "Food", "sandwich", "36"},
{"Lunch", "Food", "salad", "70"},
{"Lunch", "Food", "pie", "45"},
{"Lunch", "Food", "cookie", "25"}
};
for (int i = 0; i < data.GetLength(0); i++)
{
chart.ChartData.SetValue(i + 2, 1, data[i, 0]);
chart.ChartData.SetValue(i + 2, 2, data[i, 1]);
chart.ChartData.SetValue(i + 2, 3, data[i, 2]);
chart.ChartData.SetValue(i + 2, 4, int.Parse(data[i, 3]));
}
chart.DataRange = chart.ChartData[2, 1, 13, 4];
// Set DataLabels.
IOfficeChartSerie serie = chart.Series[0];
serie.DataPoints.DefaultDataPoint.DataLabels.IsValue = true;
// Set legend.
chart.HasLegend = true;
chart.Legend.Position = OfficeLegendPosition.Bottom;
// 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 treemap 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 treemap 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!