Remove the Primary Axis from a Combo Chart and Show Only Secondary Axis in Java

Hi Team,
I want to remove the primary axis for aspose slide.
I want to display only the secondary axis. Could you help me on this.

import java.awt.Color;

import com.aspose.slides.ChartType;
import com.aspose.slides.FillType;
import com.aspose.slides.IChart;
import com.aspose.slides.IChartDataWorkbook;
import com.aspose.slides.IChartSeries;
import com.aspose.slides.IDataLabel;
import com.aspose.slides.ISlide;
import com.aspose.slides.MarkerStyleType;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;

public class TestDualAxis {

  public static void main(String[] args) {
    chartwithmultipleTypes();

  }

  public static void chartwithmultipleTypes() {
    try {
      // Instantiate Presentation class that represents PPTX file//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(ChartType.ClusteredColumn, 0, 0, 500, 500);

      // Setting chart Title
      // chart.ChartTitle.TextFrameForOverriding.Text = "Sample Title";
      // chart.cartTitle.AddTextFrameForOverriding("Sample Title");
      // chart.ChartTitle.TextFrameForOverriding.TextFrameFormat.CenterText = NullableBool.True;
      // chart.ChartTitle.Height = 20;
      // chart.HasTitle = 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);

      // Now populating series data

      series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 1, 1, 20));
      series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 2, 1, 50));
      series.getDataPoints().addDataPointForBarSeries(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);

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

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


      // create custom labels for each of categories for new series

      // 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("/");


      ///// Addding second type
      // Add new series
      series = chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 3, "Series 3"),
          ChartType.ScatterWithSmoothLines);


      // Add new point (1:3) there.
      series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 1, 3, 10),
          fact.getCell(defaultWorksheetIndex, 1, 4, 13));

      // Add new point (2:10)
      series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 2, 3, 20),
          fact.getCell(defaultWorksheetIndex, 2, 4, 15));



      // Changing the chart series marker
      series.getMarker().setSize(10);
      series.getMarker().setSymbol(MarkerStyleType.Star);
      series.setPlotOnSecondAxis(true);
      chart.getAxes().getSecondaryVerticalAxis().setTickLabelPosition(0);

      // Take second chart series
      series = chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 5, "Series 4"),
          ChartType.ScatterWithStraightLinesAndMarkers);

      // Add new point (5:2) there.
      series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 1, 5, 5),
          fact.getCell(defaultWorksheetIndex, 1, 6, 2));

      // Add new point (3:1)
      series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 2, 5, 3),
          fact.getCell(defaultWorksheetIndex, 2, 6, 1));

      // Add new point (2:2)
      series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 3, 5, 2),
          fact.getCell(defaultWorksheetIndex, 3, 6, 2));

      // Add new point (5:1)
      series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 4, 5, 5),
          fact.getCell(defaultWorksheetIndex, 4, 6, 1));

      // Changing the chart series marker
      series.getMarker().setSize( 10);
      series.getMarker().setSymbol( MarkerStyleType.Circle);

      series.setPlotOnSecondAxis( false);


      // Save presentation with chart
      pres.save("C:\\Nielsen\\test1234.pptx", SaveFormat.Pptx);
      System.out.println(">>>>>>>>>>>>>>>>>");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

@manikandan1234,
Thank you for posting your requirements.

To understand the issue better and help you, could you kindly share a presentation file with the expected result?

Hi @andrey.potapov
The issue is for enabling secondary axis as below code.
series.setPlotOnSecondAxis(true);

import java.awt.Color;

import com.aspose.slides.ChartType;
import com.aspose.slides.FillType;
import com.aspose.slides.IChart;
import com.aspose.slides.IChartDataWorkbook;
import com.aspose.slides.IChartSeries;
import com.aspose.slides.IDataLabel;
import com.aspose.slides.ISlide;
import com.aspose.slides.MarkerStyleType;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;

public class TestDualAxis {

  public static void main(String[] args) {
    chartwithmultipleTypes();

  }

  public static void chartwithmultipleTypes() {
    try {
      Presentation pres = new Presentation();

      ISlide sld = pres.getSlides().get_Item(0);
      IChart chart = sld.getShapes().addChart(ChartType.ClusteredColumn, 0, 0, 500, 500);
      chart.getChartData().getSeries().get_Item(0).getLabels().getDefaultDataLabelFormat().setShowValue(true);
      int defaultWorksheetIndex = 0;
      IChartDataWorkbook fact = chart.getChartData().getChartDataWorkbook();
      chart.getChartData().getSeries().clear();
      chart.getChartData().getCategories().clear();
      int s = chart.getChartData().getSeries().size();
      s = chart.getChartData().getCategories().size();
      chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 1, "Series 1"), chart.getType());
      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"));
      chart.getAxes().getVerticalAxis().setVisible(false);
      IChartSeries series = chart.getChartData().getSeries().get_Item(0);
      series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 1, 1, 20));
      series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 2, 1, 50));
      series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 3, 1, 30));
      series.getFormat().getFill().setFillType(FillType.Solid);
      series.getFormat().getFill().getSolidFillColor().setColor(Color.RED);
      series.setPlotOnSecondAxis( true);
      // Save presentation with chart
      pres.save("C:\\Nielsen\\test1234.pptx", SaveFormat.Pptx);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

I am getting below error.
image.png (5.0 KB)

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

@manikandan1234,
Thank you for your patience. I reproduced the error you described.

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-39443

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.

@manikandan1234,
Our developers have investigated the case. The exception occurs because the chart contains only one axis. If you add more series, no exceptions will occur.
plotOnAxis.png (11.2 KB)

We can’t remove the primary axes, but we can hide them. Please try using the following code to hide all primary axes:

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

noPrimAxis.png (25.5 KB)

Please try using the following code example:

try {
    // Instantiate Presentation class that represents PPTX file//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(ChartType.ClusteredColumn, 0, 0, 500, 500);

    // Setting chart Title
    // chart.ChartTitle.TextFrameForOverriding.Text = "Sample Title";
    // chart.cartTitle.AddTextFrameForOverriding("Sample Title");
    // chart.ChartTitle.TextFrameForOverriding.TextFrameFormat.CenterText = NullableBool.True;
    // chart.ChartTitle.Height = 20;
    // chart.HasTitle = 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);

    // Now populating series data

    series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 1, 1, 20));
    series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 2, 1, 50));
    series.getDataPoints().addDataPointForBarSeries(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);

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

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


    // create custom labels for each of categories for new series

    // 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("/");


    ///// Addding second type
    // Add new series
    series = chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 3, "Series 3"),
            ChartType.ScatterWithSmoothLines);


    // Add new point (1:3) there.
    series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 1, 3, 10),
            fact.getCell(defaultWorksheetIndex, 1, 4, 13));

    // Add new point (2:10)
    series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 2, 3, 20),
            fact.getCell(defaultWorksheetIndex, 2, 4, 15));



    // Changing the chart series marker
    series.getMarker().setSize(10);
    series.getMarker().setSymbol(MarkerStyleType.Star);
    series.setPlotOnSecondAxis(true);
    chart.getAxes().getSecondaryVerticalAxis().setTickLabelPosition(0);

    // Take second chart series
    series = chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 5, "Series 4"),
            ChartType.ScatterWithStraightLinesAndMarkers);

    // Add new point (5:2) there.
    series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 1, 5, 5),
            fact.getCell(defaultWorksheetIndex, 1, 6, 2));

    // Add new point (3:1)
    series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 2, 5, 3),
            fact.getCell(defaultWorksheetIndex, 2, 6, 1));

    // Add new point (2:2)
    series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 3, 5, 2),
            fact.getCell(defaultWorksheetIndex, 3, 6, 2));

    // Add new point (5:1)
    series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 4, 5, 5),
            fact.getCell(defaultWorksheetIndex, 4, 6, 1));

    // Changing the chart series marker
    series.getMarker().setSize( 10);
    series.getMarker().setSymbol( MarkerStyleType.Circle);

    series.setPlotOnSecondAxis( false);

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

    // Save presentation with chart
    pres.save(folderPath + "test1234.pptx", SaveFormat.Pptx);
    System.out.println(">>>>>>>>>>>>>>>>>");
} catch (Exception e) {
    e.printStackTrace();
}

Chart Axis|Aspose.Slides Documentation