How to create PowerPoint waterfall chart in C#, VB.NET?
This article explains how to create a waterfall chart in PowerPoint using Presentation.
What is a 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 created using Presentation
To create a waterfall chart in PowerPoint using Presentation, you need to do the following steps.
Steps to create waterfall chart
- Initialize chart
Create a chart object by calling the slide.Charts.Add method and specify the chart type to OfficeChartType.WaterFall enum value.
//Create the chart IPresentationChart chart = slide.Charts.AddChart(50, 50, 500, 400); //Set chart type to WaterFall chart.ChartType = OfficeChartType.WaterFall;
- Assign data
Set a range of data to chart’s DataRange property. To plot the series values in column and categories in row, set chart’s IsSeriesInRows property to false.
//Set region of Chart data chart.DataRange = chart.ChartData[1,1,8,2]; //Set chart series in column for assigned data region chart.IsSeriesInRows = false;
- Apply basic chart elements
Add the basic elements like chart title, data labels and legend with the below list of properties.
- ChartTitle of chart object.
- Set DataLabels via DefaultDataPoint.
- Set TRUE to chart’s HasLegend property, to show the legend.
//Apply chart elements //Set Chart Title chart.ChartTitle = "Waterfall Chart"; //Set Legend chart.HasLegend = true; //Set Datalabels IOfficeChartSerie serie = chart.Series[0]; series.DataPoints[3].SetAsTotal = true; series.DataPoints[6].SetAsTotal = true; //Showing the connector lines between data points series.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;
Applicable properties of waterfall chart
Below is the list of other properties that is applicable for a waterfall chart.
1. GapWidth
3. SetAsTotal
NOTES:
1)Applying other properties unrelated to this chart might throw exception or the changes will not be reflected in the output document because those properties are not related to waterfall chart.
2)As waterfall chart is introduced in MS PowerPoint 2016, it can’t be viewed in MS PowerPoint 2013 and its earlier versions.
To know more about creating charts with various settings using Presentation, please refer the documentation.
The following C#/ VB.NET complete code snippet shows the creation of waterfall chart using Presentation.
C#
using Syncfusion.Presentation; using Syncfusion.OfficeChart; namespace ChartSample { class Program { static void Main(string[] args) { using (IPresentation pptxDoc = Presentation.Create()) { ISlide slide1 = pptxDoc.Slides.Add(SlideLayoutType.Blank); //Initialize chart IPresentationChart chart = slide1.Charts.AddChart(100, 10, 700, 500); chart.ChartType = OfficeChartType.WaterFall; //Assign data chart.DataRange = chart.ChartData[1,1,8,2]; chart.IsSeriesInRows = false; //Set data 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); //Apply chart elements //Set Chart Title chart.ChartTitle = "WaterFall Chart"; //Set Datalabels IOfficeChartSerie serie1 = chart.Series[0]; series.DataPoints[3].SetAsTotal = true; series.DataPoints[6].SetAsTotal = true; //Showing the connector lines between data points series.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; //Saving and closing the Presentation pptxDoc.Save("WaterFallChart.pptx"); pptxDoc.Close(); } } } }
VB
Imports Syncfusion.Presentation Imports Syncfusion.OfficeChart Namespace ChartSample Class Program Public Shared Sub Main(ByVal args As String()) 'Creates a PowerPoint instance Dim pptxDoc As IPresentation = Presentation.Create() Dim slide1 As ISlide = pptxDoc.Slides.Add(SlideLayoutType.Blank) 'Initialize chart Dim chart As IPresentationChart = slide1.Charts.AddChart(50, 50, 500, 400) chart.ChartType = OfficeChartType.WaterFall 'Assign data chart.DataRange = chart.ChartData[1, 1, 8, 2] chart.IsSeriesInRows = False 'Set data 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); 'Apply chart elements 'Set Chart Title chart.ChartTitle = "Waterfall Chart" 'Set Datalabels Dim serie1 As IOfficeChartSerie = chart.Series(0) series.DataPoints[3].SetAsTotal = True series.DataPoints[6].SetAsTotal = True 'Showing the connector lines between data points series.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 'Saving and closing the workbook pptxDoc.Save("WaterFallChart.pptx") pptxDoc.Close() End Sub End Class End Namespace