Set color in pie chart in C # .net
I am trying to create a simple pie chart using data retrieved from a database in C # .NET. I used the following codes:
string[] xAxis = { "Banana", "Mango", "Apple" };
double BananaPercentage= 40;
double MangoPercentage= 30;
double ApplePercentage = 30;
double[] Percentage = { BananaPercentage, MangoPercentage, ApplePercentage };
Color[] PieColors = { Color.Green, Color.Red, Color.Gray };
chart1.Series[0].Label = "#PERCENT";
chart1.Series[0].LegendText = "#AXISLABEL";
//chart1.Series[0].Points[0].Color = PieColors[0];
chart1.Series[0].Points.DataBindXY(xAxis, Percentage);
A pie chart with the correct values ββis displayed here. But when I try to assign a specific color to Banana (Green), Mango (Red) and Apple (Gray), it shows that "the index was out of range" must be non-negative ..... ". Can someone give me any hints what is wrong here?
source to share
"the index was out of range ..." because of chart1.Series[0].Points[0]
, especially .Points[0]
. Not because of PieColors[0]
. You must add a few items earlier if you want to use them further or want to change their colors. For example:
int index = chart1.Series[0].Points.AddXY(x, y);
and then you can do the following:
chatr1.Series[0].Points[index].Color = PieColors[0]; //or whatever color
In your case, the problem is that you snap points to chart1.Series[0].Points
after you try to assign a color to the point. Try to change this:
chart1.Series[0].Label = "#PERCENT";
chart1.Series[0].LegendText = "#AXISLABEL";
chart1.Series[0].Points[0].Color = PieColors[0];
chart1.Series[0].Points.DataBindXY(xAxis, Percentage);
For
chart1.Series[0].Label = "#PERCENT";
chart1.Series[0].LegendText = "#AXISLABEL";
chart1.Series[0].Points.DataBindXY(xAxis, Percentage);
chart1.Series[0].Points[0].Color = PieColors[0];
chart1.Series[0].Points[1].Color = PieColors[1];
chart1.Series[0].Points[2].Color = PieColors[2];
If you want to change the color of the Series and not the Point, you can write something like:
chart1.Series[0].Color = Color.Red; //or any other color, maybe from PieColor
source to share
this.CategoryGraphChart.Series ["Categories"]. Points.AddXY (test.ToString () + "(" + number + ")", number);
CategoryGraphChart.Palette = ChartColorPalette.None;
CategoryGraphChart.PaletteCustomColors = new Color [] {Color .BlanchedAlmond, Color.Blue, Color.Yellow};
This code will work ...
source to share