Microsoft Chart 控制图例项目排序

发布于 2024-09-24 16:47:25 字数 809 浏览 3 评论 0原文

我有一张包含 8 个系列的图表 - 将它们称为 S1 到 S8。它们在图表的系列列表中按顺序排列,并使用自定义图例项 (Legend.CustomItems) 呈现。一切工作正常,除了当图例换行到新行时图例中项目的显示方式似乎存在错误。

我希望这些项目按行显示:

S1 S2 S3 S4
S5 S6 S7 S8

不幸的是,当图例检测到它将需要两行时,它会在水平方向之前垂直填充,如下所示:

S1 S3 S5 S7
S2 S4 S6 S8

有什么方法可以正确排列项目吗?这是控件的错误吗?

var chart = new Chart();
// More chart setup
foreach(var s in chart.Series)
{
    if (simpleLegend) chart.Legends[0].CustomItems.Add(s.Color, s.LegendText);
    else
    {
        var legendItem = new LegendItem();
        // Legend item customization
        chart.Legends[0].CustomItems.Add(legendItem);
    }
}

编辑

为了明确起见,问题在于图例项的布局,而不是顺序。根据图例项的长度,我最终可能会得到以下布局:

S1 S3 S5 S7 S8
S2 S4 S6

I've got a chart with 8 series - call them S1 through S8. They're in order in the chart's list of series, and they're presented using custom legend items (Legend.CustomItems). Everything works fine, except there seems to be a bug with how items are displayed in the legend when the legend wraps around to a new line.

I'd like the items to be displayed in rows:

S1 S2 S3 S4
S5 S6 S7 S8

Unfortunately, it seems like when the legend detects that it's going to take two rows, it fills in vertically before horizontally, like so:

S1 S3 S5 S7
S2 S4 S6 S8

Is there any way to get the items arranged properly? Is this a bug with the controls?

var chart = new Chart();
// More chart setup
foreach(var s in chart.Series)
{
    if (simpleLegend) chart.Legends[0].CustomItems.Add(s.Color, s.LegendText);
    else
    {
        var legendItem = new LegendItem();
        // Legend item customization
        chart.Legends[0].CustomItems.Add(legendItem);
    }
}

EDIT

To make it clear, the issue is with the layout of the legend items, not the order. Depending on the length of the legend items, I may end up with this layout:

S1 S3 S5 S7 S8
S2 S4 S6

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

成熟稳重的好男人 2024-10-01 16:47:25

您可以在 CustomizeLegend 事件中排列它们。

OnCustomizeLegend="Chart1_CustomizeLegend" 添加到图表标记中或将其绑定到后面的代码中。然后创建处理程序:

protected void Chart1_CustomizeLegend(object sender, CustomizeLegendEventArgs e)
{
    //change order of legend items
    var items = e.LegendItems;
    var item = items[1]; //s2
    items.RemoveAt(1);
    items.Insert(2, item);
    item = items[1]; //after removing s2, s3 is now here
    items.RemoveAt(1);
    items.Insert(4, item);
    //etc...
}

或者您可以首先创建一些集合,并通过按所需顺序引用现有图例项目来填充它,然后清除 LegendItems 并立即插入所有项目。我认为你可以用一种对所有项目编号都有效的方式来编写它,但我把它留给你;)。

详细信息:http://msdn.microsoft.com/en-us/library/ dd488245.aspx

(我知道这个问题已经有近 2 年历史了,但也许有同样问题的人(比如今天的我)会发现这很有帮助。)

You can arrange them in CustomizeLegend event.

Add OnCustomizeLegend="Chart1_CustomizeLegend" to your Chart markup or bind it in code behind. Then create handler:

protected void Chart1_CustomizeLegend(object sender, CustomizeLegendEventArgs e)
{
    //change order of legend items
    var items = e.LegendItems;
    var item = items[1]; //s2
    items.RemoveAt(1);
    items.Insert(2, item);
    item = items[1]; //after removing s2, s3 is now here
    items.RemoveAt(1);
    items.Insert(4, item);
    //etc...
}

Or you can create some collection first and fill it by referencing existing legend items in desired order, then clearing LegendItems and inserting all items at once. I think you can write it in a way it will be valid for all items number, but I leave it to you ;).

More info: http://msdn.microsoft.com/en-us/library/dd488245.aspx

(I know this question is almost 2 years old but maybe someone with same problem (like me today) will find this helpful.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文