Microsoft Chart Chart Controls Control

I have a chart with 8 series - name them S1 - S8. They are listed in the chart series, and they are presented using custom legend elements (Legend.CustomItems). Everything works fine, except that there seems to be an error in how the elements are displayed in the legend when the legend wraps around a new line.

I would like the elements to appear in the lines:

S1 S2 S3 S4 S5 S6 S7 S8 

Unfortunately, it seems that when a legend discovers that it will occupy two lines, it is filled vertically to the horizontal, for example:

 S1 S3 S5 S7 S2 S4 S6 S8 

Is there a way to arrange items correctly? 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 problem is related to the location of the elements of the legend, and not to order. Depending on the length of the legend elements, I can finish this layout:

 S1 S3 S5 S7 S8 S2 S4 S6 
+2
source share
1 answer

You can organize them in the CustomizeLegend event.

Add OnCustomizeLegend="Chart1_CustomizeLegend" to your chart OnCustomizeLegend="Chart1_CustomizeLegend" or anchor it in the code behind. Then create a 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 first create some collection and populate it by specifying existing legend elements in the desired order, then clearing LegendItems and inserting all the elements at once. I think you can write it in such a way that it is valid for all numbers, but I leave it to you;).

Additional information: http://msdn.microsoft.com/en-us/library/dd488245.aspx

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

+4
source

All Articles