I Want to Create Chart on Secondary Axis Using Aspose.Slides for Java

Hi! Aspose Team
I want to create the chart on Secondary axis in Slides.
After the setting chartSeries to plotOnSecondaryAxis still the chart is not getting plotted on Secondary Axis.
I have attached the snippet code.
Please let me know if you need more details on the same.
thanks!
Rohan

public static void main(String[] args) {
    
    //ExStart:NormalCharts
    // The path to the documents directory.
    String dataDir = "D://AsposeSlides//";

    // Instantiate Presentation class that represents PPTX file
    Presentation pres = new Presentation();

    // Access first slide
    ISlide sld = pres.getSlides().get_Item(0);

    // Add chart with default data
    IChart chart = sld.getShapes().addChart(com.aspose.slides.ChartType.Line, 0, 0, 500, 500);

    // Setting chart Title
    // Chart.getChartTitle().getTextFrameForOverriding().setText("Sample Title");
    chart.getChartTitle().addTextFrameForOverriding("Sample Title");
    chart.getChartTitle().getTextFrameForOverriding().getTextFrameFormat().setCenterText(NullableBool.True);
    chart.getChartTitle().setHeight(20);
    chart.setTitle(true);

    // Set first series to Show Values
    chart.getChartData().getSeries().get_Item(0).getLabels().getDefaultDataLabelFormat().setShowValue(true);

    // Setting the index of chart data sheet
    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();
    int s = chart.getChartData().getSeries().size();
    s = chart.getChartData().getCategories().size();

    // Adding new series
    chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 1, "Series 1"), chart.getType());
    chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 2, "Series 2"), chart.getType());

    // Adding new categories
    chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 1, 0, "Caetegoty 1"));
    chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 2, 0, "Caetegoty 2"));
    chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 3, 0, "Caetegoty 3"));

    // Take first chart series
    IChartSeries series = chart.getChartData().getSeries().get_Item(0);
    series.setPlotOnSecondAxis(true);

    // Now populating series data

    series.getDataPoints().addDataPointForLineSeries(fact.getCell(defaultWorksheetIndex, 1, 1, 20));
    series.getDataPoints().addDataPointForLineSeries(fact.getCell(defaultWorksheetIndex, 2, 1, 50));
    series.getDataPoints().addDataPointForLineSeries(fact.getCell(defaultWorksheetIndex, 3, 1, 30));

    // Setting fill color for series
    series.getFormat().getFill().setFillType(FillType.Solid);
    series.getFormat().getFill().getSolidFillColor().setColor(Color.RED);


    // Take second chart series
    series = chart.getChartData().getSeries().get_Item(1);
    series.setPlotOnSecondAxis(true);

    // Now populating series data
    series.getDataPoints().addDataPointForLineSeries(fact.getCell(defaultWorksheetIndex, 1, 2, 30));
    series.getDataPoints().addDataPointForLineSeries(fact.getCell(defaultWorksheetIndex, 2, 2, 10));
    series.getDataPoints().addDataPointForLineSeries(fact.getCell(defaultWorksheetIndex, 3, 2, 60));

    // Setting fill color for series
    series.getFormat().getFill().setFillType(FillType.Solid);
    series.getFormat().getFill().getSolidFillColor().setColor(Color.GREEN);

    // First label will be show Category name
    IDataLabel lbl = series.getDataPoints().get_Item(0).getLabel();
    lbl.getDataLabelFormat().setShowCategoryName(true);

    lbl = series.getDataPoints().get_Item(1).getLabel();
    lbl.getDataLabelFormat().setShowSeriesName(true);

    // Show value for third label
    lbl = series.getDataPoints().get_Item(2).getLabel();
    lbl.getDataLabelFormat().setShowValue(true);
    lbl.getDataLabelFormat().setShowSeriesName(true);
    lbl.getDataLabelFormat().setSeparator("/");
    IAxis xAxis = chart.getAxes().getSecondaryHorizontalAxis();
    IAxis yAxis = chart.getAxes().getSecondaryVerticalAxis();
    System.err.println("xAxis :"+xAxis);
    System.err.println("yAxis :"+yAxis);
    System.err.println(chart.getChartData().getSeries().size());
    // Save presentation with chart
    pres.save(dataDir + "AsposeChart_out.pptx", SaveFormat.Pptx);
    //ExEnd:NormalCharts

  }

@Rohan_Wankar,
Thank you for contacting support.

If I understand correctly, you would like to display the secondary axes on the chart. Could you please confirm?

yes @Andrey_Potapov

@Rohan_Wankar,
I’ve reproduced the problem with the secondary axes.

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): SLIDESJAVA-39110

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.

@Rohan_Wankar,
Our developers have investigated the case. To display the secondary axes on the chart, you should use the PlotOnSecondAxis method. Please try using the following code snippet:

series.setPlotOnSecondAxis(true);

chart.getAxes().getSecondaryVerticalAxis().setMaxValue(7);
chart.getAxes().getSecondaryVerticalAxis().setAutomaticMaxValue(false);

IAxis xAxis = chart.getAxes().getSecondaryHorizontalAxis();
IAxis yAxis = chart.getAxes().getSecondaryVerticalAxis();

System.err.println("xAxis :" + xAxis);
System.err.println("yAxis :" + yAxis);

Documents: Chart Formatting

Hi! @Andrey_Potapov
When we have two series object for the chart then I am getting the secondary axis, but if I have only one series for the chart then secondary axis is coming null.
I have added the snippet code.
Please take a look

public static void main(String[] args) {

    //ExStart:NormalCharts
    // The path to the documents directory.
    String dataDir = "D://AsposeSlides//";

    // Instantiate Presentation class that represents PPTX file
    Presentation pres = new Presentation();

    // Access first slide
    ISlide sld = pres.getSlides().get_Item(0);

    // Add chart with default data
    IChart chart = sld.getShapes().addChart(com.aspose.slides.ChartType.Line, 0, 0, 500, 500);

    // Setting chart Title
    // Chart.getChartTitle().getTextFrameForOverriding().setText("Sample Title");
    chart.getChartTitle().addTextFrameForOverriding("Sample Title");
    chart.getChartTitle().getTextFrameForOverriding().getTextFrameFormat().setCenterText(NullableBool.True);
    chart.getChartTitle().setHeight(20);
    chart.setTitle(true);

    // Set first series to Show Values
    chart.getChartData().getSeries().get_Item(0).getLabels().getDefaultDataLabelFormat().setShowValue(true);

    // Setting the index of chart data sheet
    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();
    int s = chart.getChartData().getSeries().size();
    System.err.println("series Size :"+s);
    s = chart.getChartData().getCategories().size();

    // Adding new series
    chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 1, "Series 1"), chart.getType());
    //chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 2, "Series 2"), chart.getType());

    // Adding new categories
    chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 1, 0, "Caetegory 1"));
//    chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 2, 0, "Caetegory 2"));
//    chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 3, 0, "Caetegory 3"));

    // Take first chart series
    IChartSeries series = chart.getChartData().getSeries().get_Item(0);
    series.setPlotOnSecondAxis(true);

    // Now populating series data

    series.getDataPoints().addDataPointForLineSeries(fact.getCell(defaultWorksheetIndex, 1, 1, 20));
    series.getDataPoints().addDataPointForLineSeries(fact.getCell(defaultWorksheetIndex, 2, 1, 50));
    series.getDataPoints().addDataPointForLineSeries(fact.getCell(defaultWorksheetIndex, 3, 1, 30));

    // Setting fill color for series
    series.getFormat().getFill().setFillType(FillType.Solid);
    //series.getFormat().getFill().getSolidFillColor().setColor(Color.RED);


    // Take second chart series
//    series = chart.getChartData().getSeries().get_Item(1);
//    series.setPlotOnSecondAxis(true);
//
//    // Now populating series data
//    series.getDataPoints().addDataPointForLineSeries(fact.getCell(defaultWorksheetIndex, 1, 2, 30));
//    series.getDataPoints().addDataPointForLineSeries(fact.getCell(defaultWorksheetIndex, 2, 2, 10));
//    series.getDataPoints().addDataPointForLineSeries(fact.getCell(defaultWorksheetIndex, 3, 2, 60));
//
//    // Setting fill color for series
//    series.getFormat().getFill().setFillType(FillType.Solid);
//    //series.getFormat().getFill().getSolidFillColor().setColor(Color.GREEN);

    // First label will be show Category name
    IDataLabel lbl = series.getDataPoints().get_Item(0).getLabel();
    lbl.getDataLabelFormat().setShowCategoryName(true);

    lbl = series.getDataPoints().get_Item(1).getLabel();
    lbl.getDataLabelFormat().setShowSeriesName(true);

    // Show value for third label
    lbl = series.getDataPoints().get_Item(2).getLabel();
    lbl.getDataLabelFormat().setShowValue(true);
    lbl.getDataLabelFormat().setShowSeriesName(true);
    lbl.getDataLabelFormat().setSeparator("/");
    IAxis xAxis = chart.getAxes().getSecondaryHorizontalAxis();
    IAxis yAxis = chart.getAxes().getSecondaryVerticalAxis();
    System.err.println("xAxis :"+xAxis);
    System.err.println("yAxis :"+yAxis);
    System.err.println(chart.getChartData().getSeries().size());
    // Save presentation with chart
    pres.save(dataDir + "AsposeChart_out.pptx", SaveFormat.Pptx);
    //ExEnd:NormalCharts
}

@Rohan_Wankar,
I’ve reproduced the same result and forwarded the code example to our developers. We will get back to you ASAP.

@Rohan_Wankar,
With PowerPoint 2013 and 2021, we were unable to set plot series on the secondary axis with only one series. Could you please provide the chart created in PowerPoint with the expected result?

Please note that if there is only one series in the chart, then this series can only be on Primary Axis. Therefore null for the Secondary Axis is the expected behavior in this case.