Hi Team,
In my Excel waterfall chart, I set the data labels to ‘Show Value = False’ for the series and for specific data points, but the labels are still displayed. How can I successfully remove these data labels.
Please find the Java program below:
public static void main(String[] args) {
try {
// Create a new workbook
Workbook workbook = new Workbook();
// Get the first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);
Cells cells = worksheet.getCells();
// Add headers
cells.get("A1").setValue("Category");
cells.get("B1").setValue("Value");
// Sample data for waterfall chart
String[] categories = {"Start", "Q1 Growth", "Q2 Loss", "Q3 Growth", "Q4 Loss"};
double[] values = {-15000000, -2500000, 1800000, 32000000, -9500000};
// Populate data with compact formatting
for (int i = 0; i < categories.length; i++) {
cells.get(i + 2, 0).setValue(categories[i]);
cells.get(i + 2, 1).setValue(values[i]);
Style style = cells.get(i + 2, 1).getStyle();
style.setCustom("#,##0,,.0\"B\";[>=1000000]#,##0,.0\"M\";#,##0");
cells.get(i + 2, 1).setStyle(style);
}
{
Style style = cells.get(1, 1).getStyle();
style.setCustom("#,##0,,.0\"B\";[>=1000000]#,##0,.0\"M\";#,##0");
cells.get(1, 1).setStyle(style);
}
// Create waterfall chart
int chartIndex = worksheet.getCharts().add(com.aspose.cells.ChartType.WATERFALL, 8, 2, 20, 12);
Chart chart = worksheet.getCharts().get(chartIndex);
// Set data range for chart
chart.getNSeries().add("B3:B7", true);
chart.getNSeries().setCategoryData("A3:A7");
// Configure chart appearance
chart.getTitle().setText("Simple Waterfall Chart");
chart.setShowLegend(false);
Series series = workbook.getWorksheets().get(0).getCharts().get(0).getNSeries().get(0);
chart.getValueAxis().getTickLabels().setNumberFormat("#,##0,,.0\"B\";[>=1000000]#,##0,.0\"M\";#,##0");
// Set number format for data labels first
for (int i = 0; i< series.getPoints().getCount();i++)
{
series.getPoints().get(i).getDataLabels().setNumberFormat("#,##0,,.0\"B\";[>=1000000]#,##0,.0\"M\";#,##0");
}
// Now disable data labels AFTER setting the format - this is critical!
for (int i = 0; i< series.getPoints().getCount();i++)
{
series.getPoints().get(i).getDataLabels().setShowValue(false);
series.getPoints().get(i).getDataLabels().setNumberFormatLinked(true);
}
// Also disable at series level
chart.getNSeries().get(0).getDataLabels().setShowValue(false);
// Save the workbook
workbook.save("simple_waterfall_chart.xlsx", SaveFormat.XLSX);
System.out.println("Simple waterfall chart created successfully: waterfall_chart.xlsx");
} catch (Exception e) {
e.printStackTrace();
}
}