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

本文关键字:添加 颜色 控件 asp net | 更新日期: 2023-09-27 18:17:52

我正在做的编码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(数据源)还有第三列命名为严重性和值将是"a", "b","c"…

现在我想根据这个值自定义列颜色,意思是

如果a ->红色

b ->蓝色

c ->像那样的绿色

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

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

通过为

设置value,可以为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 = ...
}

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)];
    //    }
    //}

# endregionSeries sr = new Series();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;
    }

}
    列表项