Group Two-Level Axis Labels in a Chart in PowerPoint in C#, VB.NET

2018-03-07 06:41:56 Written by  support iceblue
Rate this item
(0 votes)

Sometimes, you may have a chart that contains two levels of horizontal axis labels, as shown in the following screenshot, and you need to group the labels by fruit and vegies. This article will show you how to group the category axis labels using Spire.Presentation.

Group Two-Level Axis Labels in a Chart in PowerPoint in C#, VB.NET

Step 1: Create a Presentation instance and load the sample PowerPoint file.

Presentation ppt = new Presentation();
ppt.LoadFromFile("chart.pptx");

Step 2: Get the chart.

IChart chart = ppt.Slides[0].Shapes[0] as IChart;

Step 3: Get the category axis from the chart.

IChartAxis chartAxis = chart.PrimaryCategoryAxis;

Step 4: Determine if the axis has multilevel labels, if yes, group the axis labels that have the same first-level label.

if(chartAxis.HasMultiLvlLbl)
{
    chartAxis.IsMergeSameLabel = true;
}

Step 5: Save the file.

ppt.SaveToFile("result.pptx", FileFormat.Pptx2010);

Output:

Group Two-Level Axis Labels in a Chart in PowerPoint in C#, VB.NET

Full Code:

[C#]
using Spire.Presentation;
using Spire.Presentation.Charts;

namespace GroupLabel
{
    class Program
    {
        static void Main(string[] args)
        {
            Presentation ppt = new Presentation();
            ppt.LoadFromFile("chart.pptx");
            IChart chart = ppt.Slides[0].Shapes[0] as IChart;
            IChartAxis chartAxis = chart.PrimaryCategoryAxis;
            if (chartAxis.HasMultiLvlLbl)
            {
                //group labels
                chartAxis.IsMergeSameLabel = true;
            }
            ppt.SaveToFile("result.pptx", FileFormat.Pptx2010);
        }
    }
}
[VB.NET]
Imports Spire.Presentation
Imports Spire.Presentation.Charts

Namespace GroupLabel
	Class Program
		Private Shared Sub Main(args As String())
			Dim ppt As New Presentation()
			ppt.LoadFromFile("chart.pptx")
			Dim chart As IChart = TryCast(ppt.Slides(0).Shapes(0), IChart)
			Dim chartAxis As IChartAxis = chart.PrimaryCategoryAxis
			If chartAxis.HasMultiLvlLbl Then
				'group labels
				chartAxis.IsMergeSameLabel = True
			End If
			ppt.SaveToFile("result.pptx", FileFormat.Pptx2010)
		End Sub
	End Class
End Namespace

Additional Info

  • tutorial_title: Group Two-Level Axis Labels in a Chart in PowerPoint
Last modified on Friday, 24 September 2021 09:24