Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set series Z-Order in Chart

I have a Chart Control (System.Windows.Form.DataVisualisation.Charting, so WinForms) with multiple series, some are assigned to the primary, and some to the secondary Y-axis.

I need the chart to draw the series in a specific Z-order (meaning which series is drawn first, second, and so on), because some of them are overlapping. I can't find any related property.

I thought the z-order would depend on the order in which the series are added to the SeriesCollection, but this doesn't seem to change anything in my tests.

Am I missing something?

PS: It's not a 3D-Graph. So I am only asking about the order in which the different series are drawn.

like image 211
Jens Avatar asked Dec 11 '22 05:12

Jens


2 Answers

The series are drawn in the order they are placed into the Chart.Series collection. Therefore you can automatically send new series to the back by using an Insert instead of an Add:

myChart1.Series.Add(myNewSeries1); // Draws this series on top of the others.
myChart1.Series.Insert(0, myNewSeries2); // Draw this series behind the others.

The following could be converted to an extension method for the chart control and (along with other methods e.g. BringToFront) could then be used in setting the order of series.

public void SendToBack(Series s)
{
    if (myChart1.Series.Contains(s))
    {
        myChart1.Series.Remove(s);
        myChart1.Series.Insert(0, s);
    }
}
like image 136
Ben Avatar answered Dec 18 '22 08:12

Ben


The series are drawn in the order in which they are added to the Chart.Series collection. Add the one you want drawn on top as the last element in the collection.

like image 34
mmathis Avatar answered Dec 18 '22 07:12

mmathis