Microsoft Chart Controls Legend Item Ordering

后端 未结 1 1517
鱼传尺愫
鱼传尺愫 2020-12-21 15:28

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.CustomI

相关标签:
1条回答
  • 2020-12-21 15:34

    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.)

    0 讨论(0)
提交回复
热议问题