以表格格式创建自定义图例 - ASP.NET图表

时间:2014-02-03 21:41:02

标签: asp.net charts mschart asp.net-charts

我是ASP.NET Charting的新手,并且对将自定义组件添加到条形图有疑问。我试图以表格格式创建自定义图例。我的意思是我的传奇风格是桌子。我正在从数据库值创建每个LegendItem并将其添加到chart.Legends [0] .CustomItems集合。

我获取了数据,但我将所有LegendItem放在一行中。我想在新行上显示每个LegendItem。我目前的代码看起来像这样 -

chart.Legends.Add(new Legend
{
LegendStyle = LegendStyle.Table,
BorderColor = Color.Black,
BorderWidth = 1,
BorderDashStyle = ChartDashStyle.Solid,
Alignment = StringAlignment.Center,
DockedToChartArea = areaCounter.ToString(),
Docking = Docking.Bottom,
Name = "CustomLegend",
IsTextAutoFit = true,
InterlacedRows = true,
TableStyle = LegendTableStyle.Auto,
IsDockedInsideChartArea = false
});

LegendItem newItem = new LegendItem();
newItem.Cells.Add(LegendCellType.Text, " - value1 - ", ContentAlignment.MiddleCenter);
newItem.Cells.Add(LegendCellType.Text, " - State Average = - ", ContentAlignment.MiddleCenter);
newItem.Cells[1].CellSpan = 2;
newItem.BorderColor = Color.Black;
newItem.Cells.Add(LegendCellType.Text, " - ", ContentAlignment.MiddleCenter);
newItem.Cells.Add(LegendCellType.Text, " - top - ", ContentAlignment.MiddleCenter);
chart.Legends[1].CustomItems.Add(newItem);



LegendItem newItem1 = new LegendItem();
newItem1.Cells.Add(LegendCellType.Text, "value1", ContentAlignment.MiddleCenter);
newItem1.Cells.Add(LegendCellType.Text, "State Average =", ContentAlignment.MiddleCenter);
newItem1.Cells[1].CellSpan = 2;
newItem1.BorderColor = Color.Black;
newItem1.Cells.Add(LegendCellType.Text, "", ContentAlignment.MiddleCenter);
newItem1.Cells.Add(LegendCellType.Text, "top", ContentAlignment.MiddleCenter);
chart.Legends[1].CustomItems.Add(newItem1);

newItem和newItem1都与图例显示在同一行。你能帮我解决这个问题吗?我非常感谢你的帮助。

1 个答案:

答案 0 :(得分:4)

发现一旦我将HeaderSeparator添加到我的自定义Legend对象并处理聊天对象的CustomizeLegend事件,它就按我的意愿工作。自定义项目显示在单独的行上。以下是我所做的更改。

chart.Legends.Add(new Legend
                {
                    LegendStyle = LegendStyle.Table,
                    BorderColor = Color.Black,
                    BorderWidth = 1,
                    BorderDashStyle = ChartDashStyle.Solid,
                    Alignment = StringAlignment.Center,
                    DockedToChartArea = areaCounter.ToString(),
                    Docking = Docking.Bottom,
                    Name = "CustomLegend",
                    IsTextAutoFit = true,
                    InterlacedRows = true,
                    TableStyle = LegendTableStyle.Tall,
                    HeaderSeparator = LegendSeparatorStyle.Line,
                    HeaderSeparatorColor = Color.Gray,
                    IsDockedInsideChartArea = false
                });

                LegendItem newItem3 = new LegendItem();
                var strVal = item.Value;
                foreach (var val in strVal)
                {
                    newItem3.Cells.Add(LegendCellType.Text, val, ContentAlignment.BottomCenter);
                }
                chart.Legends["CustomLegend"].CustomItems.Add(newItem3);

                chart.CustomizeLegend += chart_CustomizeLegend;



        void chart_CustomizeLegend(object sender, CustomizeLegendEventArgs e)
        {
            Chart chart = sender as Chart;
            if (chart == null) return;
            foreach (var item in e.LegendItems)
            {
                item.SeparatorType = LegendSeparatorStyle.Line;
                item.SeparatorColor = Color.Black;
            }

        }