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

Hi @andrey.potapov ,

Thanks for your inputs.

But we have a requirement to have the clustered column chart in secondary axis.

We have a similar implementation using Aspose cells as well in Microsoft Excel.

image.png (64.8 KB)

Attaching the excel file for your reference

Labels-chart.zip (11.7 KB)

So what I mean here is, using Aspose Cell, we are able to see the legends symbol for Line chart properly but only for Aspose Slides, the line chart symbol is being missed.

Could you please help us resolve this issue in Aspose slides even with using the “plot on secondary axis” ?

Thanks,
Thilak

@Thilakbabu,
I need some time to answer your question. I will get back to you as soon as possible.

1 Like

@Thilakbabu,
Thank you for your patience.

In this case, you should use the first solution from the post above. You only need to remove the following line of code:

series.getRelatedLegendEntry().setHide(true);

@andrey.potapov ,
Thanks for your reply.
Removing this line causes the fact3 series to remain visible in the legend, which is not the desired behavior.
We expect it to function the same way as it does in Excel. (provided above by @Thilakbabu )

@VaradS,
If you want to hide the second series (Fact 2), you can add that code line to the addLineWithMarkersChart method.

Hi @andrey.potapov

Thanks for the reply!

The requirement is to have Stacked bar and Line chart in primary axis and invisible clustered column chart in the secondary axis.

Also, we want the invisible clustered column chart legend to be hidden.

While trying to do this, we are seeing Line chart’s legend symbol is missing. But this works perfectly fine in Excel… only in PPT, this does not work.

Please help us resolve this issue without altering our requirement.

cc @VaradS

Thanks,
Thilak Babu

@Thilakbabu,
Thank you for the clarification. I am working on the issue and will get back to you soon.

1 Like

@Thilakbabu,
I’ve carefully reviewed and executed the first code example from the post above, and the result aligns with your requirements:

  • The Stacked Column chart and Line chart are on the primary axis.
  • The Clustered Column chart is on the secondary axis.
  • The legend for the Clustered Column chart entry is invisible.

Code example:

Presentation presentation = new Presentation();
presentation.getSlideSize().setSize(SlideSizeType.Widescreen, SlideSizeScaleType.EnsureFit);

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

IChart chart = addStackedColumnChart(slide);
addClusteredColumnChart(chart);
addLineWithMarkersChart(chart);

configureHorizontalAxis(chart.getAxes().getHorizontalAxis());
configureVerticalAxis(chart.getAxes().getVerticalAxis());
configureSecondaryHorizontalAxis(chart.getAxes().getSecondaryHorizontalAxis());
configureSecondaryVerticalAxis(chart.getAxes().getSecondaryVerticalAxis());

configurePlotArea(chart.getPlotArea());
configureChartLegend(chart.getLegend());

chart.getChartData().setRange("Sheet1!$A$1:$D$18");
hideChartZeroValues(chart);

presentation.save("output.pptx", SaveFormat.Pptx);
presentation.dispose();
static IChart addStackedColumnChart(ISlide slide) {
    IChart chart = slide.getShapes().addChart(ChartType.StackedColumn, 28.5f, 101f, 902.5f, 355f);

    chart.getChartData().getSeries().clear();
    chart.getChartData().getCategories().clear();

    IChartDataWorkbook workbook = chart.getChartData().getChartDataWorkbook();
    final int worksheetIndex = 0;
    final int seriesColumnIndex = 1;

    chart.getChartData().getCategories().add(workbook.getCell(worksheetIndex, 1, 0, "ABC"));
    chart.getChartData().getCategories().add(workbook.getCell(worksheetIndex, 2, 0, "DEF"));
    chart.getChartData().getCategories().add(workbook.getCell(worksheetIndex, 3, 0, "XYZ"));
    chart.getChartData().getCategories().add(workbook.getCell(worksheetIndex, 4, 0, "RST"));
    chart.getChartData().getCategories().add(workbook.getCell(worksheetIndex, 5, 0, "MNO"));

    IChartSeries series = chart.getChartData().getSeries().add(
            workbook.getCell(worksheetIndex, 0, seriesColumnIndex, "Fact 1"), ChartType.StackedColumn);

    series.getDataPoints().addDataPointForBarSeries(workbook.getCell(worksheetIndex, 1, seriesColumnIndex, 59.4));
    series.getDataPoints().addDataPointForBarSeries(workbook.getCell(worksheetIndex, 2, seriesColumnIndex, 16.2));
    series.getDataPoints().addDataPointForBarSeries(workbook.getCell(worksheetIndex, 3, seriesColumnIndex, 6.2));
    series.getDataPoints().addDataPointForBarSeries(workbook.getCell(worksheetIndex, 4, seriesColumnIndex, 2.6));
    series.getDataPoints().addDataPointForBarSeries(workbook.getCell(worksheetIndex, 5, seriesColumnIndex, 2.3));

    setSeriesDataFormat(workbook, worksheetIndex, seriesColumnIndex, series.getDataPoints().size());

    series.getFormat().getFill().setFillType(FillType.Solid);
    series.getFormat().getFill().getSolidFillColor().setColor(new Color(103, 88, 149));

    series.getParentSeriesGroup().setOverlap((byte) 100);
    series.getParentSeriesGroup().setGapWidth(40);

    series.getLabels().getDefaultDataLabelFormat().setShowValue(true);
    series.getLabels().getDefaultDataLabelFormat().getFormat().getFill().setFillType(FillType.Solid);
    series.getLabels().getDefaultDataLabelFormat().getFormat().getFill().getSolidFillColor().setColor(Color.WHITE);
    series.getLabels().getDefaultDataLabelFormat().getFormat().getLine().setWidth(0.75);
    series.getLabels().getDefaultDataLabelFormat().getFormat().getLine().getFillFormat().setFillType(FillType.Solid);
    series.getLabels().getDefaultDataLabelFormat().getFormat().getLine().getFillFormat().getSolidFillColor().setColor(new Color(89, 89, 89));
    series.getLabels().getDefaultDataLabelFormat().getTextFormat().getPortionFormat().setFontHeight(10);
    series.getLabels().getDefaultDataLabelFormat().getTextFormat().getPortionFormat().setLatinFont(new FontData("Arial"));

    series.setOrder(0);

    return chart;
}

static void addLineWithMarkersChart(IChart chart) {
    IChartDataWorkbook workbook = chart.getChartData().getChartDataWorkbook();
    final int worksheetIndex = 0;
    final int seriesColumnIndex = 3;

    IChartSeries series = chart.getChartData().getSeries().add(
            workbook.getCell(worksheetIndex, 0, seriesColumnIndex, "Fact 2"), ChartType.LineWithMarkers);

    series.getDataPoints().addDataPointForLineSeries(workbook.getCell(worksheetIndex, 1, seriesColumnIndex, 59.4));
    series.getDataPoints().addDataPointForLineSeries(workbook.getCell(worksheetIndex, 2, seriesColumnIndex, 75.6));
    series.getDataPoints().addDataPointForLineSeries(workbook.getCell(worksheetIndex, 3, seriesColumnIndex, 81.9));
    series.getDataPoints().addDataPointForLineSeries(workbook.getCell(worksheetIndex, 4, seriesColumnIndex, 84.4));
    series.getDataPoints().addDataPointForLineSeries(workbook.getCell(worksheetIndex, 5, seriesColumnIndex, 86.7));

    setSeriesDataFormat(workbook, worksheetIndex, seriesColumnIndex, series.getDataPoints().size());

    series.getFormat().getLine().getFillFormat().setFillType(FillType.Solid);
    series.getFormat().getLine().getFillFormat().getSolidFillColor().setColor(new Color(70, 151, 226));

    series.getMarker().setSymbol(MarkerStyleType.Circle);
    series.getMarker().setSize(5);
    series.getMarker().getFormat().getFill().setFillType(FillType.Solid);
    series.getMarker().getFormat().getFill().getSolidFillColor().setColor(new Color(70, 151, 226));
    series.getMarker().getFormat().getLine().getFillFormat().setFillType(FillType.Solid);
    series.getMarker().getFormat().getLine().getFillFormat().getSolidFillColor().setColor(new Color(70, 151, 226));

    series.getLabels().getDefaultDataLabelFormat().setShowValue(true);
    series.getLabels().getDefaultDataLabelFormat().getFormat().getFill().setFillType(FillType.Solid);
    series.getLabels().getDefaultDataLabelFormat().getFormat().getFill().getSolidFillColor().setColor(Color.WHITE);
    series.getLabels().getDefaultDataLabelFormat().getFormat().getLine().setWidth(0.75);
    series.getLabels().getDefaultDataLabelFormat().getFormat().getLine().getFillFormat().setFillType(FillType.Solid);
    series.getLabels().getDefaultDataLabelFormat().getFormat().getLine().getFillFormat().getSolidFillColor().setColor(new Color(89, 89, 89));
    series.getLabels().getDefaultDataLabelFormat().getTextFormat().getPortionFormat().setFontHeight(10);
    series.getLabels().getDefaultDataLabelFormat().getTextFormat().getPortionFormat().setLatinFont(new FontData("Arial"));

    series.setOrder(1);
}

static void addClusteredColumnChart(IChart chart) {
    IChartDataWorkbook workbook = chart.getChartData().getChartDataWorkbook();
    final int worksheetIndex = 0;
    final int seriesColumnIndex = 2;

    IChartSeries series = chart.getChartData().getSeries().add(
            workbook.getCell(worksheetIndex, 0, seriesColumnIndex, "Fact 3"), ChartType.ClusteredColumn);

    series.setPlotOnSecondAxis(true);

    series.getDataPoints().addDataPointForBarSeries(workbook.getCell(worksheetIndex, 1, seriesColumnIndex, 59.4));
    series.getDataPoints().addDataPointForBarSeries(workbook.getCell(worksheetIndex, 2, seriesColumnIndex, 52.8));
    series.getDataPoints().addDataPointForBarSeries(workbook.getCell(worksheetIndex, 3, seriesColumnIndex, 41.6));
    series.getDataPoints().addDataPointForBarSeries(workbook.getCell(worksheetIndex, 4, seriesColumnIndex, 29.0));
    series.getDataPoints().addDataPointForBarSeries(workbook.getCell(worksheetIndex, 5, seriesColumnIndex, 24.6));

    setSeriesDataFormat(workbook, worksheetIndex, seriesColumnIndex, series.getDataPoints().size());

    series.getFormat().getFill().setFillType(FillType.NoFill);
    series.getFormat().getLine().getFillFormat().setFillType(FillType.NoFill);

    series.getParentSeriesGroup().setOverlap((byte) -100);
    series.getParentSeriesGroup().setGapWidth(320);

    series.getLabels().getDefaultDataLabelFormat().setShowValue(true);
    series.getLabels().getDefaultDataLabelFormat().getFormat().getLine().setWidth(0.75);
    series.getLabels().getDefaultDataLabelFormat().getFormat().getLine().getFillFormat().setFillType(FillType.Solid);
    series.getLabels().getDefaultDataLabelFormat().getFormat().getLine().getFillFormat().getSolidFillColor().setColor(new Color(211, 211, 211));
    series.getLabels().getDefaultDataLabelFormat().getTextFormat().getPortionFormat().getFillFormat().setFillType(FillType.Solid);
    series.getLabels().getDefaultDataLabelFormat().getTextFormat().getPortionFormat().getFillFormat().getSolidFillColor().setColor(new Color(19, 130, 56));
    series.getLabels().getDefaultDataLabelFormat().getTextFormat().getPortionFormat().setFontHeight(10);
    series.getLabels().getDefaultDataLabelFormat().getTextFormat().getPortionFormat().setLatinFont(new FontData("Arial"));
    series.getLabels().getDefaultDataLabelFormat().setPosition(LegendDataLabelPosition.InsideBase);

    series.getRelatedLegendEntry().setHide(true);

    series.setOrder(2);
}

static void configureHorizontalAxis(IAxis axis) {
    axis.getFormat().getLine().setWidth(2);
    axis.getFormat().getLine().getFillFormat().setFillType(FillType.Solid);
    axis.getFormat().getLine().getFillFormat().getSolidFillColor().setColor(Color.BLACK);

    axis.getTextFormat().getPortionFormat().getFillFormat().setFillType(FillType.Solid);
    axis.getTextFormat().getPortionFormat().getFillFormat().getSolidFillColor().setColor(new Color(21, 25, 33));
    axis.getTextFormat().getPortionFormat().setFontHeight(10);
    axis.getTextFormat().getPortionFormat().setLatinFont(new FontData("Arial"));

    axis.setMajorTickMark(TickMarkType.None);

    axis.getMajorGridLinesFormat().getLine().setWidth(1);
    axis.getMajorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.Solid);
    axis.getMajorGridLinesFormat().getLine().getFillFormat().getSolidFillColor().setColor(new Color(229, 229, 229));
}

static void configureVerticalAxis(IAxis axis) {
    axis.getFormat().getLine().setWidth(2);
    axis.getFormat().getLine().getFillFormat().setFillType(FillType.Solid);
    axis.getFormat().getLine().getFillFormat().getSolidFillColor().setColor(Color.BLACK);

    axis.getTextFormat().getPortionFormat().getFillFormat().setFillType(FillType.Solid);
    axis.getTextFormat().getPortionFormat().getFillFormat().getSolidFillColor().setColor(new Color(21, 25, 33));
    axis.getTextFormat().getPortionFormat().setFontHeight(10);
    axis.getTextFormat().getPortionFormat().setLatinFont(new FontData("Arial"));

    axis.setMajorTickMark(TickMarkType.None);

    axis.setNumberFormatLinkedToSource(false);
    axis.setNumberFormat("#,##0.0\\%");

    axis.getMajorGridLinesFormat().getLine().setWidth(1);
    axis.getMajorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.Solid);
    axis.getMajorGridLinesFormat().getLine().getFillFormat().getSolidFillColor().setColor(new Color(229, 229, 229));
}

static void configureSecondaryHorizontalAxis(IAxis axis) {
    axis.setVisible(false);

    axis.getMajorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.NoFill);
    axis.getMinorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.NoFill);
}

static void configureSecondaryVerticalAxis(IAxis axis) {
    axis.setVisible(false);
    axis.setPlotOrderReversed(true);
    axis.setCrossType(CrossesType.Maximum);

    axis.getMajorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.NoFill);
    axis.getMinorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.NoFill);
}

static void configurePlotArea(IChartPlotArea plotArea) {
    plotArea.getFormat().getLine().getFillFormat().setFillType(FillType.Solid);
    plotArea.getFormat().getLine().setDashStyle(LineDashStyle.Dot);
}

static void configureChartLegend(ILegend legend) {
    legend.setPosition(LegendPositionType.Bottom);

    legend.getTextFormat().getPortionFormat().setFontHeight(13);
    legend.getTextFormat().getPortionFormat().setLatinFont(new FontData("Arial"));
    legend.getTextFormat().getPortionFormat().getFillFormat().setFillType(FillType.Solid);
    legend.getTextFormat().getPortionFormat().getFillFormat().getSolidFillColor().setColor(new Color(102, 102, 102));
}

static void setSeriesDataFormat(IChartDataWorkbook workbook, int worksheetIndex, int columnIndex, int pointCount) {
    final String format = "#,##0.0\\%";

    for (int i = 0; i < pointCount; i++) {
        int rowIndex = 1 + i;
        workbook.getCell(worksheetIndex, rowIndex, columnIndex).setCustomNumberFormat(format);
    }
}

static void hideChartZeroValues(IChart chart) {
    for (IChartSeries series : chart.getChartData().getSeries()) {
        for (int i = 0; i < series.getDataPoints().size(); i++) {
            IChartDataPoint dataPoint = series.getDataPoints().get_Item(i);

            if (Double.isNaN(dataPoint.getValue().toDouble())) {
                dataPoint.getFormat().getFill().setFillType(FillType.NoFill);
                dataPoint.getFormat().getLine().getFillFormat().setFillType(FillType.NoFill);

                dataPoint.getMarker().getFormat().getFill().setFillType(FillType.NoFill);
                dataPoint.getMarker().getFormat().getLine().getFillFormat().setFillType(FillType.NoFill);

                series.getLabels().get_Item(i).hide();
            }
        }
    }
}

files.zip (231.4 KB)

Could you please check the result and clarify what is wrong?

Hi @andrey.potapov

I was going over the code snippet shared by you.

I see you have changed the order of method invocation

image.png (10.6 KB)

We always have to render the charts which are in primary axis first followed by secondary axis charts, so the order of invocation should be
1 → addStackedColumnChart(slide);
2 → addLineWithMarkersChart(chart);
3 → addClusteredColumnChart(chart);

This way invocation works perfectly fine for Excel but for ppt only we are facing the issue of missing the line chart legend symbol

image.png (68.0 KB)

So , could you please help us resolve this issue without changing the order of method invocation ?

Thanks,
Thilak Babu

@Thilakbabu,
Thank you for the details. I need some time to answer your question. I will get back to you as soon as possible.

1 Like

Hi @andrey.potapov

Good day!

Just trying to follow up if you have any updates! Please keep us posted. Thanks.

@Thilakbabu,
We will keep you updated. Thank you for your patience.

Hi @andrey.potapov
If we could get an update on this it would be great. Please let us know if there are any updates from your side.

@devkapur,
The issue was scheduled to be investigated this week. Unfortunately, I don’t have any additional information yet. We will keep you updated.

Hi @andrey.potapov

Good day!

By any chance, were you able to check on this issue? We are waiting for an update. Pls keep us posted!

Thanks,
Thilak Babu

@Thilakbabu,
The issue has been blocked by a similar issue in Aspose.Slides for .NET. Our developers are working on the issue. That is all I can tell you for now. Thank you for your patience.

Hi @andrey.potapov

Could you pls share the Aspose ticket reference if you have any?

@Thilakbabu,
The issue you encountered has been registered under ticket SLIDESJAVA-39651. The blocking ticket is SLIDESNET-44931.

1 Like

Hi @andrey.potapov

Could you please update on this issue? Were you able to get this fixed. Pls keep us posted on the Expected release version for this fix.

Thanks,
Thilak Babu

@Thilakbabu,
Our developers are working on the issue. Unfortunately, I don’t have any additional information yet. Thank you for your patience.