[英]How to add different color of column in chart control in asp.net
我在做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” ...
現在我想根據此值自定義列顏色
如果->紅色
b->藍色
c->像這樣的綠色
如果有人做過這種邏輯,請幫助我。
提前致謝。
您可以通過為以下項設置值來為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)];
// }
//}
#endregion Series sr = new Series(); sr.Name =“ A”; sr.Points.DataBindXY(x,y); sr.ChartType = SeriesChartType.StackedBar; 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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.