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?
Click the chart, and then click the Chart Layout tab. To change the position of the legend, under Labels, click Legend, and then click the legend position that you want. To change the format of the legend, under Labels, click Legend, click Legend Options, and then make the format changes that you want.
The chart legend contains descriptions for each category in a paginated report chart. A legend always contains one or more legend items, where each legend item consists of a colored box that represents the series, and a text string that describes the series, as indicated in the following illustration.
Click Chart Elementsnext to the table. Select the Legend check box. The chart now has a visible legend.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With