Format Data Labels of Series Chart in Presentation in C#, VB.NET

By default, Microsoft PowerPoint automatically hides the data labels when we create a series chart on presentation slide. In order to make your readers have an intuitive understanding of your chart, you can choose to set formatting of label to display series name, category name, value, percentage and adjust its displayed position. In this article, I would like to show you how to format data labels in PowerPoint presentation via Spire.Presentation.

In the class of Spire.Presentation.Charts.ChartDataLabel, it contains properties like LabelValueVisible, PercentageVisible, SeriesNameVisible, CategoryNameVisible, Position and etc , which will enable us to easily manage the data labels formatting as you desired. Look at the pie chart below, it is not that informative if it doesn’t display data labels.

Format Data Labels of Series Chart in Presentation in C#, VB.NET

Now, let's format pie chart to display percentages in data labels with following code snippet:

Step 1: Create a new instance of Presentation class and load test the file that contains the pie chart.

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

Step 2: Get the chart from presentation slide.

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

Step 3: Get chart's series.

ChartSeriesFormatCollection sers = chart.Series;

Step 4: Set the position of legend on chart.

chart.ChartLegend.Position = ChartLegendPositionType.TopRight;

Step 5: Initialize four instance of series label and set parameters of each label.

ChartDataLabel cd1 = sers[0].DataLabels.Add();
cd1.PercentageVisible = true;
cd1.Position = ChartDataLabelPosition.Center;
            
ChartDataLabel cd2 = sers[0].DataLabels.Add();
cd2.PercentageVisible = true;
cd2.Position = ChartDataLabelPosition.Center;

ChartDataLabel cd3 = sers[0].DataLabels.Add();
cd3.PercentageVisible = true;
cd3.Position = ChartDataLabelPosition.Center;

ChartDataLabel cd4 = sers[0].DataLabels.Add();
cd4.PercentageVisible = true;
cd4.Position = ChartDataLabelPosition.Center;

Step 6: Save the changes to a new .pptx file.

ppt.SaveToFile("Result.pptx", FileFormat.Pptx2007);

Result:

Format Data Labels of Series Chart in Presentation in C#, VB.NET

Full Code:

[C#]
Presentation ppt = new Presentation();
ppt.LoadFromFile("Test.pptx");

IChart chart = ppt.Slides[0].Shapes[0] as IChart;
ChartSeriesFormatCollection sers = chart.Series;
chart.ChartLegend.Position = ChartLegendPositionType.TopRight;

ChartDataLabel cd1 = sers[0].DataLabels.Add();
cd1.PercentageVisible = true;
cd1.Position = ChartDataLabelPosition.Center;
ChartDataLabel cd2 = sers[0].DataLabels.Add();
cd2.PercentageVisible = true;
cd2.Position = ChartDataLabelPosition.Center;
ChartDataLabel cd3 = sers[0].DataLabels.Add();
cd3.PercentageVisible = true;
cd3.Position = ChartDataLabelPosition.Center;
ChartDataLabel cd4 = sers[0].DataLabels.Add();
cd4.PercentageVisible = true;
cd4.Position = ChartDataLabelPosition.Center;

ppt.SaveToFile("Result.pptx", FileFormat.Pptx2007);
[VB.NET]
Presentation ppt = new Presentation();
ppt.LoadFromFile("Test.pptx");

IChart chart = ppt.Slides[0].Shapes[0] as IChart;
ChartSeriesFormatCollection sers = chart.Series;
chart.ChartLegend.Position = ChartLegendPositionType.TopRight;

ChartDataLabel cd1 = sers[0].DataLabels.Add();
cd1.PercentageVisible = true;
cd1.Position = ChartDataLabelPosition.Center;
ChartDataLabel cd2 = sers[0].DataLabels.Add();
cd2.PercentageVisible = true;
cd2.Position = ChartDataLabelPosition.Center;
ChartDataLabel cd3 = sers[0].DataLabels.Add();
cd3.PercentageVisible = true;
cd3.Position = ChartDataLabelPosition.Center;
ChartDataLabel cd4 = sers[0].DataLabels.Add();
cd4.PercentageVisible = true;
cd4.Position = ChartDataLabelPosition.Center;

ppt.SaveToFile("Result.pptx", FileFormat.Pptx2007);