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

问题描述 投票:2回答:1

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

传说将是..红色低床号码

设计师代码:

                <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相当新...

感谢任何帮助。谢谢..

c# asp.net .net data-visualization
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.