C# 根据条件自定义ASP.NET图表数据点

C# 根据条件自定义ASP.NET图表数据点,c#,asp.net,.net,charts,mschart,C#,Asp.net,.net,Charts,Mschart,我无法分别更改每个数据点的颜色 条件是如果数字为10,则颜色将为绿色 代码(我尝试过先使用foreach循环,然后使用for循环,但没有效果) 请只看一下//Available: ChartClass.Series.Clear(); BedsBLL get = new BedsBLL(); int A1Available = get.countAvailA1(); int A1Alloted = get.countUnavail

我无法分别更改每个数据点的颜色

条件是如果数字为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);
ChartClass.Series.Clear();
BEDBBLL get=新BEDBBLL();
int A1Available=get.countAvailA1();
int a1allocated=get.countUnavailA1();
int B1Available=get.countAvailB1();
int b1allocated=get.countUnavailB1();
int B2Available=get.countAvailB2();
int b2allocated=get.countUnavailB2();
int C1Available=get.countAvailC1();
int c1allocated=get.countUnavailC1();
//可用
Series seriesAvail=ChartClass.Series.Add(“SeriesAvailable”);
seriesavil.Color=Color.ForestGreen;
seriesAvail.LegendText=“可用床位数”;
字符串[]classArrAvail={“A1”、“B1”、“B2”、“C1”};
int[]countAvailable={A1Available,B1Available,B2Available,C1Available};
ChartClass.Series[“SeriesAvailable”].Points.DataBindXY(classArrAvail,countAvailable);
ChartClass.Series[“SeriesAvailable”]。YValuesPerPoint=2;
foreach(ChartClass.Series[“SeriesAvailable”].Points中的数据点pt)
{
如果(pt.X值10)
{
颜色=绿色;
}
/*for(int i=0;i

设计师:

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

设置颜色的循环不正确。以下是您应该如何做到这一点:

public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string[] classArrAvail = { "A1", "B1", "B2", "C1" };
        int[] countAvailable = { 20, 5, 10, 15 };

        Chart1.Series["SeriesAvailable"].Points.DataBindXY(classArrAvail, countAvailable);

        foreach (DataPoint pt in Chart1.Series["SeriesAvailable"].Points)
        {
            if (pt.YValues[0] <= 10)
                pt.Color = Color.Red;
            else pt.Color = Color.Green;
        }
    }
}
public分部类WebForm1:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
字符串[]classArrAvail={“A1”、“B1”、“B2”、“C1”};
int[]countAvailable={20,5,10,15};
图表1.系列[“系列可用”].点.数据绑定(classArrAvail,countAvailable);
foreach(图表1.系列[“系列可用”].点中的数据点pt)
{

if(pt.y)值[0]我回答了一个类似的问题,看一看。但是为什么我需要随机的?我的是基于一个条件,然后更改符合该条件的数据点的颜色。对不起,我有点迷路了。自从我启动asp.net以来,我只做了两个图表。@jstreetYou不需要随机的。您只需要处理
PreRender
事件。但是现在我看到您显式地添加了数据点(而不是像我的示例中那样从数据库)所以,也许你甚至不需要这样做……使用调试器来确保改变颜色的循环正常工作。我的数据点实际上来自一个带有Select Count语句的数据库……但我认为它不会影响。我该怎么做呢?@jstreeti实际上尝试过使用这个if-else语句,因为我正在专门玩XV值..将编辑问题并在@jstreet中添加代码它实际上会将所有数据点更改为红色..如果我对if-else使用[“SeriesAvailable”]以及..Chart1.Series[“SeriesAvailable”].points.DataBinxy(ClassArraAvail,countAvailable);谢谢..我使用了[0]而是..如果我错了,你能纠正我吗。0代表你在图表的设计器中添加的第一个系列。1代表你添加的第二个系列。依此类推。@jstreety你可以保留你的系列名称。我使用的是
[0]
只是因为我没有在我的示例中命名我的系列。不,
YValues
是一个双精度数组。在柱状图中,你只需要
YValues[0]
,但在其他图表类型中,你可能需要YValues[1],YValues[2]等等。这仅指数据点,而不是系列。不要将这两个值混合在一起…:O)祝贺你,很乐意帮助。
    <asp:Chart ID="Chart1" runat="server" Height="400px" Width="600px">
        <series>
            <asp:Series Name="SeriesAvailable" Font="Microsoft Sans Serif, 12pt" IsValueShownAsLabel="True" LabelAngle="-90">
                <SmartLabelStyle Enabled="False" />
            </asp:Series>
        </series>
        <chartareas>
            <asp:ChartArea Name="ChartArea1">
                <AxisX>
                    <MajorGrid Enabled="False" />
                </AxisX>
            </asp:ChartArea>
        </chartareas>
    </asp:Chart>