使用颜色指示器ASP.NET图表添加自定义图例

时间:2017-02-05 14:10:53

标签: c# asp.net .net data-visualization

直截了当..请问如何在图表底部创建自定义图例?

传奇将是...... 红色低床号

设计师代码:

                <asp:Chart ID="ChartClass" Visible="false" runat="server" Height="500px" Width="720px"> 
                <Series>
                    <asp:Series Name="SeriesAvailableClass" IsValueShownAsLabel="True" LabelAngle="-90" Font="Microsoft Sans Serif, 12pt" Legend="LegendClass" ChartArea="ChartAreaClass" ChartType="Column">
                        <SmartLabelStyle Enabled="false" />
                    </asp:Series>
                    <asp:Series Name="SeriesAllotedClass" IsValueShownAsLabel="True" LabelAngle="-90" Font="Microsoft Sans Serif, 12pt" Legend="LegendClass" ChartArea="ChartAreaClass" ChartType="Column">
                        <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>

代码后端:

        Series seriesAvail = ChartClass.Series.Add("SeriesAvailableClass");
        seriesAvail.Color = Color.ForestGreen;
        seriesAvail.LegendText = "Available Number of Beds";
        seriesAvail.IsValueShownAsLabel = true;
        seriesAvail.LabelAngle = 0;
        seriesAvail.Font = new Font("Microsoft Sans Serif", 8);
        seriesAvail.SmartLabelStyle.Enabled = false;


        String[] classArrAvail = { "A1", "B1", "B2", "C1" };
        int[] countAvailable = { A1Available, B1Available, B2Available, C1Available };

        ChartClass.Series["SeriesAvailableClass"].Points.DataBindXY(classArrAvail, countAvailable);

        foreach (DataPoint pt in ChartClass.Series["SeriesAvailableClass"].Points)
        {
            if (pt.YValues[0] < 10)
            {
                pt.Color = Color.Red;
            }
            else if (pt.YValues[0] >= 10)
            {
                pt.Color = Color.ForestGreen;
            }

        }


        Series seriesAlloted = ChartClass.Series.Add("SeriesAllotedClass");
        seriesAlloted.Color = Color.SkyBlue;
        seriesAlloted.LegendText = "Alloted Number of Beds";
        seriesAlloted.IsValueShownAsLabel = true;
        seriesAlloted.LabelAngle = 0;
        seriesAlloted.Font = new Font("Microsoft Sans Serif", 8);
        seriesAlloted.SmartLabelStyle.Enabled = true;

        String[] classArrAlloted = { "A1", "B1", "B2", "C1" };
        int[] countAlloted = { A1Alloted, B1Alloted, B2Alloted, C1Alloted };

        ChartClass.Series["SeriesAllotedClass"].Points.DataBindXY(classArrAlloted, countAlloted);

图像:

enter image description here

我尝试在网上查找和搜索。似乎无法找到我正在寻找的任何可靠来源或解决方案。

大多数解决方案都添加了特定系列的图例。

我对ms-chart相当新......

感谢任何帮助。谢谢..

1 个答案:

答案 0 :(得分:3)

在构建系列后添加以下代码:

    LegendItem item1 = new LegendItem();
    item1.ImageStyle = LegendImageStyle.Rectangle;
    item1.Color = Color.Red;
    item1.BorderColor = Color.Red;
    item1.Cells.Add(LegendCellType.SeriesSymbol, "", ContentAlignment.MiddleCenter);
    item1.Cells.Add(LegendCellType.Text, "Low Bed Number", ContentAlignment.MiddleLeft);
    ChartClass.Legends[0].CustomItems.Add(item1);

enter image description here