How to create waterfall chart in 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 waterfall chart in Word document in C#.
What is waterfall chart?
Waterfall chart helps to quickly understand the finances of business owners by viewing profit and loss statements. With a Waterfall chart, you can quickly illustrate the line items in your financial data and get a clear picture of how each item is impacting your bottom line.
Waterfall Chart in Word document
Steps to create waterfall chart in Word document:
Step 1: Initialize chart
Create a chart object by calling the paragraph.AppendChart(446,270) method and specify the chart type to the OfficeChartType.WaterFall enum value.
C#
//Create and append the chart to the paragraph. WChart chart = paragraph.AppendChart(446, 270); //Set chart type. chart.ChartType = OfficeChartType.WaterFall;
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[1, 1, 8, 2]; //Apply the chart elements. //Set a chart title. chart.ChartTitle = "Company Profit (in USD)"; //Set Datalabels. IOfficeChartSerie serie = chart.Series[0]; chart.Series[0].DataPoints[3].SetAsTotal = true; chart.Series[0].DataPoints[6].SetAsTotal = true; chart.Series[0].SerieFormat.ShowConnectorLines = true; serie.DataPoints.DefaultDataPoint.DataLabels.IsValue = true; chart.Series[0].DataPoints.DefaultDataPoint.DataLabels.Size = 8; chart.ChartArea.Border.LinePattern = OfficeChartLinePattern.None; //Set legend. chart.Legend.Position = OfficeLegendPosition.Right;
Applicable properties for waterfall chart
Below is a list of other common properties applicable for waterfall chart.
- GapWidth
- ShowConnectorLines
- SetAsTotalNote:
- 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 waterfall chart.
- As waterfall 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 waterfall chart using the Word library.
C#
//Creates a new Word document. using (WordDocument document = new WordDocument()) { //Adds section to the document. IWSection sec = document.AddSection(); //Adds paragraph to the section. IWParagraph paragraph = sec.AddParagraph(); //Creates and Appends chart to the paragraph. WChart chart = paragraph.AppendChart(446, 270); //Sets chart type. chart.ChartType = OfficeChartType.WaterFall; //Sets data range. chart.DataRange = chart.ChartData[1, 1, 8, 2]; chart.IsSeriesInRows = false; chart.ChartData.SetValue(2, 1, "Start"); chart.ChartData.SetValue(2, 2, 120000); chart.ChartData.SetValue(3, 1, "Product Revenue"); chart.ChartData.SetValue(3, 2, 570000); chart.ChartData.SetValue(4, 1, "Service Revenue"); chart.ChartData.SetValue(4, 2, 230000); chart.ChartData.SetValue(5, 1, "Positive Balance"); chart.ChartData.SetValue(5, 2, 920000); chart.ChartData.SetValue(6, 1, "Fixed Costs"); chart.ChartData.SetValue(6, 2, -345000); chart.ChartData.SetValue(7, 1, "Variable Costs"); chart.ChartData.SetValue(7, 2, -230000); chart.ChartData.SetValue(8, 1, "Total"); chart.ChartData.SetValue(8, 2, 345000); //Data point settings as total in chart. IOfficeChartSerie series = chart.Series[0]; chart.Series[0].DataPoints[3].SetAsTotal = true; chart.Series[0].DataPoints[6].SetAsTotal = true; //Showing the connector lines between data points. chart.Series[0].SerieFormat.ShowConnectorLines = true; //Set the chart title. chart.ChartTitle = "Company Profit (in USD)"; //Formatting data label and legend option. chart.Series[0].DataPoints.DefaultDataPoint.DataLabels.IsValue = true; chart.Series[0].DataPoints.DefaultDataPoint.DataLabels.Size = 8; chart.Legend.Position = OfficeLegendPosition.Right; chart.ChartArea.Border.LinePattern = OfficeChartLinePattern.None; //Creates file stream. using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Result.docx"), FileMode.Create, FileAccess.ReadWrite)) { //Saves the Word document to file stream. document.Save(outputFileStream, FormatType.Docx); } }
A complete working sample of how to create waterfall chart in 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 the features like mail merge, merge and split documents, find and replace text in the Word document, protect the Word documents, and most importantly, the 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 stacked bar cone chart in Word document using C#?
I hope you enjoyed learning about how to create waterfall chart in Word document using C#.
You can refer to our ASP.NET Core DocIo’s feature tour page to know about its other groundbreaking feature representations. You can also explore our ASP.NET Core DocIo documentation to understand how to present and manipulate data.
For current customers, you can check out ASP.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 ASP.NET Core DocIo and other ASP.NET Core components.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!