【问题标题】:Create a custom legend in tabular format - ASP.NET Chart以表格格式创建自定义图例 - ASP.NET 图表
【发布时间】:2014-02-03 21:41:01
【问题描述】:

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

我得到了数据,但我得到了所有的 LegendItems。我想在新行上显示每个 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 都与图例出现在同一行。你能帮我解决这个问题吗?非常感谢您的帮助。

【问题讨论】:

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


    【解决方案1】:

    发现一旦我将 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;
                }
    
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-01
      • 1970-01-01
      • 2020-09-11
      • 2021-10-30
      • 1970-01-01
      • 2014-11-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多