How to add data labels to pivot chart?

I'm writing an application in C# that processes some CSV cost data and saves it as an xlsx file with two sheets: Data and Charts. The CSV data goes into the Data sheet and the application then creates a pivot table and corresponding pivot chart from this data in the Charts sheet. The chart is created alright but i see no option to add data labels to it using XlsIO. The chart is created as follows:

IChartShape pivotChart = chartsSheet.Charts.Add();
pivotChart.PivotSource = pivotTable;
pivotChart.PivotChartType = ExcelChartType.Pie;

I see an option to add or disable legend

pivotChart.HasLegend = false;

which works fine. I have tried adding a series to the chart but the DataPoints are empty:

IChartShape pivotChart = chartsSheet.Charts.Add();
pivotChart.PivotSource = pivotTable;
pivotChart.Series.Add(ExcelChartType.Pie);
pivotChart.Series[0].DataPoints[0].DataLabels.IsFormula = true;

Adding Values and CategoryLabels to the series does not help either since these are references to a pivot table and a standard range.

Is there any way to add data labels to a pivot chart?


1 Reply

KK Konduru Keerthi Konduru Ravichandra Raju Syncfusion Team September 1, 2021 07:28 AM UTC

Hi Jonathan, 

Greetings from Syncfusion. 

We suggest you to use below code snippet to add data labels for pivot chart. 

Existing Code Snippet 
Suggested Code Snippet 
pivotChart.Series[0].DataPoints[0].DataLabels.IsFormula = true; 
pivotChart.Series[0].DataPoints.DefaultDataPoint.DataLabels.IsValue = true; 

Kindly try the suggestion and let us know if this helps. 

Regards, 
Keerthi. 


Loader.
Up arrow icon