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.

Hi @amjad.sahi
This issue seems to be still happening in 25.6 version.
Can you please confirm on the fix.
Thanks

@manavjaiswal,

I tested the following sample code with the template file (attached) with Aspose.Cells for Java v25.6 and it works fine fine and instantly. There is no delay on Chart.calculate() method.
e.g.,
Sample code:


       Workbook workbook = new Workbook("d:\\files\\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("d:\\files\\dest5.xlsx");
       System.out.println("done");

Please find attached the input and output XLSX files in the zipped archive for your reference.
files1.zip (26.1 KB)

Kindly ensure that you are using the latest version or fix. If the issue persists with Aspose.Cells for Java v25.6, we would appreciate it if you could share your sample runnable code and the template Excel file to help us reproduce the issue on our end. We will check your issue soon.

@amjad.sahi
We would like to understand what updates have been made on chart.calculate() to resolve this issue. Because we have the same scenario but with different data where this issue is still occurring and if you could share insights on some property or axis etc which has been updated recently, that would be helpful.
For e.g. if you have updated something related to tickmark labels position then we would try to find reasons on our end corresponding to that and it would also help us to create a sample standalone program to share with you guys as well because we are not able to create a sample program where the issue is reproducible yet.
Thanks

Hi @manavjaiswal
The issue is that some special data may cause an infinite loop during calculation, and we have optimized the calculation. If you encounter similar problems, please provide more files and we will further analyze them, thank you.