How to plot MULTIPLE LineSeries on an OxyPlot chart?

前端 未结 3 1675
予麋鹿
予麋鹿 2021-02-07 14:51

I apologize for asking so many OxyPlot questions, but I seem to be really struggling with using the OxyPlot chart control.

My project is in WPF format so I was original

3条回答
  •  醉梦人生
    2021-02-07 15:36

    Sucess!!!!

    AwkwardCoder, thank you for the help, but I realized my mistake was just me having overlooked 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 listPointAray = new List();
    
            // 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 the multiple things I did wrong:

    1. In my foreach var series loop, I was adding the original series which had already been added and NOT the next var series in the list! (dumb!)
    2. I was creating a new model each time I ran the method. This means that each time the code ran, I was adding a series that already existed in the previous model. (also dumb!)
    3. I was creating a new plot every time and trying to add a model in the new plot that already belonged to a previous plot. (getting dummer..)
    4. The plot was being added to the grid each time I ran the method, so I had to CLEAR the grid's children first before re-adding the same plot.
    5. I was not refreshing the plot.

    That was a lot of mistakes, but I worked through it. Hopefully this helps someone in the future. Also, I know I am not using ordinary data binding techniques, but this, at-least, works.

    Final result:

提交回复
热议问题