简体   繁体   中英

Creating Oxyplot PlotModel that automatically updates with new data incoming

I've been following this excellent tutorial found here in order to learn how to create LineSeries plots using OxyPlot while following MVVC. I've also downloaded, and after some error fixing, managed to get the author's source code for the tutorial to compile and run. The source code on Github can be found here .

Unfortunately, while the code runs and the chart is created, the chart doesn't update itself every 5 seconds like it should. This is what the chart looks like on my machine forever. It doesn't update:

在此处输入图片说明

This is what it should look like, more plots should show as time increases: 在此处输入图片说明

Now, I've done some digging of my own to find out why it isn't updating. I've added some debugging commands to find whether new points are added correctly, as below: 在此处输入图片说明

I've checked that UpdateModel gets called every 5 seconds, and that measurements are correctly returning from GetUpdateData, and that the lineSerie.Points count is increasing before the end of the function. InvalidatePlot is also called in the actual View class.

Is there anything else I can do to solve this problem? Thanks

I figured it out. According to this tutorial here (Create The View section), I thought I had to put:

<Window.DataContext>
    <local:MainViewModel/>
</Window.DataContext>

in my XAML file. But according to the tutorial project, you don't need it:

<Window x:Class="OxyPlotDemo.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:oxy="http://oxyplot.org/wpf"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <oxy:PlotView x:Name="Plot1" Model="{Binding PlotModel}" Margin="10" Grid.Row="1">
    </oxy:PlotView>
</Grid>

Removing the local.MainViewModel line fixed the issue. I think it was using the wrong PlotModel, instead of the correct one created in the View class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM