facebooktwittermenuarrow-up

GemBox Support Forum

Pie Chart Legend with percentages

I’m making a pie chart in a Presentation. How can I configure the legend to include the pie chart percentages?

I used SetCategoryLabels to make it work.

Hi,

I’m not exactly sure what you’re looking to get, but nevertheless, it seems you were able to solve this.

Anyway, here are some examples that shows how you can get percentage values on the chart area:

var workbook = new ExcelFile();
var worksheet = workbook.Worksheets.Add("Sheet1");

worksheet.Cells["A1"].Value = "Values";
worksheet.Cells["A2"].Value = 1000;
worksheet.Cells["A3"].Value = 2000;
worksheet.Cells["A4"].Value = 3000;
worksheet.Cells["A5"].Value = 4000;

var pieChart = worksheet.Charts.Add<PieChart>("D2", "M25");
pieChart.SelectData(worksheet.Cells.GetSubrange("A1:A5"), true, true);

pieChart.DataLabels.LabelContainsValue = false;
pieChart.DataLabels.LabelContainsPercentage = true;

workbook.Save("Output.xlsx");

Here is the resulting pie chart:

Also, the following will result in the same, but in this case the series values have percentages:

var workbook = new ExcelFile();
var worksheet = workbook.Worksheets.Add("Sheet1");

worksheet.Cells["A1"].Value = "Values";
worksheet.Cells["A2"].Value = 0.1;
worksheet.Cells["A3"].Value = 0.2;
worksheet.Cells["A4"].Value = 0.3;
worksheet.Cells["A5"].Value = 0.4;

worksheet.Columns["A"].Style.NumberFormat = "0%";

var pieChart = worksheet.Charts.Add<PieChart>("D2", "M25");
pieChart.SelectData(worksheet.Cells.GetSubrange("A1:A5"), true, true);

pieChart.DataLabels.LabelPosition = DataLabelPosition.InsideEnd;

workbook.Save("Output.xlsx");

I hope this helps.

Regards,
Mario