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

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#]
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]
Dim ppt As Presentation = New Presentation()
ppt.LoadFromFile("chart.pptx")
Dim chart As IChart = ppt.Slides(0).Shapes(0) as IChart
Dim chartAxis As IChartAxis = chart.PrimaryCategoryAxis
if(chartAxis.HasMultiLvlLbl)
{
    'group labels
    chartAxis.IsMergeSameLabel = True
}                 
ppt.SaveToFile("result.pptx", FileFormat.Pptx2010)