Need a Sample Code to Create a Excel Combination Chart in Java

Hi Team,

I have a combination chart of stacked bar in primary axis and clustered bar on the secondary axis.

image.png (77.9 KB)

please refer the attached excel file and provide the code to generate the same.

Combination-StackedAndClusteredBar.zip (11.7 KB)

Thanks.

@Thilakbabu,

Thanks for the template Excel file and screenshot.

We will work on to devise code snippet to accomplish your task. In the meantime, please see the document with example code on how to create a combo chart. Please refer to it and write your own code for your required chart.

@Thilakbabu
Please try the following codes:

 Workbook workbook = new Workbook(dir + "Combination-StackedAndClusteredBar.xlsx");
		 int index = workbook.getWorksheets().add();
		 ChartCollection charts = workbook.getWorksheets().get(index).getCharts();
		 index = charts.add(ChartType.BAR_STACKED, 0, 0, 33, 15);
		 Chart chart = charts.get(index);
		 chart.getNSeries().add("='1-DataSheet'!C6:D6", true);
		 
		 chart.getNSeries().get(chart.getNSeries().getCount() - 1).setPlotOnSecondAxis( true);
		 chart.getNSeries().get(chart.getNSeries().getCount() - 1).setType (ChartType.BAR);

		 chart.getNSeries().get(0).setGapWidth((short)25);
		
		  workbook.save(dir +"dest.xlsx");

Please check document :

Hi @simon.zhao / @amjad.sahi

Thanks for the sharing the code snippet.

I have the below issue -
Whenever I have the values for both stacked bar and clustered bar with same values and the chart.calculate() method execution takes forever to complete.

Please find the attached code snippet I have used as well as the updated input file to be used.

  public static void main(String[] args) throws Exception {
    Workbook workbook = new Workbook(dir + "Combination-StackedAndClusteredBar-Updated.xlsx");
    int index = workbook.getWorksheets().add();
    ChartCollection charts = workbook.getWorksheets().get(index).getCharts();
    index = charts.add(ChartType.BAR_STACKED, 0, 0, 33, 15);
    Chart chart = charts.get(index);
    chart.getWorksheet().setGridlinesVisible(false);
    
    chart.getPlotArea().getArea().getFillFormat().setFillType(FillType.NONE);
    chart.getNSeries().add("='1-DataSheet'!C6:D6", true);
    
    chart.getNSeries().get(0).getDataLabels().setShowValue(true);
    
    Series labelSeries = chart.getNSeries().get(chart.getNSeries().getCount() - 1);
    
    labelSeries.setPlotOnSecondAxis( true);
    labelSeries.setType (ChartType.BAR);
    
    labelSeries.getArea().getFillFormat().setFillType(FillType.NONE);
    labelSeries.getBorder().setFormattingType(ChartLineFormattingType.NONE);
    
    chart.getSecondCategoryAxis().setCrossType(CrossType.AUTOMATIC);

    chart.getNSeries().get(0).setGapWidth((short)25);
    
    labelSeries.setOverlap((byte) 0);
    labelSeries.setGapWidth((short) 150);
    labelSeries.getDataLabels().setShowValue(true);
    labelSeries.getDataLabels().setPosition(LabelPositionType.INSIDE_BASE);
    labelSeries.getLegendEntry().setDeleted(true);    
    
    chart.calculate();
    
    Axis valueAxis = chart.getValueAxis();
    
    valueAxis.getMajorGridLines().setVisible(false);
    
    valueAxis.setAutomaticMinValue(true);
    valueAxis.setAutomaticMaxValue(true);
    valueAxis.setAutomaticMajorUnit(true);
    valueAxis.setAutomaticMinorUnit(true);
    
    Axis categoryAxis = chart.getCategoryAxis();
    
    
    categoryAxis.setAutomaticMinValue(true);
    categoryAxis.setAutomaticMaxValue(true);
    categoryAxis.setAutomaticMajorUnit(true);
    categoryAxis.setAutomaticMinorUnit(true);
   
     workbook.save(dir +"dest5.xlsx");
     System.out.println("done");

  }

Updated input file → Combination-StackedAndClusteredBar-Updated.zip (11.7 KB)

If I remove the chart.calculate(), then the chart rendering completes quickly and able to get the excel file.

We have been asked by Aspose team to invoke this chart.Calculate() before configuring the axis bounds and units.

Could you please check and let us know whey chart.Calculate() method takes infinite time to complete and help us with the resolution?

Thanks,
Thilak

@Thilakbabu
By testing with sample code and file on the latest version v25.4, we can reproduce the issue. Chart.Calculate() method takes infinite time for stacked bar and clustered bar with same values.

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): CELLSJAVA-46353

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.

1 Like

@Thilakbabu,

This is to inform you that your issue (Ticket ID: “CELLSJAVA-46353”) has been resolved. The fix/enhancement will be included in an upcoming release (Aspose.Cells v25.5) that we plan to release in the first half of May 2025. You will be notified when the next version is released.

1 Like

Thanks for the update @amjad.sahi and @John.He!

@Thilakbabu,

You are welcome. We will keep you posted with updates on new version once it is available.

1 Like

The issues you have found earlier (filed as CELLSJAVA-46353) have been fixed in Aspose.Cells for Java 25.5.