OxyPlot for real time data

Nov 28, 2011 at 11:29 AM

I'm using oxyplot to render real time data, i.e. I have data that is updating at regular intervals (right now its once or twice per second). I'm using WPF and I have created my PlotModel in the viewmodel which I then bind to from my XAML

<oxy:Plot Model="{Binding PollutionHistoryModel}" />

I create the plot model during viewmodel construction like so:

PollutionHistoryModel = new PlotModel { PlotMargins = new OxyThickness(-6, 2, 0, -2), AutoAdjustPlotMargins = false, Background = OxyColors.Transparent };
PollutionHistoryModel.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, IsAxisVisible = false, MajorGridlineStyle = LineStyle.Dot });
PollutionHistoryModel.Axes.Add(new LinearAxis { Position = AxisPosition.Left, Minimum = 0, Maximum = 130, IsAxisVisible = false });
PollutionHistoryModel.Annotations.Add(new LineAnnotation { Type = LineAnnotationType.Horizontal, Y = 40, Color = OxyColors.Orange, Text = "Level 1" });
PollutionHistoryModel.Annotations.Add(new LineAnnotation { Type = LineAnnotationType.Horizontal, Y = 120, Color = OxyColors.Crimson, Text = "Level 2" });
PollutionHistoryModel.Series.Add(new LineSeries(OxyColor.FromRGB(0xA8, 0xD6, 0xFF)));

Then in my update method (in the viewmodel) I do (the following gets called periodically):

var series = (LineSeries)PollutionHistoryModel.Series[0];
if (series.Points.Count >= 100)
    series.Points.RemoveAt(0);
series.Points.Add(new DataPoint(ventilation.Iteration, PollutionLevel));
RaisePropertyChanged("PollutionHistoryModel");

But I'm not sure the chart actually updates itself. It seems to do it "sometimes" (???) but it's very unreliable... In fact, the only time I reliably get a redraw/refresh of the chart is when I resize the plot by resizing the parent window which hosts the plot.

I have also tried changing the RaisePropertyChanged call to a call to PollutionHistoryModel.Update(true); but without success.

So my question is: what is the correct approach for using Oxyplot with real time data?

PS. My update method is called on a worker thread if that makes any difference

Coordinator
Nov 28, 2011 at 1:06 PM

Calling RefreshPlot (I think this has to be on the UI thread) on the plot control should cause the plot to update immediately.

Raising the property changed event is not enough if you don't change the PlotModel.
You could set it to null, raise a npc event, then set to the model again and set a npc event. Maybe someone knows a better way to do this?

Coordinator
Nov 28, 2011 at 1:08 PM

(OxyPlot is not listening to property and collection changes inside the PlotModel. I wanted to keep the model as simple as possible.)

Nov 28, 2011 at 2:01 PM

Thanks for the quick response. Indeed, calling RefreshPlot does the trick.

It would be nice if there was a RefreshPlot kind of method on the PlotModel instead of (or in addition to) the plot control. In my case, and I assume most other codebases using MVVM, the viewmodel is creating and updating the model and it feels wrong to have to create a custom notification mechanism from the VM to the view just to tell it to refresh the plot. 

A quick look at the Plot.cs (wpf) control tells me it should be possible to have an event on the PlotModel which the plot control listens to and calls its own RefreshPlot() method. If you think this would be a good approach, I could cook up a patch which adds this functionality

Coordinator
Nov 28, 2011 at 6:53 PM

yes, there could absolutely be a refresh event on the PlotModel. The event args should specify if the data should be refreshed (from ItemSources).  Great if you will create a patch! I am a bit short on time right now. Could you also make sure it works with SL and WinForms?