简体   繁体   English

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

[英]How to add different color of column in chart control in asp.net

I am doing coding of ASP.NET chart control and chart code is as per below 我在做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>

and i have bind this chart control from code behind as per below 我已经按照下面的代码代码中绑定了此图表控件

    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"; 

Now in dt (datasource) there is also 3rd column named as Severity and values will be "a", "b","c" ... 现在,在dt(数据源)中 ,还有第3列,称为严重性 ,其值将为“ a”,“ b”,“ c” ...

Now i would like to customize column color as per this value, means 现在我想根据此值自定义列颜色

if a -> Red 如果->红色

b -> Blue b->蓝色

c -> green like that c->像这样的绿色

Please help me if any one has done this type of logic. 如果有人做过这种逻辑,请帮助我。

Thanks in advance. 提前致谢。

You can set multiple values for the Y member by setting value for 您可以通过为以下项设置值来为Y成员设置多个值:

Chart1.Series[0].YValuesPerPoint = 2;

Now for Column it would be using only one value but in the other column you could bind a value corresponding to a,b,c as say 1,2,3 and now bind this to the series. 现在,对于Column,它将仅使用一个值,但在另一列中,您可以将与a,b,c对应的值绑定为1,2,3,然后将其绑定至序列。

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

To apply the colors you will have to loop through the points in the series like 要应用颜色,您将需要遍历系列中的点,例如

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 = ...
}

.cs code .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 Series sr = new Series(); #endregion Series sr = new Series(); sr.Name = "A"; sr.Name =“ A”; sr.Points.DataBindXY(x, y); sr.Points.DataBindXY(x,y); sr.ChartType = SeriesChartType.StackedBar; sr.ChartType = SeriesChartType.StackedBar; sr.Font = new System.Drawing.Font("Tahoma", 8, System.Drawing.FontStyle.Bold); sr.Font =新的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;
    }


}
  • List item 项目清单

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM