How to build a MULTIPLE LineSeries on an OxyPlot chart?

I apologize for asking so many OxyPlot questions, but it seems to me that I'm really struggling with using the OxyPlot chart control.

My project is in WPF format, so I initially used the hosted WINFORMS diagram, which worked like a charm and did absolutely everything that I needed until I had to overlay the WPF element on top of the hosted winform diagram. Due to the "AirSpace" problem, I could not see the WPF element that I placed on top of the posted chart, no matter what I did. That's when I decided to go with OxyPlot, which still causes me a lot of headache.

Here is my [original question] [2]! what I asked at CodePlex. It seems to me that they don’t really help there, so I'm trying here again.

My question is:

Does anyone know how to apply MULTIPLE LineSeries onto a site ??

My approach so far:

I take an array of c # List and add a new copy of LineSeries, which contains the new data that should be plotted. My code is:

// 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][1]][1] <!-- source: http://i59.tinypic.com/jqs3n9.jpg --> I would like a new line with a new color to be plotted each time I run the method. [1]: https://i.stack.imgur.com/xO0Y0.jpg [2]: https://archive.codeplex.com/?p=oxyplot 
+9
c # charts wpf oxyplot
source share
2 answers

Sucess !!!!

AwkwardCoder, thanks for the help, but I realized that my mistake was only that I missed some things!

Here is the version of the code that works:

  // Make a new plotmodel private PlotModel model = new PlotModel(); // Create the OxyPlot graph for Salt Split private OxyPlot.Wpf.PlotView plot = new OxyPlot.Wpf.PlotView(); // Function to plot data private void plotData(double numWeeks, double startingSS) { List<LineSeries> listPointAray = new List<LineSeries>(); // Initialize new Salt Split class for acess to data variables Salt_Split_Builder calcSS = new Salt_Split_Builder(); calcSS.compute(numWeeks, startingSS, maxDegSS); // 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); // Add Chart Title model.Title = "Salt Split Degradation"; // Add Each series to the foreach (var series in listPointAray) { // Define X-Axis OxyPlot.Axes.LinearAxis 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 OxyPlot.Axes.LinearAxis 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"; //Yaxis.StringFormat = "{0.00} %"; model.Axes.Add(Yaxis); model.Series.Add(series); } // Add the plot to the window plot.Model = model; plot.InvalidatePlot(true); SaltSplitChartGrid.Children.Clear(); SaltSplitChartGrid.Children.Add(plot); } 

Here are a few things I did wrong:

  1. In my foreach var series loop, I added the original series that was already added, NOT the next var series in the list! (Dumb!)
  2. I created a new model every time I ran the method. This means that every time I run the code, I add a series that already existed in the previous model. (also dumb!)
  3. Each time I created a new plot and tried to add a model to the new plot that already belonged to the previous plot. (getting dumber ..)
  4. The graph was added to the grid every time I ran the method, so I had to CLEAR the children of the grid first before adding the same graph again.
  5. I did not update the plot.

It was a lot of mistakes, but I worked on it. Hope this helps someone in the future. In addition, I know that I am not using the usual data binding methods, but that at least works.

Final result: working plot

+4
source share

This is how I created several lines in the OxyPlot diagram before, the key creates a set of DataPoints for each series - roundPoints and linePoints in the following code example, which are then bound to CircleSeries and LineSeries:

 var xAxis = new DateTimeAxis { Position = AxisPosition.Bottom, StringFormat = Constants.MarketData.DisplayDateFormat, Title = "End of Day", IntervalLength = 75, MinorIntervalType = DateTimeIntervalType.Days, IntervalType = DateTimeIntervalType.Days, MajorGridlineStyle = LineStyle.Solid, MinorGridlineStyle = LineStyle.None, }; var yAxis = new LinearAxis { Position = AxisPosition.Left, Title = "Value", MajorGridlineStyle = LineStyle.Solid, MinorGridlineStyle = LineStyle.None }; var plot = new PlotModel(); plot.Axes.Add(xAxis); plot.Axes.Add(yAxis); var circlePoints = new[] { new ScatterPoint(DateTimeAxis.ToDouble(date1), value1), new ScatterPoint(DateTimeAxis.ToDouble(date2), value2), }; var circleSeries = new ScatterSeries { MarkerSize = 7, MarkerType = MarkerType.Circle, ItemsSource = circlePoints }; var linePoints = new[] { new DataPoint(DateTimeAxis.ToDouble(date1), value1), new DataPoint(DateTimeAxis.ToDouble(date2), value2), }; var lineSeries = new LineSeries { StrokeThickness = 2, Color = LineDataPointColor, ItemsSource = linePoints }; plot.Series.Add(circleSeries); plot.Series.Add(lineSeries); 
+7
source share

All Articles