Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MS Chart Control: Change the order of series in a legend

Tags:

mschart

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?

like image 657
Jeff Meatball Yang Avatar asked Dec 16 '09 21:12

Jeff Meatball Yang


People also ask

How do I rearrange the legend in Excel?

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.

What is Report legend?

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.

Where is the legend in Excel?

Click Chart Elementsnext to the table. Select the Legend check box. The chart now has a visible legend.


2 Answers

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.

like image 191
Emil Badh Avatar answered Oct 11 '22 06:10

Emil Badh


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

like image 28
Shawn Avatar answered Oct 11 '22 05:10

Shawn