Aspose.Cells and Charts

Hi all,

I'm trying to set the color of ech slice of a pie chart via following code

bool trueFalse = true;

for (int i = 0; i < excel.Worksheets[0].Charts[0].NSeries.Count; i++)

{

datalabels = excel.Worksheets[0].Charts[0].NSeries[i].DataLabels;

datalabels.IsCategoryNameShown = true;

datalabels.IsValueShown = false;

datalabels.IsPercentageShown = true;

for (int k = 0; k < excel.Worksheets[0].Charts[0].NSeries[i].Points.Count; k++)

{

if (trueFalse)

{

excel.Worksheets[0].Charts[0].NSeries[i].Points[k].Area.BackgroundColor = System.Drawing.Color.Blue;

excel.Worksheets[0].Charts[0].NSeries[i].Points[k].Area.ForegroundColor = System.Drawing.Color.Blue;

}

else

{

excel.Worksheets[0].Charts[0].NSeries[i].Points[k].Area.BackgroundColor = System.Drawing.Color.Gray;

excel.Worksheets[0].Charts[0].NSeries[i].Points[k].Area.ForegroundColor = System.Drawing.Color.Gray;

}

trueFalse = !trueFalse;

}

}

It has no effect and I cannot figure why. Any help on this one would be great.

best regards

Jörg

Hi,

I think, you should use ChartPoint.Area.ForegroundColor property only.

Please see the following code. It loads the source input file, which has a pie chart, then it changes the color of the first chart point.

I have attached both the input and output xlsx files. Please have a look.

Please try this code with the latest version: Aspose.Cells
for .NET v6.0.0.2


C#


Workbook workbook = new Workbook(DIR + “Pie.xlsx”);


Worksheet worksheet = workbook.Worksheets[0];


Chart chart = worksheet.Charts[0];


chart.NSeries[0].Points[0].Area.ForegroundColor = Color.Red;


workbook.Save(DIR + “Pie.out.xlsx”);


Hi,

thank you for your reply. That's a way I used first, the main difference is that you are using xlsx Format and I'm using xls 2003 format. I switched to xslx and it worked. I'm considering to change all my code to use the latest excel file format.

best regards

Jörg

Hi,


If you are setting the fill colors for data points in the series for XLS files and the color is custom oriented (that does not present in the MS Excel 97 standard color palette (that has only 256 colors)), you need to add the colors to the color palette first. See the document for your complete reference:
http://www.aspose.com/documentation/.net-components/aspose.cells-for-.net/colors-and-palette.html


Thank you.

Right, used the sample to make sure this won’t be the problem but it did not solve my prob :frowning:

Hi,

Strangely, I was able to change the color, I tried using some custom color which is a darkish blue color.

Please have a look at the source and output xls files and the screenshot.

C#


string piePath = @“C:\Documents and Settings\Home\Desktop\Pie.xls”;

Workbook workbook = new Workbook(piePath);


Worksheet worksheet = workbook.Worksheets[0];


Chart chart = worksheet.Charts[0];


Color customColor = Color.FromArgb(29, 14, 88); //dark bluish


chart.NSeries[0].Points[2].Area.ForegroundColor = customColor;


workbook.Save(piePath + “.out.xls”, SaveFormat.Excel97To2003);


Screenshot: