We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Get slice's color from a PieChart (automatic color)

Hi !

Is it posible to retrieve a slice’s color in a piechart ?
I mean not a user defined color but an automatic color (set by Excel)
Thanks

Hi,

Thanks for your posting and considering Aspose.Cells.

Currently, you can retrieve the Pie Chart’s Slice Custom Color using the following code.


chart.NSeries[0].Points[0].Area.ForegroundColor

But, when it is Automatic Color i.e ( chart.NSeries[0].Points[0].Area.Formatting is Automatic) then you cannot retrieve Slice color.

We have logged a New Feature Request for this issue in our database. We will look into it and implement this feature if possible. Once, the feature is available or we have some other update for you, we will let you know asap.

This issue has been logged as CELLSNET-42668.

Hi,

Thanks for using Aspose.Cells.

Please try the following code for your needs.

C#

Workbook workbook = new Workbook(FileFormatType.Xlsx);

Worksheet sheet = workbook.Worksheets[0];

//Put data

sheet.Cells[0, 1].PutValue(5);

sheet.Cells[0, 2].PutValue(10);

sheet.Cells[0, 3].PutValue(6);

sheet.Cells[0, 4].PutValue(4);

sheet.Cells[0, 5].PutValue(8);

//Generate the first chart

int chartIndex = sheet.Charts.Add(Aspose.Cells.Charts.ChartType.Pie, 2, 0, 20, 7);

Chart chart = sheet.Charts[chartIndex];

//Insert series

chart.NSeries.Add("B1:F1", false);

chart.ShowLegend = false;

chart.Title.Text = "Test Pie";

Series series = chart.NSeries[0];

int pointsCount = series.Points.Count;

for (int i = 0; i < pointsCount; i++)

{

Color c = series.Points[i].Area.ForegroundColor;

Console.WriteLine(c);

}

chart.Calculate();

Console.WriteLine("After call calculate!");

for (int i = 0; i < pointsCount; i++)

{

Color c = series.Points[i].Area.ForegroundColor;

Console.WriteLine(c);

}

workbook.Save(@"D:\Aspose\User\0785\TestPie.xlsx", Aspose.Cells.SaveFormat.Xlsx);