![](/img/trans.png)
[英]The line color of zedgraph control in c#.net changes as the background color. How to set it a fix color?
[英]Set color in pie chart in c#.net
我正在嘗試使用從C#.NET中的數據庫中提取的數據創建一個簡單的餅圖。 我使用了以下代碼 -
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);
它顯示具有正確值的餅圖。 但是當我嘗試為香蕉(綠色),芒果(紅色)和蘋果(灰色)分配特定顏色時,它顯示錯誤“索引超出范圍。必須是非負的......”。 任何人都可以給我任何暗示這里有什么問題嗎?
“索引超出范圍......”是因為chart1.Series[0].Points[0]
,特別是.Points[0]
。 不是因為PieColors[0]
。 如果您想進一步使用它們或者想要更改它們的顏色,您應該先添加一些點。 例如 :
int index = chart1.Series[0].Points.AddXY(x, y);
然后你可以這樣做:
chatr1.Series[0].Points[index].Color = PieColors[0]; //or whatever color
在您的情況下,問題是,在嘗試分配Point的顏色后,將點綁定到chart1.Series[0].Points
。 試着改變這個:
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);
至
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];
如果你想改變系列顏色,而不是Point,你可以寫下這樣的東西:
chart1.Series[0].Color = Color.Red; //or any other color, maybe from PieColor
this.CategoryGraphChart.Series [“Categories”]。Points.AddXY(test.ToString()+“(”+ number +“)”,number);
CategoryGraphChart.Palette = ChartColorPalette.None;
CategoryGraphChart.PaletteCustomColors = new Color [] {Color.BlanchedAlmond,Color.Blue,Color.Yellow};
這段代碼可以工作......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.