tags:

views:

332

answers:

2

How do I change the order of series in a legend?

My Line series is appearing before my StackedColumn series, but after my Column series.

Chart c = new Chart();
ChartArea ca = c.ChartAreas.Add("main");
Legend lg = c.Legends.Add("mainLegend");
Series s1 = c.Series.Add("s1");
s1.ChartType = ChartType.StackedColumn;
Series s2 = c.Series.Add("s2");
s2.ChartType = ChartType.Column;
Series s3 = c.Series.Add("s3");
s3.ChartType = ChartType.Line;

Forgive the poor ASCII art, but the legend looks like:

.......[yellow] s2 .......[red] s3 ......[blue] s1 ............

when I would like it to go in order: s1, s2, s3.

Any ideas?

+2  A: 

That seems very strange indeed. To me it seems that the order of the series in the legend should be the same as the order in which you add them, or if you set the LegendItemOrder property of your Legend instance to ReversedSeriesOrder in the reversed order.

Emil Badh
+1  A: 

To expand on Emil's answer, the MSDN info for LegendItemOrder says:

If the LegendItemOrder property is set to Auto, the legend will automatically be reversed if StackedColumn, StackedColumn100, StackedArea or StackedArea100 chart types are used.

I'm not certain, but my guess is that it's "grouping" s2 and s3, and displaying them in reverse because s1 is a StackedColumn chart - s1, (s2, s3) becomes (s2, s3), s1. (I have no idea what actually happens behind the scenes)

As Emil said, setting the LegendItemOrder property to LegendItemOrder.SameAsSeriesOrder or LegendItemOrder.ReversedSeriesOrder should force the legend to display in a certain order.

And here's a link to MSDN that has a bit more information: http://msdn.microsoft.com/en-us/library/system.windows.forms.datavisualization.charting.legend.legenditemorder.aspx

Shawn