如何在asp.net中的图表控件中添加不同颜色的列

时间:2011-12-15 11:55:10

标签: c# asp.net charts

我正在编写 ASP.NET图表控件,图表代码如下

<asp:Chart ID="Chart1" runat="server" Width="450px" Height="200px" BackColor="211, 223, 240"
                Palette="None" BorderLineStyle="Solid" BackGradientEndColor="White" BackGradientType="TopBottom"
                BorderlineWidth="2" BorderlineColor="26, 59, 105" EnableViewState="True">
                <Series>
                    <asp:Series Name="Series1" BorderColor="180, 26, 59, 105" Color="Blue" BorderWidth="2"
                        ShadowColor="254, 0, 0, 0" ChartType="Column" ShadowOffset="1" MarkerSize="8" MarkerStyle="Diamond">
                        <EmptyPointStyle BackGradientStyle="Center" />
                    </asp:Series>                         
                <ChartAreas>
                    <asp:ChartArea Name="ChartArea" BorderColor="64, 64, 64, 64" BorderDashStyle="Solid"
                        BackSecondaryColor="White" BackColor="64, 165, 191, 228" ShadowColor="Transparent"
                        BackGradientStyle="TopBottom">
                        <AxisY LineColor="#eb9c28">
                            <MajorGrid LineColor="64, 64, 64, 64"></MajorGrid>
                        </AxisY>
                        <AxisX LineColor="64, 64, 64, 64" TextOrientation="Horizontal" IsStartedFromZero="true">
                            <LabelStyle Format="dd/MM/yyyy" IntervalType="Days" Interval="1"></LabelStyle>
                            <MajorGrid LineColor="64, 64, 64, 64"></MajorGrid>
                        </AxisX>
                    </asp:ChartArea>
                </ChartAreas>
                <BorderSkin SkinStyle="Emboss" />
            </asp:Chart>

我已将代码隐藏的图表控件绑定到

    Chart1.DataSource = dt;
    Chart1.Series["Series1"].XValueMember = "UTC";
    Chart1.Series["Series1"].YValueMembers = "Value";           

    Chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Minutes;
    Chart1.ChartAreas[0].AxisX.Interval = 5;
    Chart1.ChartAreas[0].AxisX.LabelStyle.Format = "HH:mm:ss"; 

现在在 dt(数据源)中,还有第3列命名为严重性,值将为“a”,“b”,“c”... < / p>

现在我想根据此值自定义列颜色,意味着

  

如果a - &gt;红色

     

b - &gt;蓝色

     

c - &gt;那样的绿色

如果有人做过这种逻辑,请帮助我。

提前致谢。

2 个答案:

答案 0 :(得分:1)

您可以通过设置

的值为Y成员设置多个值
Chart1.Series[0].YValuesPerPoint = 2;

现在对于Column,它只使用一个值,但在另一列中,您可以绑定对应于a,b,c的值,如1,2,3,现在将其绑定到该系列。

 Chart1.Series["Series1"].YValueMembers = "Value,intvalueforcolor";

要应用颜色,您必须循环遍历系列中的点,如

foreach (DataPoint pt in Chart1.Series[0].Points)
{
     pt.YValues[1] // this will be your value depending upon which you could set the color
     //pt.Color = ...
}

答案 1 :(得分:0)

                                                                                                                                                                                                                                                                                                                                                                                                                                                        

.cs代码

public void DuesChartLoad()
{
    string prjid = Request.QueryString[0].ToString();
    DataSet ds = objbug.GetBugDetails(prjid);

    ViewState["Dues"] = ds;
    DateTime now = DateTime.Now;

    DataTable dt = ds.Tables[0];

    string[] x = new string[dt.Rows.Count];
    int[] y = new int[dt.Rows.Count];
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        x[i] = dt.Rows[i][0].ToString();
        y[i] = Convert.ToInt32(dt.Rows[i][1]);

    }

     DuesChart.Series[0].Points.DataBindXY(x, y);
     DuesChart.Series[0].Points.DataBindXY(x, y);
   //  DuesChart.Series[0].Color=System.Drawing.Color.Red;


     DuesChart.Series[0].BorderWidth = 10;
    DuesChart.Series[0].Points.DataBindXY(x,y);
    #region cannot working
    //Color[] colors = new Color[] { Color.Red, Color.Green, Color.Wheat, Color.Gray, Color.AliceBlue,Color.Pink,Color.Black,Color.Beige };
    //foreach (Series series in DuesChart.Series)
    //{
    //    foreach (DataPoint point in series.Points)
    //    {
    //        //Set color for the bar
    //        point.LabelBackColor = colors[series.Points.IndexOf(point)];
    //    }
    //}

#endregion         系列sr =新系列();         sr.Name =“A”;         sr.Points.DataBindXY(x,y);         sr.ChartType = SeriesChartType.StackedBar;         sr.Font = new System.Drawing.Font(“Tahoma”,8,System.Drawing.FontStyle.Bold);

    for (int i = 0; i < x.Length; i++) //xValues.Lenght = 4 in this case where you have 4 Data number
    {
        if (i == 0) // Don't forget xValues[0] is Data4 in your case
            DuesChart.Series[0].Points[i].Color = Color.Blue;
        if (i == 1)
            DuesChart.Series[0].Points[i].Color = Color.Yellow;
        if (i == 2)
            DuesChart.Series[0].Points[i].Color = Color.Violet;
        if (i == 3)
            DuesChart.Series[0].Points[i].Color = Color.SkyBlue;
        if (i == 4)
            DuesChart.Series[0].Points[i].Color = Color.Orange;
        if (i == 5)
            DuesChart.Series[0].Points[i].Color = Color.Green;
        if (i == 6)
            DuesChart.Series[0].Points[i].Color = Color.Pink;
        if (i == 7)
            DuesChart.Series[0].Points[i].Color = Color.Purple;
    }


}
  • 列表项