MS Chart Control: reorder series in legend

How to change the order of series in the legend?

The My Line series appears 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 this:

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

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

Any ideas?

+4
source share
3 answers

This is really very strange. It seems to me that the order of the series in the legend should coincide with the order in which you add them, or if you set the LegendItemOrder property of your Legend instance to ReversedSeriesOrder in the reverse order.

+2
source

To expand Emil's answer, the MSDN information for LegendItemOrder reads:

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

I'm not sure, but I assume that it β€œgroups” s2 and s3 and displays them in reverse order, because s1 is the StackedColumn table - s1, (s2, s3) becomes (s2, s3), s1 . (I have no idea what is really going on behind the scenes)

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

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

+1
source
 MSChart1.ShowLegend = True With MSChart1 .Column = 1 .Row = 1 .ColumnLabel = "Series 1" .Data = 100 .Column = 2 .Row = 1 .ColumnLabel = "Series 2" .Data = 100 End With 
0
source

All Articles