Stackable LineSeries. Is it possible?

Oystein Bjorke 10 years ago 0
This discussion was imported from CodePlex

vindex wrote at 2013-07-14 18:15:

Hi,
I was wondering if it is possible to stack line series, to get something like this:


Image


I've been looking in the examples, but didn't find any similar...
Thank you

everytimer wrote at 2013-07-14 19:31:

If the LineSeries have the same X spacing you could create a new one and run a "foreach (DataPoint point in ...)" loop, adding the previous LineSeries to the current. If that is not enough and you want to have the same look as in your picture you should create AreaSeries, where the top line is the addition of the desired lines, and the low line is the lowest line.

If you have different X spacing you can't stack the lines point-per-point, you need first generate intermediate points in both lines, and then proceed with the addition.

Good luck

vindex wrote at 2013-07-14 20:11:

Thanks, your AreaSeries solution was exactly what I was looking for.

Image
public IEnumerable<AreaSeries> StackLineSeries(IList<LineSeries> series)
        {
            double[] total = new double[series[0].Points.Count];

            LineSeries lineSeries;
            AreaSeries areaSeries;

            for (int s = 0; s < series.Count; s++)
            {
                lineSeries = series[s];
                areaSeries = new AreaSeries();

                for (int p = 0; p<lineSeries.Points.Count; p++)
                {
                    double x = lineSeries.Points[p].X;
                    double y = lineSeries.Points[p].Y;

                    areaSeries.Points.Add(new DataPoint(x, total[p]));
                    total[p] += y;
                    areaSeries.Points2.Add(new DataPoint(x, total[p]));
                }

                yield return areaSeries;
            }
        }