Java: Create a Waterfall Chart in Excel

A waterfall chart, also called a bridge chart or a cascade chart, is one of the most visually descriptive charts in Excel. It shows the cumulative effect of positive and negative contributions over a period of time, which is useful in many scenarios where quantitative analysis is required, such as visualizing profit and loss statements, showing budget changes in a project, or monitoring shop inventories. In this article, you will learn how to create a waterfall chart in Excel in Java using Spire.XLS for Java.

Install Spire.XLS for Java

First of all, you're required to add the Spire.Xls.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<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>14.11.0</version>
    </dependency>
</dependencies>
    

Create a Waterfall Chart in Excel in Java

Waterfall charts are ideal for analyzing financial statements. To create a waterfall chart, you could first add a chart to a specified worksheet using Worksheet.getCharts().add() method, and then set the chart type to Waterfall using Chart.setChartType(ExcelChartType.WaterFall) method. The following are the detailed steps.

  • Create a Workbook instance.
  • Load a sample Excel document using Workbook.loadFromFile() method.
  • Get a specified worksheet by its index using Workbook.getWorksheets().get() method.
  • Add a chart to the worksheet Worksheet.getCharts().add() method, and then set the chart type to waterfall using Chart.setChartType(ExcelChartType.WaterFall) method.
  • Set data range for the chart using Chart.setDataRange() method.
  • Set position and title of the chart.
  • Get a specified data series of the chart, and then set specific data points in the chart as totals or subtotals using ChartSerie.getDataPoints().get().setAsTotal() method.
  • Show the connector lines between data points using ChartSerie.getFormat().showConnectorLines(true) method.
  • Show data labels for data points, and set the legend position of the chart.
  • Save the result document using Workbook.saveToFile() method.
  • Java
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("data.xlsx");

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

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

        //Set data range for the chart
        chart.setDataRange(sheet.getRange().get("A2:B11"));

        //Set position of the chart
        chart.setLeftColumn(4);
        chart.setTopRow(2);
        chart.setRightColumn(15);
        chart.setBottomRow(23);

        //Set chart title
        chart.setChartTitle("Income Statement");

        //Set specific data points in the chart as totals or subtotals
        chart.getSeries().get(0).getDataPoints().get(2).setAsTotal(true);
        chart.getSeries().get(0).getDataPoints().get(7).setAsTotal(true);
        chart.getSeries().get(0).getDataPoints().get(9).setAsTotal(true);

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

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

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

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

Java: Create a Waterfall Chart in Excel

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.