Create a Waterfall Chart with Legend Customization in Aspose.Slides for Java

Hi Team,

Could you please share the code snippet to render a waterfall chart for the below data using Aspose Slides Java version 25.6 ?

Period Savings
PrevYear -9
January 1
February 3
March 4
April -3
May 2
June 7
July 3
August 6
YTD 23

Output should be like

image.png (34.0 KB)

And I have below two requests

  1. I want to change the name of the legend from Increase/Decrease to Positive/Negative.
  2. Also, I would like to know if there is an option to hide the legend for Total alone without hiding the other 2 legends.

Sharing the ppt file for reference →
WaterfallChart.zip (37.3 KB)

Thanks,
Thilak Babu

@Thilakbabu

To create a waterfall chart using Aspose.Slides for Java, you can use the following code snippet. This example assumes you have the data you provided and want to render it as a waterfall chart.

import com.aspose.slides.*;

public class WaterfallChartExample {
    public static void main(String[] args) {
        // Create a presentation
        Presentation presentation = new Presentation();
        
        // Add a slide
        ISlide slide = presentation.getSlides().addEmptySlide(presentation.getLayoutSlides().get_Item(0));
        
        // Define data for the waterfall chart
        String[] categories = {"PrevYear", "January", "February", "March", "April", "May", "June", "July", "August", "YTD"};
        double[] values = {-9, 1, 3, 4, -3, 2, 7, 3, 6, 23};

        // Create a chart
        IChart chart = slide.getShapes().addChart(ChartType.Waterfall, 50, 50, 400, 300);
        
        // Set chart data
        IChartDataWorkbook dataWorkbook = chart.getChartData().getChartDataWorkbook();
        for (int i = 0; i < categories.length; i++) {
            dataWorkbook.getCell(0, i + 1, 0, categories[i]); // Categories
            dataWorkbook.getCell(0, i + 1, 1, values[i]); // Values
        }

        // Customize legend
        chart.getLegend().setText("Positive/Negative");
        
        // Note: You may need to customize the legend visibility for the total based on your specific requirements.

        // Save the presentation
        presentation.save("WaterfallChart.pptx", SaveFormat.Pptx);
    }
}

Notes:

  1. Legend Customization: The code snippet sets the legend text to “Positive/Negative”. You may need to adjust the legend visibility for the total based on your specific requirements, as the API may not directly support hiding only the total legend.
  2. Output: The output will be saved as “WaterfallChart.pptx”.

Ensure that you have included the Aspose.Slides library in your project to run this code.

@Thilakbabu,
Thank you for contacting free support. I need some time to prepare the code snippet for you. I will get back to you as soon as possible.

1 Like

@Thilakbabu,
Thank you for your patience. Unfortunately, using Aspose.Slides, I was unable to create a Waterfall chart like the one shown in the presentation you provided.

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): SLIDESJAVA-39731

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

@Thilakbabu,
Please try using the following code example:

Presentation presentation = new Presentation();
ISlide slide = presentation.getSlides().get_Item(0);

IChart chart = slide.getShapes().addChart(ChartType.Waterfall, 50, 50, 600, 400);

IChartDataWorkbook workbook = chart.getChartData().getChartDataWorkbook();
workbook.clear(0);

String[] categories = {"PrevYear", "January", "February", "March", "April", "May", "June", "July", "August", "YTD"};
double[] values = {-9, 1, 3, 4, -3, 2, 7, 3, 6, 23};

// populate data
IChartDataWorkbook dataWorkbook = chart.getChartData().getChartDataWorkbook();
for (int i = 0; i < categories.length; i++) {
    dataWorkbook.getCell(0, i + 1, 0, categories[i]); // Categories
    dataWorkbook.getCell(0, i + 1, 1, values[i]);     // Values
}
// set the chart data range
chart.getChartData().setRange("Sheet1!$A$1:$B$" + (categories.length + 1));

IChartSeries series = chart.getChartData().getSeries().get_Item(0);

// hide the connector lines
series.setShowConnectorLines(false);

// hide the series labels
series.getLabels().hide();

for (int i = 0; i < series.getDataPoints().size(); i++)
{
    // apply SetAsTotal and Color to the first and last data points
    if (i == 0 || i == series.getDataPoints().size() - 1)
    {
        series.getDataPoints().get_Item(i).setSetAsTotal(true);
        series.getDataPoints().get_Item(i).getFormat().getFill().setFillType(FillType.Solid);
        series.getDataPoints().get_Item(i).getFormat().getFill().getSolidFillColor().setColor(new Color(128, 128, 128));
        continue;
    }
    // apply Color to the decreased values
    if (series.getDataPoints().get_Item(i).getValue().toDouble() < 0)
    {
        series.getDataPoints().get_Item(i).setSetAsTotal(false);
        series.getDataPoints().get_Item(i).getFormat().getFill().setFillType(FillType.Solid);
        series.getDataPoints().get_Item(i).getFormat().getFill().getSolidFillColor().setColor(new Color(170, 33, 63));
    }
    else // apply Color to the increased values
    {
        series.getDataPoints().get_Item(i).setSetAsTotal(false);
        series.getDataPoints().get_Item(i).getFormat().getFill().setFillType(FillType.Solid);
        series.getDataPoints().get_Item(i).getFormat().getFill().getSolidFillColor().setColor(new Color(0, 138, 40));
    }
}

// hide the grid lines
chart.getAxes().getVerticalAxis().getMajorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.NoFill);

presentation.save("Waterfall.pptx", SaveFormat.Pptx);
presentation.dispose();

The result:
Waterfall.zip (31.5 KB)

We couldn’t do this in PowerPoint. Unfortunately, the legend element cannot be deleted or renamed. We can only implement what PowerPoint itself supports. If you have a presentation where this was done, please share it for analysis.