Free Support Forum - aspose.com

Moving Secondary Vertical Axis to Right Side of Chart .NET

I am having an issue generating a slide with a ClusteredColumn Chart with a secondary Vertical Axis on the right side of the chart. Below is example code that I was using as a test case to recreate this issue in version 19.7.0 in .NET.

I have seen issues that appeared similar that have been fixed in the Java version but I am still having this issue in the current version of .NET

public static void chartwithmultipleTypes()
{
//Instantiate Presentation class that represents PPTX file//Instantiate Presentation class that represents PPTX file
PresentationEx pres = new PresentationEx();

        //Access first slide
        ISlide sld = pres.Slides[0];

        // Add chart with default data
        IChart chart = sld.Shapes.AddChart(ChartTypeEx.ClusteredColumn, 0, 0, 500, 500);

        //Setting chart Title
        //chart.ChartTitle.TextFrameForOverriding.Text = "Sample Title";
        chart.ChartTitle.AddTextFrameForOverriding("Sample Title");
        chart.ChartTitle.TextFrameForOverriding.TextFrameFormat.CenterText = NullableBool.True;
        chart.ChartTitle.Height = 20;
        chart.HasTitle = true;

        //Set first series to Show Values
        chart.ChartData.Series[0].Labels.DefaultDataLabelFormat.ShowValue = true;

        //Setting the index of chart data sheet
        int defaultWorksheetIndex = 0;

        //Getting the chart data worksheet
        IChartDataWorkbook fact = chart.ChartData.ChartDataWorkbook;

        //Delete default generated series and categories
        chart.ChartData.Series.Clear();
        chart.ChartData.Categories.Clear();
        int s = chart.ChartData.Series.Count;
        s = chart.ChartData.Categories.Count;

        //Adding new series
        chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 0, 1, "Series 1"), chart.Type);
        chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 0, 2, "Series 2"), chart.Type);

        //Adding new categories
        chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 1, 0, "Category 1"));
        chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 2, 0, "Category 2"));
        chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 3, 0, "Category 3"));

        //Take first chart series
        IChartSeries series = chart.ChartData.Series[0];

        //Now populating series data

        series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 1, 1, 20));
        series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 2, 1, 50));
        series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 3, 1, 30));

        //Setting fill color for series
        series.Format.Fill.FillType = FillType.Solid;
        series.Format.Fill.SolidFillColor.Color = Color.Red;


        //Take second chart series
        series = chart.ChartData.Series[1];

        //Now populating series data
        series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 1, 2, 30));
        series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 2, 2, 10));
        series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 3, 2, 60));

        //Setting fill color for series
        series.Format.Fill.FillType = FillType.Solid;
        series.Format.Fill.SolidFillColor.Color = Color.Green;


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

        //first label will be show Category name
        IDataLabel lbl = series.DataPoints[0].Label;
        lbl.DataLabelFormat.ShowCategoryName = true;

        lbl = series.DataPoints[1].Label;
        lbl.DataLabelFormat.ShowSeriesName = true;

        //Show value for third label
        lbl = series.DataPoints[2].Label;
        lbl.DataLabelFormat.ShowValue = true;
        lbl.DataLabelFormat.ShowSeriesName = true;
        lbl.DataLabelFormat.Separator = "/";


        /////Addding second type
        //Add new series
        series = chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 0, 3, "Series 3"), ChartTypeEx.Line);


        //Add new point (1:3) there.
        series.DataPoints.AddDataPointForLineSeries(fact.GetCell(defaultWorksheetIndex, 1, 3, 10));
        series.DataPoints.AddDataPointForLineSeries(fact.GetCell(defaultWorksheetIndex, 2, 3, 5));
        series.DataPoints.AddDataPointForLineSeries(fact.GetCell(defaultWorksheetIndex, 3, 3, 20));



        //Changing the chart series marker
        series.Marker.Size = 10;
        series.Marker.Symbol = MarkerStyleType.Star;
        series.PlotOnSecondAxis = true;
        chart.Axes.SecondaryHorizontalAxis.IsVisible = false;
        chart.Axes.SecondaryVerticalAxis.Position = AxisPositionType.Right;


        //Save presentation with chart
        pres.Save("C:\\Presentations\\AsposeChart.pptx", SaveFormat.Pptx);
    }

@justin.brown,

I have observed the sample code provided. Can you please share the details of issue incurring on your end along with generated presentation and issue being highlighted.

AsposeChart.pdf (310.2 KB)
@mudassir.fayyaz I have attached a PDF export of the presentation that gets generated on my end on the first page. The second page would be the desired look with the axis on the right side of the chart. Thanks for taking a look!

@justin.brown,

I suggest you to please try using following sample code on your end.

public static void TestSecondaryAxis()
{
    // Instantiate Presentation class that represents PPTX file
    Presentation pres = new Presentation();

    // Access first slide
    ISlide sld = pres.Slides[0];

    // Add chart with default data
    IChart chart = sld.Shapes.AddChart(ChartType.ClusteredColumn, 0, 0, 500, 500);

    // Setting chart Title
    chart.ChartTitle.AddTextFrameForOverriding("Sample Title");
    chart.ChartTitle.TextFrameForOverriding.TextFrameFormat.CenterText=NullableBool.True;
    chart.ChartTitle.Height=20;
    chart.HasTitle = true ;

    // Set first series to Show Values
    chart.ChartData.Series[0].Labels.DefaultDataLabelFormat.ShowValue=true;

    // Setting the index of chart data sheet
    int defaultWorksheetIndex = 0;

    // Getting the chart data WorkSheet
    IChartDataWorkbook fact = chart.ChartData.ChartDataWorkbook;

    // Delete default generated series and categories
    chart.ChartData.Series.Clear();
    chart.ChartData.Categories.Clear();
    int s = chart.ChartData.Series.Count;
    s = chart.ChartData.Categories.Count;

    // Adding new series
    chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 0, 1, "Series 1"), chart.Type);
    chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 0, 2, "Series 2"), chart.Type);

    // Adding new categories
    chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 1, 0, "Caetegoty 1"));
    chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 2, 0, "Caetegoty 2"));
    chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 3, 0, "Caetegoty 3"));

    // Take first chart series
    IChartSeries series = chart.ChartData.Series[0];

    // Now populating series data
    series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 1, 1, 20));
    series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 2, 1, 50));
    series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 3, 1, 30));

    // Setting fill color for series
    series.Format.Fill.FillType=FillType.Solid;
    series.Format.Fill.SolidFillColor.Color=Color.Red;

    // Take second chart series
    series = chart.ChartData.Series[1];
    series.PlotOnSecondAxis=true;

    // Now populating series data
    series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 1, 2, 30));
    series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 2, 2, 10));
    series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 3, 2, 60));

    // Setting fill color for series
    series.Format.Fill.FillType=FillType.Solid;
    series.Format.Fill.SolidFillColor.Color=Color.Green;

    // create custom labels for each of categories for new series
    // first label will be show Category name
    IDataLabel lbl = series.DataPoints[0].Label;
    lbl.DataLabelFormat.ShowCategoryName=true;

    lbl = series.DataPoints[1].Label;
    lbl.DataLabelFormat.ShowSeriesName=true;

    // Show value for third label
    lbl = series.DataPoints[2].Label;
    lbl.DataLabelFormat.ShowValue=true;
    lbl.DataLabelFormat.ShowSeriesName=true;
    lbl.DataLabelFormat.Separator="/";

    // Move Primary and Secondary vertical axis
    chart.Axes.SecondaryHorizontalAxis.TickLabelPosition=TickLabelPositionType.High;
    chart.Axes.SecondaryHorizontalAxis.CrossType=CrossesType.Maximum;
    chart.Axes.SecondaryHorizontalAxis.IsVisible=false;

    // Save presentation with chart
    pres.Save(@"C:\Aspose Data\AsposeChart.pptx", Aspose.Slides.Export.SaveFormat.Pptx);
}

@mudassir.fayyaz Those updates did work to move the axis. Thank you very much for your help!

@justin.brown,

Its good to know things have worked on your end.