如何更改图表控件中各列的颜色?

时间:2013-09-24 11:49:33

标签: c# asp.net

我有一个ascx页面,代码背后的代码如下所示

private void CreateProjectsByRAG()
    {
        foreach (ProjectStatistic PR in repository.GetProjectByRAG())
        {
            ProjectsByProjectTypePieChart.Series["series"].Points.AddXY(PR.Name, PR.Value);
        }

        ProjectsByProjectTypePieChart.Titles["Title1"].Text = "Projects by RAG Status";
        ProjectsByProjectTypePieChart.Series["series"].ChartType = SeriesChartType.Column;// Set the Pie width
        ProjectsByProjectTypePieChart.Series["series"].Color = System.Drawing.Color.FromArgb(255, 0, 0);
    }

生成一个看起来像这样的图表

enter image description here

但我想单独为列着色,使它们看起来像这样,我也希望列与其标签具有相同的颜色(因此不是随机的)。

enter image description here

另外,如何订购列?所以我可以按照Green,Amber,Red的顺序排列它们

1 个答案:

答案 0 :(得分:5)

代码是

foreach (ProjectStatistic PR in repository.GetProjectByRAG())
        {
            int index = ProjectsByProjectTypePieChart.Series["series"].Points.AddXY(PR.Name, PR.Value);
            if (PR.Name == "Green")
            { 
                ProjectsByProjectTypePieChart.Series["series"].Points[index].Color = System.Drawing.Color.Green;
            }

            if (PR.Name == "Amber")
            {
                ProjectsByProjectTypePieChart.Series["series"].Points[index].Color = System.Drawing.Color.Yellow;
            }

            if (PR.Name == "Red")
            {
                ProjectsByProjectTypePieChart.Series["series"].Points[index].Color = System.Drawing.Color.Red;
            }

        }