Microsoft图表控件图例项目排序
我有一个8系列的图表-称它们为S1到S8.它们在图表的系列列表中是按顺序排列的,并且使用自定义图例项目(Legend.CustomItems)进行显示.一切正常,除了当图例换行到新行时似乎在图例中显示项目的方式似乎有一个错误.
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);
}
}
编辑
为了清楚起见,问题出在图例项的 layout 上,而不是顺序.根据图例项的长度,我可能会得到以下布局:
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
您可以在CustomizeLegend
事件中安排它们.
You can arrange them in CustomizeLegend
event.
将OnCustomizeLegend="Chart1_CustomizeLegend"
添加到您的图表标记中,或将其绑定到后面的代码中.然后创建处理程序:
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...
}
或者您可以先创建一些集合,然后按所需顺序引用现有图例项目,然后清除LegendItems
并一次插入所有项目来填充它.我认为您可以以一种对所有商品均有效的方式来编写它,但我将其留给您;).
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 ;).
更多信息: http://msdn.microsoft.com/zh-CN /library/dd488245.aspx
(我知道这个问题将近2岁了,但也许有相同问题的人(如今天的我)会有所帮助.)