0

I am trying to plot my data using OxyPlot. I want to plot two lines in my graph but I am not sure how to provide the plot with data. This is how my XAML file looks like:

<oxy:Plot Name="Plot1">
  <oxy:Plot.Series>
    <oxy:LineSeries ItemsSource="{Binding Graph1}"/>
    <oxy:LineSeries ItemsSource="{Binding Graph2}"/>
  </oxy:Plot.Series>
</oxy:Plot>

My question is how can I plot two lines in the same graph while using LineSeries?

kristian
  • 27
  • 9

1 Answers1

2

Usually you don't add the LineSeries directly to a PlotView but to a PlotModel which is then bound to the Plot View.

The C# code could look like this:

        PlotModel pm = new PlotModel();

        var s1 = new LineSeries();

        for (int i = 0; i < 1000; i++)
        {
            double x = Math.PI * 10 * i / (1000 - 1);
            s1.Points.Add(new DataPoint(x, Math.Sin(x)));
        }

        pm.Series.Add(s1);

        var s2 = new LineSeries();

        for (int i = 0; i < 1000; i++)
        {
            double x = Math.PI * 10 * i / (1000 - 1);
            s2.Points.Add(new DataPoint(x, Math.Cos(x)));
        }

        pm.Series.Add(s2);

        Plot1.Model = pm; 

The binding to Plot1 can of course also be done in XAML. If your DataContext provides the PlotModel via a property 'MyModel', it would look like this:

<oxyplot:PlotView Model="{Binding MyModel}"></oxyplot:PlotView>
Döharrrck
  • 687
  • 1
  • 4
  • 15
  • Thanks for clearing this up. I have another issue when plotting line parallel to the Y-Axis with log Axis: `var logAxisX = new LogarithmicAxis() { Position = AxisPosition.Bottom, Title = "I (A) - log", UseSuperExponentialFormat = false, Base = 10 }; var logAxisY = new LogarithmicAxis() { Position = AxisPosition.Left, Title = "Time (s) - log", UseSuperExponentialFormat = false, Base = 10 }; pm.Axes.Add(logAxisX); pm.Axes.Add(logAxisY);` So far everything appears to be fine but the line is never shown. The points: `s2.Points.Add(new DataPoint(n, 0)); s2.Points.Add(new DataPoint(n, n));` – kristian Aug 25 '20 at 10:22
  • 1
    I guess that's because the first point would be at negative infinity in y if drawn on a logarithmic axis. – Döharrrck Aug 25 '20 at 11:26
  • That solved it, the first point was the problem. I used a new _point_ really close to _zero_ to replace it with. – kristian Aug 26 '20 at 08:11