Create a Waterfall Chart in Excel in Java

Andrew Wilson
2 min readApr 7, 2023

--

A waterfall chart, also called a cascade or bridge chart, is one of the most visually descriptive charts in Excel that can help you undertand how an initial value is affected by a series of positive and negative values. In this article, you will learn how to programmatically create a waterfall chart using Spire.XLS for Java.

Install the Library

Spire.XLS for Java is a feature-rich API used to process Excel files in Java applications. The following are two methods to install it.

Method 1: Download and unzip it, then add the Spire.Xls.jar file to your project as dependency.

Method 2: Directly add the jar dependency to maven project by adding the following configurations to the pom.xml.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.xls</artifactId>
<version>13.3.6</version>
</dependency>
</dependencies>

Sample Code

To ceate a waterfall chart in Excel, Spire.XLS for Java allows you to add a chart to a worksheet using Worksheet.getCharts().add() method, and then you can set the chart type as waterfall chart using Chart.setChartType(ExcelChartType.WaterFall) method. The complete sample code is shown below.

import com.spire.xls.*;

public class WaterfallChart {
public static void main(String []args){
//Create a Workbook object
Workbook workbook=new Workbook();

//Load a sample Excel document
workbook.loadFromFile("input.xlsx");

//Get the first worksheet
Worksheet sheet=workbook.getWorksheets().get(0);

//Add a waterfall chart to the worksheet
Chart officeChart=sheet.getCharts().add();
officeChart.setChartType(ExcelChartType.WaterFall);

//Set data range for the chart
officeChart.setDataRange(sheet.getRange().get("A1:B5"));

//Set totals for specific data points in the chart
officeChart.getSeries().get(0).getDataPoints().get(2).setAsTotal(true);
officeChart.getSeries().get(0).getDataPoints().get(4).setAsTotal(true);

//Show the connector lines between data points
officeChart.getSeries().get(0).getFormat().showConnectorLines(true);

//Set position of the chart
officeChart.setLeftColumn(1);
officeChart.setTopRow(7);
officeChart.setRightColumn(9);
officeChart.setBottomRow(26);

//Set chart title
officeChart.setChartTitle("Company Profit (in USD)");

//Show data labels for data points
officeChart.getSeries().get(0).getDataPoints().getDefaultDataPoint().getDataLabels().hasValue(true);
officeChart.getSeries().get(0).getDataPoints().getDefaultDataPoint().getDataLabels().setSize(8);

//Set the legend position of the chart
officeChart.getLegend().setPosition(LegendPositionType.Top);

//Save the result file
workbook.saveToFile("WaterfallChart.xlsx",FileFormat.Version2016);
}
}

--

--

Andrew Wilson

Explore C#, Java and Python solutions for processing Word/Excel/PowerPoint/PDF files.