【问题标题】:Customize ASP.NET Chart DataPoint Based On Condition根据条件自定义 ASP.NET 图表数据点
【发布时间】: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


【解决方案1】:

您设置颜色的循环不正确。你应该这样做:

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


编辑:为列标签添加 ASPX:

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

【讨论】:

  • 如果我将 ["SeriesAvailable"] 用于 if else 怎么办?还有.. Chart1.Series["SeriesAvailable"].Points.DataBindXY(classArrAvail, countAvailable);谢谢你..
  • 我用 [0] 代替.. 如果我错了,你能纠正我吗? 0 代表您在图表中的设计器中添加的第一个系列。 1 代表您添加的第二个系列.. 依此类推。 @jstreet
  • 您可以保留您的系列名称。我使用[0] 只是因为我没有在我的示例中命名我的系列。
  • 不,YValues 是一个双精度数组。在柱形图中,您只需要YValues[0],但在其他图表类型中,您可能需要 YValues[1]、YValues[2] 等。这仅指数据点,而不是系列。不要混合这两个...:O)
  • 恭喜哥们,很高兴能帮上忙。
猜你喜欢
  • 1970-01-01
  • 2010-11-13
  • 2018-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多