I’m attaching a excel sheet here I want a sample program to create a similar chart using aspose cells version 25.6.
It’s a nested stacked column chart with 3rd and 6th column without any color fill so that I can only see the labels. we can provide a white background to label with different text color.
Sample Stacked Column Chart.zip (11.7 KB)
@VaradS
Please try as the following codes:
public static void main(String[] args) throws Exception {
String dir = System.getProperty("user.dir") + "/";
// Load workbook
Workbook w = new Workbook(dir + "Sample Stacked Column Chart.xlsx");
// Add new worksheet
w.getWorksheets().add();
// Get the 3rd worksheet (index 2)
Worksheet worksheet = w.getWorksheets().get(2);
ChartCollection charts = worksheet.getCharts();
// Add stacked column chart
int chartIndex = charts.add(ChartType.COLUMN_STACKED, 0, 0, 20, 10);
Chart chart = charts.get(chartIndex);
// Add data series from range
chart.getNSeries().add("'1-DataSheet'!E6:E11", true);
chart.getNSeries().add("'1-DataSheet'!E12:E17", true);
// Show data labels
chart.getNSeries().get(0).getDataLabels().setValueShown(true);
chart.getNSeries().get(1).getDataLabels().setValueShown(true);
// Modify specific points
int[] cols = {2, 5};
for (int col : cols) {
// Set area formatting to none
chart.getNSeries().get(0).getPoints().get(col).getArea().setFormatting(FormattingType.NONE);
chart.getNSeries().get(1).getPoints().get(col).getArea().setFormatting(FormattingType.NONE);
// Set font color to red
chart.getNSeries().get(0).getPoints().get(col).getDataLabels().getFont().setColor(Color.RED);
chart.getNSeries().get(1).getPoints().get(col).getDataLabels().getFont().setColor(Color.RED);
}
// Save output file
w.save(dir + "dest.xlsx");
}
Hi @simon.zhao
Thank you for your inputs.
We have an additional requirement to show the floating data labels on the same height of the previous bar.
image.png (47.6 KB)
I have achieved this manually by repositioning the labels.
stackedverticallabels.zip (15.1 KB)
Sharing the file. Please refer sheet3.
One way is,
we could calculate the exact height of the previous bar labels and apply the same Y position to the floating data labels and x position can be the original.
Or
We can create the vertical bars for 3rd and 6th based on previous bar points and change the data labels with the original floating labels value.
Could you share the sample code for both approaches?
@VaradS
1, Update position as the previous column
ChartCalculateOptions chartCalculateOptions = new ChartCalculateOptions();
chartCalculateOptions.setUpdateAllPoints(true); // C# UpdateAllPoints → Java setUpdateAllPoints
chart.calculate(chartCalculateOptions); // C# Calculate() → Java calculate()
double y = chart.getNSeries().get(0).getPoints().get(1).getDataLabels().getYRatioToChart();
chart.getNSeries().get(0).getPoints().get(2).getDataLabels().setYRatioToChart(y);
double y1 = chart.getNSeries().get(1).getPoints().get(1).getDataLabels().getYRatioToChart();
chart.getNSeries().get(1).getPoints().get(2).getDataLabels().setYRatioToChart(y1);
2 Change text of data labels.
ChartCalculateOptions chartCalculateOptions = new ChartCalculateOptions();
chartCalculateOptions.setUpdateAllPoints(true); // C# UpdateAllPoints → Java setUpdateAllPoints
chart.calculate(chartCalculateOptions); // C# Calculate() → Java calculate()
String y = chart.getNSeries().get(0).getPoints().get(1).getDataLabels().getText();
chart.getNSeries().get(0).getPoints().get(2).getDataLabels().setText(y);
String y1 = chart.getNSeries().get(1).getPoints().get(1).getDataLabels().getText();
chart.getNSeries().get(1).getPoints().get(2).getDataLabels().setText(y1);