How to Create a Combination Chart in Presentation Using Aspose.Slides for Java?

Hello,

Need help in creating a combination chart similar to the attached PPT using Aspose.Slides for Java API.
Attaching a sample PPT file with the manually created chart.
Thanks a lot
file.zip (38.8 KB)

@manavjaiswal,
Thank you for contacting support. I am working on the question and will get back to you as soon as possible.

@manavjaiswal,
We have opened the following new ticket(s) in our internal issue tracking system and will consider your question according to the terms mentioned in Free Support Policies.

Issue ID(s): SLIDESJAVA-39293

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.

Hi,
Will you be providing a code snippet for this or will there be an enhancement in aspose future versions? Please provide what is to be expected.

Thank you

@manavjaiswal,
We will provide a code example soon. Thank you for your patience.

@manavjaiswal,
Please use the following code example:

Presentation pres = new Presentation();
try {
    IChart chart = pres.getSlides().get_Item(0).getShapes().addChart(ChartType.VolumeHighLowClose, 50, 50, 600, 400);

    // Sets the chart Title
    chart.setTitle(true);
    chart.getChartTitle().addTextFrameForOverriding("Chart Title");
    chart.getChartTitle().setOverlay(false);
    IPortionFormat portionFormat = chart.getChartTitle().getTextFrameForOverriding().getParagraphs().get_Item(0).getParagraphFormat().getDefaultPortionFormat();
    portionFormat.setFontBold(NullableBool.False);
    portionFormat.setFontHeight(18);

    // Deletes the default generated series and categories
    chart.getLegend().setPosition(LegendPositionType.Bottom);
    chart.getLegend().getTextFormat().getPortionFormat().setFontHeight(12);

    final int defaultWorksheetIndex = 0;
    IChartDataWorkbook fact = chart.getChartData().getChartDataWorkbook();
    chart.getChartData().getSeries().clear();
    chart.getChartData().getCategories().clear();

    // Adds new categories
    chart.getChartData().getCategories().add(fact.getCell(0, 1, 0, "Category 1"));
    chart.getChartData().getCategories().add(fact.getCell(0, 2, 0, "Category 2"));
    chart.getChartData().getCategories().add(fact.getCell(0, 3, 0, "Category 3"));
    chart.getChartData().getCategories().add(fact.getCell(0, 4, 0, "Category 4"));

    // Populates series data
    IChartSeries series = chart.getChartData().getSeries().add(fact.getCell(0, 0, 1, "Series 1"), ChartType.ClusteredColumn);
    // Set Overlap
    series.getParentSeriesGroup().setOverlap((byte) -27);
    series.getParentSeriesGroup().setGapWidth(219);
    series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 1, 1, 4.3));
    series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 2, 1, 2.5));
    series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 3, 1, 3.5));
    series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 4, 1, 4.5));

    // Populates series data
    series = chart.getChartData().getSeries().add(fact.getCell(0, 0, 2, "Series 2"), ChartType.ClusteredColumn);
    // Set Overlap
    series.getParentSeriesGroup().setOverlap((byte) -27);
    series.getParentSeriesGroup().setGapWidth(219);
    series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 1, 2, 2.4));
    series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 2, 2, 4.4));
    series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 3, 2, 1.8));
    series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 4, 2, 2.8));

    // Populates series data
    series = chart.getChartData().getSeries().add(fact.getCell(0, 0, 3, "Series 3"), ChartType.Line);
    series.setPlotOnSecondAxis(true);
    series.getDataPoints().addDataPointForLineSeries(fact.getCell(defaultWorksheetIndex, 1, 3, 2.0));
    series.getDataPoints().addDataPointForLineSeries(fact.getCell(defaultWorksheetIndex, 2, 3, 2.0));
    series.getDataPoints().addDataPointForLineSeries(fact.getCell(defaultWorksheetIndex, 3, 3, 3.0));
    series.getDataPoints().addDataPointForLineSeries(fact.getCell(defaultWorksheetIndex, 4, 3, 5.0));

    // Sets the horizontal axis
    chart.getAxes().getHorizontalAxis().setTitle(true);
    chart.getAxes().getHorizontalAxis().getTitle().setOverlay(false);
    chart.getAxes().getHorizontalAxis().getTitle().addTextFrameForOverriding("X Axis");
    chart.getAxes().getHorizontalAxis().getTextFormat().getPortionFormat().setFontHeight(12.0f);
    chart.getAxes().getHorizontalAxis().getFormat().getLine().getFillFormat().setFillType(FillType.NoFill);
    portionFormat = chart.getAxes().getHorizontalAxis().getTitle().getTextFrameForOverriding().
            getParagraphs().get_Item(0).getParagraphFormat().getDefaultPortionFormat();
    portionFormat.setFontHeight(12.0f);
    portionFormat.setFontBold(NullableBool.False);

    // Sets the vertical axis
    chart.getAxes().getVerticalAxis().setTitle(true);
    chart.getAxes().getVerticalAxis().getTitle().setOverlay(false);
    chart.getAxes().getVerticalAxis().getTitle().addTextFrameForOverriding("Y Axis 1");
    chart.getAxes().getVerticalAxis().getTextFormat().getPortionFormat().setFontHeight(12.0f);
    chart.getAxes().getVerticalAxis().getFormat().getLine().getFillFormat().setFillType(FillType.NoFill);
    portionFormat = chart.getAxes().getVerticalAxis().getTitle().getTextFrameForOverriding().
            getParagraphs().get_Item(0).getParagraphFormat().getDefaultPortionFormat();
    portionFormat.setFontHeight(12.0f);
    portionFormat.setFontBold(NullableBool.False);
    // Sets the major gridlines color
    chart.getAxes().getVerticalAxis().getMajorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.Solid);
    chart.getAxes().getVerticalAxis().getMajorGridLinesFormat().getLine().getFillFormat().getSolidFillColor().setColor(new Color(217, 217, 217));

    // Sets the secondary vertical axis
    chart.getAxes().getSecondaryVerticalAxis().setTitle(true);
    chart.getAxes().getSecondaryVerticalAxis().getTitle().setOverlay(false);
    chart.getAxes().getSecondaryVerticalAxis().getTitle().addTextFrameForOverriding("Y Axis 2");
    chart.getAxes().getSecondaryVerticalAxis().getTextFormat().getPortionFormat().setFontHeight(12.0f);
    chart.getAxes().getSecondaryVerticalAxis().getFormat().getLine().getFillFormat().setFillType(FillType.NoFill);
    portionFormat = chart.getAxes().getSecondaryVerticalAxis().getTitle().getTextFrameForOverriding().
            getParagraphs().get_Item(0).getParagraphFormat().getDefaultPortionFormat();
    portionFormat.setFontHeight(12.0f);
    portionFormat.setFontBold(NullableBool.False);

    // Sets the categories color
    chart.getChartData().getSeriesGroups().get_Item(0).get_Item(0).getFormat().getFill().setFillType(FillType.Solid);
    chart.getChartData().getSeriesGroups().get_Item(0).get_Item(0).getFormat().getFill().getSolidFillColor().setColor(new Color(68, 114, 196));
    chart.getChartData().getSeriesGroups().get_Item(0).get_Item(1).getFormat().getFill().setFillType(FillType.Solid);
    chart.getChartData().getSeriesGroups().get_Item(0).get_Item(1).getFormat().getFill().getSolidFillColor().setColor(new Color(237, 125, 49));

    pres.save("output.pptx", SaveFormat.Pptx);
} finally {
    if (pres != null) pres.dispose();
}

Hi,
Could you help with having the same output but with the chart type as ClusteredColumn.
Your code is not working for the ClusteredColumn chart. Pllease help with that.
Thank you

@manavjaiswal,
The code example above creates a chart similar to the one you provided as a sample. Could you please describe in detail the problem you are currently facing? We will do our best to help you.

I am trying to create a combo chart. I have done setPlotOnSecondAxis as TRUE for the second series but I am not able to get the right output i.e. both the axis are coming on left side.

Output file: ComboChart-01.zip (34.0 KB)

Please refer to this code and let me know what I’m doing wrong (Note: Chart type is ClusteredColumn) :

Presentation pres = new Presentation();
IChart chart = pres.getSlides().get_Item(0).getShapes().addChart(ChartType.ClusteredColumn, 50, 50, 600, 400);
chart.getChartTitle().addTextFrameForOverriding("Chart Title");
chart.getChartTitle().setHeight(20);
chart.getChartTitle().setOverlay(false);
chart.setTitle(true);
chart.getLegend().setPosition(LegendPositionType.Bottom);
chart.getLegend().getTextFormat().getPortionFormat().setFontHeight(12);
// Setting the index of the chart data sheet
final int defaultWorksheetIndex = 0;
// Getting the chart data worksheet
IChartDataWorkbook fact = chart.getChartData().getChartDataWorkbook();
// Delete default generated series and categories
chart.getChartData().getSeries().clear();
chart.getChartData().getCategories().clear();

// Adding new categories
chart.getChartData().getCategories().add(fact.getCell(0, 1, 0, "Jan"));
chart.getChartData().getCategories().add(fact.getCell(0, 2, 0, "Feb"));
chart.getChartData().getCategories().add(fact.getCell(0, 3, 0, "Mar"));
chart.getChartData().getCategories().add(fact.getCell(0, 4, 0, "Apr"));
chart.getChartData().getCategories().add(fact.getCell(0, 5, 0, "May"));

// Adding new series
IChartSeries series = chart.getChartData().getSeries().add(fact.getCell(0, 0, 1, "Total Revenue"), ChartType.ClusteredColumn);
// Now populating series data
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 1, 1, 34.912));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 2, 1, 21.919));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 3, 1, 49.198));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 4, 1, 45.214));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 5, 1, 26.871));

// Adding new series
IChartSeries series1 = chart.getChartData().getSeries().add(fact.getCell(0, 0, 2, "Price 2020"), ChartType.Line);
series1.setPlotOnSecondAxis(true);
// Now populating series data
series1.getDataPoints().addDataPointForLineSeries(fact.getCell(defaultWorksheetIndex, 1, 2, 10.312));
series1.getDataPoints().addDataPointForLineSeries(fact.getCell(defaultWorksheetIndex, 2, 2, 9.272));
series1.getDataPoints().addDataPointForLineSeries(fact.getCell(defaultWorksheetIndex, 3, 2, 12.696));
series1.getDataPoints().addDataPointForLineSeries(fact.getCell(defaultWorksheetIndex, 4, 2, 11.313));
series1.getDataPoints().addDataPointForLineSeries(fact.getCell(defaultWorksheetIndex, 5, 2, 11.379));

chart.getAxes().getVerticalAxis().setTitle(true);
chart.getAxes().getVerticalAxis().getTitle().addTextFrameForOverriding("Axis 1");
chart.getAxes().getVerticalAxis().getTitle().setOverlay(false);


chart.getAxes().getSecondaryVerticalAxis().setTitle(true);
chart.getAxes().getSecondaryVerticalAxis().getTitle().addTextFrameForOverriding("Axis 2");
chart.getAxes().getSecondaryVerticalAxis().getTitle().setOverlay(false);

chart.getAxes().getHorizontalAxis().setVisible(false);

pres.save("ComboChart-01.pptx", SaveFormat.Pptx);

@manavjaiswal,
I am working on the issue and will get back to you as soon as possible.

Will you providing a code snippet or will there be an advancement in aspose ?
Please share some issue no. for tracking purpose.

@manavjaiswal,
Could you please clarify why the above solution does not suit you?

I have a use case where ClusteredColumn combo chart needs to be created and as I described above through sample code, I am not getting the correct output.
And as per the requirement I have to get the correct output based on this scenario.
Please provide an update on code improvement or if some aspose enhancement will be done.

@manavjaiswal,
Thank you for the additional information. I’ve forwarded it to our developers.

The issue number is still the same (SLIDESJAVA-39293).

Hi,
Any updates on this?

@manavjaiswal,

I am afraid, we have not enhanced/devised the code snippet to accomplish your exact task yet. Once we have an update on it, we will let you know.

We are sorry for any inconvenience caused!

@manavjaiswal,

Could you please try the following code snippet for your task:

Presentation pres = new Presentation();
try {
    IChart chart = pres.getSlides().get_Item(0).getShapes().addChart(ChartType.ClusteredColumn, 50, 50, 600, 400);

    // Sets the chart Title
    chart.setTitle(true);
    chart.getChartTitle().addTextFrameForOverriding("Chart Title");
    chart.getChartTitle().setOverlay(false);
    IPortionFormat portionFormat = chart.getChartTitle().getTextFrameForOverriding().
            getParagraphs().get_Item(0).getParagraphFormat().getDefaultPortionFormat();
    portionFormat.setFontBold(NullableBool.False);
    portionFormat.setFontHeight(18);

    // Deletes the default generated series and categories
    chart.getLegend().setPosition(LegendPositionType.Bottom);
    chart.getLegend().getTextFormat().getPortionFormat().setFontHeight(12);

    int defaultWorksheetIndex = 0;
    IChartDataWorkbook fact = chart.getChartData().getChartDataWorkbook();
    chart.getChartData().getSeries().clear();
    chart.getChartData().getCategories().clear();

    // Adds new categories
    chart.getChartData().getCategories().add(fact.getCell(0, 1, 0, "Category 1"));
    chart.getChartData().getCategories().add(fact.getCell(0, 2, 0, "Category 2"));
    chart.getChartData().getCategories().add(fact.getCell(0, 3, 0, "Category 3"));
    chart.getChartData().getCategories().add(fact.getCell(0, 4, 0, "Category 4"));

    // Populates series data
    IChartSeries series = chart.getChartData().getSeries().add(fact.getCell(0, 0, 1, "Series 1"), ChartType.ClusteredColumn);
    // Set Overlap
    series.getParentSeriesGroup().setOverlap((byte) -27);
    series.getParentSeriesGroup().setGapWidth(219);
    series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 1, 1, 4.3));
    series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 2, 1, 2.5));
    series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 3, 1, 3.5));
    series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 4, 1, 4.5));

    // Populates series data
    series = chart.getChartData().getSeries().add(fact.getCell(0, 0, 2, "Series 2"), ChartType.ClusteredColumn);
    // Set Overlap
    series.getParentSeriesGroup().setOverlap((byte) -27);
    series.getParentSeriesGroup().setGapWidth(219);
    series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 1, 2, 2.4));
    series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 2, 2, 4.4));
    series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 3, 2, 1.8));
    series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 4, 2, 2.8));

    // Populates series data
    series = chart.getChartData().getSeries().add(fact.getCell(0, 0, 3, "Series 3"), ChartType.Line);
    series.setPlotOnSecondAxis(true);
    series.getDataPoints().addDataPointForLineSeries(fact.getCell(defaultWorksheetIndex, 1, 3, 2.0));
    series.getDataPoints().addDataPointForLineSeries(fact.getCell(defaultWorksheetIndex, 2, 3, 2.0));
    series.getDataPoints().addDataPointForLineSeries(fact.getCell(defaultWorksheetIndex, 3, 3, 3.0));
    series.getDataPoints().addDataPointForLineSeries(fact.getCell(defaultWorksheetIndex, 4, 3, 5.0));

    chart.getAxes().getSecondaryHorizontalAxis().setPosition(AxisPositionType.Bottom);
    chart.getAxes().getSecondaryHorizontalAxis().setTickLabelPosition(TickLabelPositionType.None);
    chart.getAxes().getSecondaryHorizontalAxis().setCrossType(CrossesType.Maximum);
    chart.getAxes().getSecondaryHorizontalAxis().setAxisBetweenCategories(true);
    chart.getAxes().getSecondaryHorizontalAxis().setVisible(false);
    chart.getAxes().getSecondaryHorizontalAxis().getMajorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.NoFill);
    chart.getAxes().getSecondaryHorizontalAxis().getMinorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.NoFill);

    // Sets the horizontal axis
    chart.getAxes().getHorizontalAxis().setTitle(true);
    chart.getAxes().getHorizontalAxis().getTitle().setOverlay(false);
    chart.getAxes().getHorizontalAxis().getTitle().addTextFrameForOverriding("X Axis");
    chart.getAxes().getHorizontalAxis().getTextFormat().getPortionFormat().setFontHeight(12.0f);
    chart.getAxes().getHorizontalAxis().getFormat().getLine().getFillFormat().setFillType(FillType.NoFill);
    portionFormat = chart.getAxes().getHorizontalAxis().getTitle().getTextFrameForOverriding().getParagraphs().get_Item(0).getParagraphFormat().getDefaultPortionFormat();
    portionFormat.setFontHeight(12.0f);
    portionFormat.setFontBold(NullableBool.False);

    // Sets the vertical axis
    chart.getAxes().getVerticalAxis().setBinWidth(4);
    chart.getAxes().getVerticalAxis().setAxisBetweenCategories(true);
    chart.getAxes().getVerticalAxis().setCrossAt(0);
    chart.getAxes().getVerticalAxis().setCrossType(CrossesType.AxisCrossesAtZero);
    chart.getAxes().getVerticalAxis().setPosition(AxisPositionType.Left);
    chart.getAxes().getVerticalAxis().setTickLabelPosition(TickLabelPositionType.NextTo);
    chart.getAxes().getVerticalAxis().setTitle(true);
    chart.getAxes().getVerticalAxis().getTitle().setOverlay(false);
    chart.getAxes().getVerticalAxis().getTitle().addTextFrameForOverriding("Y Axis 1");
    chart.getAxes().getVerticalAxis().getTextFormat().getPortionFormat().setFontHeight(12.0f);
    chart.getAxes().getVerticalAxis().getFormat().getLine().getFillFormat().setFillType(FillType.NoFill);
    portionFormat = chart.getAxes().getVerticalAxis().getTitle().getTextFrameForOverriding().getParagraphs().get_Item(0).getParagraphFormat().getDefaultPortionFormat();
    portionFormat.setFontHeight(12.0f);
    portionFormat.setFontBold(NullableBool.False);
    // Sets the major gridlines color
    chart.getAxes().getVerticalAxis().getMajorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.Solid);
    chart.getAxes().getVerticalAxis().getMajorGridLinesFormat().getLine().getFillFormat().getSolidFillColor().setColor( new Color(217, 217, 217));

    // Sets the secondary vertical axis
    chart.getAxes().getSecondaryVerticalAxis().setBinWidth(4);
    chart.getAxes().getSecondaryVerticalAxis().setPosition(AxisPositionType.Right);
    chart.getAxes().getSecondaryVerticalAxis().setTickLabelPosition(TickLabelPositionType.NextTo);
    chart.getAxes().getSecondaryVerticalAxis().setAxisBetweenCategories(true);
    chart.getAxes().getSecondaryVerticalAxis().setCrossType(CrossesType.AxisCrossesAtZero);
    chart.getAxes().getSecondaryVerticalAxis().getMajorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.NoFill);
    chart.getAxes().getSecondaryVerticalAxis().getMinorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.NoFill);
    chart.getAxes().getSecondaryVerticalAxis().setTitle(true);
    chart.getAxes().getSecondaryVerticalAxis().getTitle().setOverlay(false);
    chart.getAxes().getSecondaryVerticalAxis().getTitle().addTextFrameForOverriding("Y Axis 2");
    chart.getAxes().getSecondaryVerticalAxis().getTextFormat().getPortionFormat().setFontHeight(12.0f);
    chart.getAxes().getSecondaryVerticalAxis().getFormat().getLine().getFillFormat().setFillType(FillType.NoFill);
    portionFormat = chart.getAxes().getSecondaryVerticalAxis().getTitle().getTextFrameForOverriding().getParagraphs().get_Item(0).getParagraphFormat().getDefaultPortionFormat();
    portionFormat.setFontHeight(12.0f);
    portionFormat.setFontBold(NullableBool.False);

    // Sets the categories color
    chart.getChartData().getSeriesGroups().get_Item(0).get_Item(0).getFormat().getFill().setFillType(FillType.Solid);
    chart.getChartData().getSeriesGroups().get_Item(0).get_Item(0).getFormat().getFill().getSolidFillColor().setColor(new Color(68, 114, 196));
    chart.getChartData().getSeriesGroups().get_Item(0).get_Item(1).getFormat().getFill().setFillType(FillType.Solid);
    chart.getChartData().getSeriesGroups().get_Item(0).get_Item(1).getFormat().getFill().getSolidFillColor().setColor(new Color(237, 125, 49));

    pres.save("output-ClusteredColumn.pptx", SaveFormat.Pptx);
} finally {
    if (pres != null) pres.dispose();
} 

Hope, this helps a bit.