Adding multiple instances of a LineSeries to OxyPlot

Jul 24, 2014 at 1:14 PM
Edited Jul 24, 2014 at 1:16 PM
Hello,

I am trying to add multiple LineSeries to my OxyPlot graph. I am using WPF so I decided to switch to OxyPlot. I am not used to the way OxyPlot works as I am used to using Winforms chart controls so please bare with me. I have read the example project code for plotting multiple series on a PlotModel, but this does not seem to be helping me.

My approach so far:

I am taking a c# List array and adding a new copy of the LineSeries that holds new data to be plotted. My code:
        // Function to plot data
        private void plotData(double numWeeks, double startingSS)
        {

            // Initialize new Salt Split class for acess to data variables
            Salt_Split_Builder calcSS = new Salt_Split_Builder();
            calcSS.compute(numWeeks, startingSS, maxDegSS);

            // Create the OxyPlot graph for Salt Split
            OxyPlot.Wpf.PlotView plot = new OxyPlot.Wpf.PlotView();
            
            var model = new PlotModel();

            // Add Chart Title
            model.Title = "Salt Split Degradation";

            // Create new Line Series
            LineSeries linePoints = new LineSeries() { StrokeThickness = 1, MarkerSize = 1, Title = numWeeks.ToString() + " weeks" };


            // Add each point to the new series
            foreach (var point in calcSS.saltSplitCurve)
            {
                DataPoint XYpoint = new DataPoint();
                XYpoint = new DataPoint(point.Key, point.Value * 100);
                linePoints.Format("%", XYpoint.Y);
                linePoints.Points.Add(XYpoint);
            }

            listPointAray.Add(linePoints);


            // Define X-Axis
            var Xaxis = new OxyPlot.Axes.LinearAxis();
            Xaxis.Maximum = numWeeks;
            Xaxis.Minimum = 0;
            Xaxis.Position = OxyPlot.Axes.AxisPosition.Bottom;
            Xaxis.Title = "Number of Weeks";
            model.Axes.Add(Xaxis);

            //Define Y-Axis
            var Yaxis = new OxyPlot.Axes.LinearAxis();
            Yaxis.MajorStep = 15;
            Yaxis.Maximum = calcSS.saltSplitCurve.Last().Value * 100;
            Yaxis.MaximumPadding = 0;
            Yaxis.Minimum = 0;
            Yaxis.MinimumPadding = 0;
            Yaxis.MinorStep = 5;
            Yaxis.Title = "Percent Degradation";
            model.Axes.Add(Yaxis);

            // Add Each series to the
            foreach (var series in listPointAray)
            {
                LineSeries newpoints = new LineSeries();
                newpoints = linePoints;
                model.Series.Add(newpoints);
            }


            // Add the plot to the window
            plot.Model = model;
            SaltSplitChartGrid.Children.Add(plot);
            
        }
My code works the first time I press my "Graph Data" button, but fails on consecutive attempts with the following error:
The element cannot be added, it already belongs to a Plot Model
The following plot is the type of plot I would like to produce (it worked fine using WinForms Chart control):

Image


I would like a new line with a new color to be plotted each time I run the method.
Jul 24, 2014 at 3:04 PM
Anyone with an idea?

...bump
Jul 24, 2014 at 4:17 PM
In my opinion you are doing it wrong, try to follow any example where the model is linked using "Binding". That way you only need to create a LineSeries and then to add it to the Series collection of the PlotModel.
Note that you will need to update the plot using MyModel.RefreshPlot(true); after that.

Good luck!
Jul 24, 2014 at 5:44 PM
Thanks, I'll try to create a DataBinding so I don't have to do all the work from code-Behind. Maybe things will work better, but won't I still have the issue of adding multiple LineSeries to the same PlotModel?
Jul 24, 2014 at 7:47 PM
Edited Jul 24, 2014 at 7:49 PM
For the record, I answered my own question. The answer can be found here:
"http://stackoverflow.com/questions/24938689/how-to-plot-multiple-lineseries-on-an-oxyplot-chart/24942582#24942582"
Marked as answer by amodedude1 on 7/24/2014 at 12:49 PM