How to create sunburst chart in Word document using C# .NET Core?
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 sunburst chart in a Word document in C#.
What is a sunburst chart
A sunburst chart is a type of hierarchical chart that displays data in a circular format, showing relationships between categories and subcategories. It's especially useful for visualizing multi-level data, like organizational structures, file directories, or nested categories.
Sunburst chart in Word document
Steps to create a sunburst 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.SunBurst enum value.
C#
// Create and append the chart to the paragraph. WChart chart = paragraph.AppendChart(446, 270); // Set chart type. chart.ChartType = OfficeChartType.SunBurst;
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, 16, 4]; // Apply the chart elements. //cSet a chart title. chart.ChartTitle = "Sales by Annual - Sunburst 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 sunburst chart
Below is a list of other common properties applicable for sunburst 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 pie charts.
The following C# code sample shows the creation of a sunburst 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 Sunburst chart to the paragraph.
WChart chart = paragraph.AppendChart(446, 270);
chart.ChartType = OfficeChartType.SunBurst;
// Set chart title.
chart.ChartTitle = "Sales by Annual - Sunburst Chart";
// Set headers.
chart.ChartData.SetValue(1, 1, "Quarter");
chart.ChartData.SetValue(1, 2, "Month");
chart.ChartData.SetValue(1, 3, "Week");
chart.ChartData.SetValue(1, 4, "Sales");
// Add data rows.
string[,] data = {
{"1st", "Jan", "", "3.5"},
{"1st", "Feb", "Week 1", "1.2"},
{"1st", "Feb", "Week 2", "0.8"},
{"1st", "Feb", "Week 3", "0.6"},
{"1st", "Feb", "Week 4", "0.5"},
{"1st", "Mar", "", "1.7"},
{"2nd", "Apr", "", "1.1"},
{"2nd", "May", "", "0.8"},
{"2nd", "Jun", "", "0.8"},
{"3rd", "Jul", "", "1"},
{"3rd", "Aug", "", "0.7"},
{"3rd", "Sep", "", "0.9"},
{"4th", "Oct", "", "2"},
{"4th", "Nov", "", "2"},
{"4th", "Dec", "", "2"}
};
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, float.Parse(data[i, 3]));
}
// Set data range and hierarchy.
chart.DataRange = chart.ChartData[2, 1, data.GetLength(0) + 1, 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 sunburst 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 sunburst chart in Word document using C# .NET Core.
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!