【发布时间】:2017-02-05 04:37:04
【问题描述】:
我无法分别更改每个数据点的颜色。
条件是如果数字 10,颜色将为绿色。
代码(我确实尝试使用 foreach 循环然后使用 for 循环,但无济于事..) 请看一下 //Available :
ChartClass.Series.Clear();
BedsBLL get = new BedsBLL();
int A1Available = get.countAvailA1();
int A1Alloted = get.countUnavailA1();
int B1Available = get.countAvailB1();
int B1Alloted = get.countUnavailB1();
int B2Available = get.countAvailB2();
int B2Alloted = get.countUnavailB2();
int C1Available = get.countAvailC1();
int C1Alloted = get.countUnavailC1();
//Available
Series seriesAvail = ChartClass.Series.Add("SeriesAvailable");
seriesAvail.Color = Color.ForestGreen;
seriesAvail.LegendText = "Available Number of Beds";
String[] classArrAvail = { "A1", "B1", "B2", "C1" };
int[] countAvailable = { A1Available, B1Available, B2Available, C1Available };
ChartClass.Series["SeriesAvailable"].Points.DataBindXY(classArrAvail, countAvailable);
ChartClass.Series["SeriesAvailable"].YValuesPerPoint = 2;
foreach (DataPoint pt in ChartClass.Series["SeriesAvailable"].Points)
{
if (pt.XValue <= 10)
{
pt.Color = Color.Red;
}
else if (pt.XValue > 10)
{
pt.Color = Color.ForestGreen;
}
/*for (int i = 0; i < countAvailable.Length; i++)
{
if (countAvailable[i] <= 10)
{
pt.Color = Color.Red;
}
else if (countAvailable[i] > 10)
{
pt.Color = Color.ForestGreen;
}
}*/
}
//Alloted
Series seriesAlloted = ChartClass.Series.Add("SeriesAlloted");
seriesAlloted.Color = Color.Gray;
seriesAlloted.LegendText = "Alloted Number of Beds";
String[] classArrAlloted = { "A1", "B1", "B2", "C1" };
int[] countAlloted = { A1Alloted, B1Alloted, B2Alloted, C1Alloted };
ChartClass.Series["SeriesAlloted"].Points.DataBindXY(classArrAlloted, countAlloted);
设计师:
<asp:Chart ID="ChartClass" runat="server" Height="350px" Width="380px">
<Series>
<asp:Series Name="SeriesAvailable" IsValueShownAsLabel="True" LabelAngle="-90" Font="Microsoft Sans Serif, 12pt" Legend="LegendClass" ChartArea="ChartAreaClass" ChartType="StackedColumn">
<SmartLabelStyle Enabled="false" />
</asp:Series>
<asp:Series Name="SeriesAlloted" IsValueShownAsLabel="True" LabelAngle="-90" Font="Microsoft Sans Serif, 12pt" Legend="LegendClass" ChartArea="ChartAreaClass" ChartType="StackedColumn">
<SmartLabelStyle Enabled="false"/>
</asp:Series>
</Series>
<ChartAreas>
<asp:ChartArea Name="ChartAreaClass">
<AxisX Title="Class">
<MajorGrid Enabled="false" />
</AxisX>
<AxisY Title="Number of Beds">
<MajorGrid Enabled="false" />
</AxisY>
</asp:ChartArea>
</ChartAreas>
<Legends>
<asp:Legend Docking="Bottom" Name="LegendClass"></asp:Legend>
</Legends>
<Titles>
<asp:Title Name="TitleChart" Font="Microsoft Sans Serif, 15pt, style=Bold" Text="Beds Statistics Summary (Class)" Alignment="TopCenter"></asp:Title>
</Titles>
</asp:Chart>
【问题讨论】:
-
我回答了一个类似的问题,看看HERE。
-
但是为什么我需要一个随机的?我的基于一个条件,然后更改满足该条件的数据点的颜色。对不起,我有点迷路了。自从我开始 asp.net 以来,我只做了 2 个图表。@jstreet
-
你不需要随机数。您只需要处理
PreRender事件。但是现在我看到您正在明确添加数据点(而不是来自数据库,就像在我的示例中一样)所以,也许您甚至不需要...。使用调试器确保更改颜色的循环正常工作. -
我的数据点实际上来自带有 Select Count 语句的数据库。但我认为它不会影响。我该怎么做..? @jstreet
-
我实际上尝试使用这个 if else 语句,因为我正在玩专门的 XValues.. 将编辑问题并在@jstreet 中添加代码它实际上将所有数据点更改为红色..
标签: c# asp.net .net charts mschart