0

I have a chart with some data present in it. I don't want to show the x and Y axis. I somehow find out how to remove the legend in the chart. But failed to hide the axis.

enter image description here

I just want to remove the Grid line and Axes also. I have the chart object with the below code.

  PowerPoint.Shape chartShape = slide.Shapes.AddChart(Microsoft.Office.Core.XlChartType.xlBarClustered, left, top, width, height);

  //Get the chart
  PowerPoint.Chart chart = chartShape.Chart;
  chart.Legend.Clear();

Can anyone knows how to hide it with c#?

Mari Selvan
  • 3,598
  • 3
  • 21
  • 36
  • Create a similar chart in Excel, then use Excel's VBA macro recorder to record what happens when you make the changes you want manually. That'll often give you a good idea of how to use the chart object model programmatically. – Steve Rindsberg Feb 24 '21 at 16:34
  • I use c# for my addon. Is this help for me. – Mari Selvan Feb 25 '21 at 03:25
  • I record the detail it shows `ActiveChart.SetElement (msoElementPrimaryCategoryAxisNone)` but I can't find a way to use this in c# VSTO addon. – Mari Selvan Feb 25 '21 at 03:46
  • I don't use C#, so can't really help with translating from VBA, but I'd imagine you'd start with chart.SetElement ... etc – Steve Rindsberg Feb 25 '21 at 16:52
  • Thanks. Very useful comment. Can you post it as an answer? The SetElement thing was working well. – Mari Selvan Feb 26 '21 at 04:03
  • Glad it helped; but rather than (C# VSTO clueless) me posting an answer, please post the answer yourself along with a simple code sample showing how you solved the problem. That'd be much more useful and trustworthy to future visitors. – Steve Rindsberg Feb 26 '21 at 17:56
  • Thanks a lot for your guidance...will update this with an answer. – Mari Selvan Mar 02 '21 at 08:45

1 Answers1

1

If you use Aspose.Slides for .NET instead of Office Interop, you will hide the chart legend, axes and gridlines as shown below:

    using var presentation = new Presentation();
    var chart = presentation.Slides[0].Shapes.AddChart(ChartType.ClusteredBar, 20, 20, 400, 300);
    
    chart.HasLegend = false;
    
    chart.Axes.VerticalAxis.IsVisible = false;
    chart.Axes.HorizontalAxis.IsVisible = false;
    
    chart.Axes.HorizontalAxis.MajorGridLinesFormat.Line.FillFormat.FillType = FillType.NoFill;
    
    // Save the chart with default data
    presentation.Save("result.pptx", SaveFormat.Pptx);

Result with default data:

enter image description here

Documentation | API Reference | Free forum

You can also evaluate Aspose.Slides Cloud for presentation manipulating. This REST-based API allows you to make 150 free API calls per month for API learning and presentation processing.

I work at Aspose.

Andrey Potapov
  • 29
  • 2
  • 14