Adding multiple instances of a LineSeries to OxyPlot

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

amodedude1 wrote at 2014-07-24 15:14:


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);


            // 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";

            //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";

            // Add Each series to the
            foreach (var series in listPointAray)
                LineSeries newpoints = new LineSeries();
                newpoints = linePoints;

            // Add the plot to the window
            plot.Model = model;
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):


I would like a new line with a new color to be plotted each time I run the method.

amodedude1 wrote at 2014-07-24 17:04:

Anyone with an idea?


everytimer wrote at 2014-07-24 18:17:

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!

amodedude1 wrote at 2014-07-24 19:44:

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?

amodedude1 wrote at 2014-07-24 21:47:

For the record, I answered my own question. The answer can be found here: