2011-06-22 64 views
0

任何人都可以看到我可以在ASP.Net圖表控件中使用自定義顏色。例如,我有兩個系列TOTAL和TARGET,我想將總色設爲綠色,目標色爲紅色。asp.net c#如何在圖表中使用自定義顏色

+0

我學到了問題,比如這有時會在微軟的論壇上更好的答案。 –

回答

1

事情是這樣的:

Color[] colors = new Color[] { Color.Green, Color.Red }; 
foreach (Series series in Chart1.Series) 
{ 
    foreach (DataPoint point in series.Points) 
    { 
     point.LabelBackColor = colors[series.Points.IndexOf(point)]; 
    } 
} 
0

System.Web.UI.DataVisualization.Charting.Series對象有一個Color屬性。只需將顏色屬性設置爲您所需的顏色屬性即可。這裏有個代碼示例,增加了一個給定值的水平線的圖表:

public static System.Web.UI.DataVisualization.Charting.Chart addLineToChart(
    System.Web.UI.DataVisualization.Charting.Chart pChart, double pValue, System.Drawing.Color pColor) 
{   
    // I will declare a new series where every value is the value passed in 
    System.Web.UI.DataVisualization.Charting.Series constantLineSeries = new System.Web.UI.DataVisualization.Charting.Series(); 
    constantLineSeries.ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Line; 
    constantLineSeries.BorderWidth = ChartingValues.CHART_LINE_THICKNESS; 
    constantLineSeries.Color = pColor; 
    // At each point in the old series, add a constant point in the new series. 
    foreach(System.Web.UI.DataVisualization.Charting.DataPoint point in pChart.Series[0].Points) 
    { 
     System.Web.UI.DataVisualization.Charting.DataPoint constantLinePoint = new System.Web.UI.DataVisualization.Charting.DataPoint(); 
     constantLinePoint.XValue = point.XValue;    
     constantLinePoint.YValues = new double[] { pValue }; 
     constantLineSeries.Points.Add(constantLinePoint); 
    } 
    pChart.Series.Add(constantLineSeries); 
    pChart.ChartAreas[0].Area3DStyle.Enable3D = false; 

    return pChart; 
} 

此代碼示例進行調整以一些其他的屬性,你可能會發現有用的。

1

IV做過類似

Chart2.Series.Add(new Series("Target") 
    { 
     ChartType = SeriesChartType.Column, 
     Color = Color.Red, 
    }); 
+1

+1:我喜歡你在'new'語句中設置顏色的方式。它看起來比我使用的方法更優雅。 –

1

假設你有你的系列一兩分。 所以,你可以自定義的顏色如下:

Chart1.Series["Series1"].Points[0].Color=Color.Red; 
Chart1.Series["Series1"].Points[1].Color=Color.Yellow; 
相關問題