I have a a OxyPlot chart definied in my XAML like this:
<oxy:Plot Height="336">
<oxy:Plot.Series>
<oxy:LineSeries ItemsSource="{Binding Chart}"/>
</oxy:Plot.Series>
</oxy:Plot>
In the viewModel I have the following:
public ObservableCollection<DataPoint> Chart { get; private set; }
public MainViewModel()
{
Chart = new ObservableCollection<DataPoint>()
{ new DataPoint(12, 14), new DataPoint(20, 26) };
public void PriceChange(Model[] quotes)
{
for (int i = 0; i < quotes.Length; i++)
{
Chart.Add(new DataPoint(quotes[i].LastTradePrice, i*10));
}
}
}
I can see the initial graph drawn for the initial two hardcoded DataPoints.
But after everything is up and the PriceChange()
method is firing, the new DataPoints aren't drawn on the chart. Since its an onbervableCollection it should notify the UI automatically, isn't it? Or what am I missing?
BTW I have following this example on the documentation.
Although the Chart
ObservableCollection will be providing appropriate notifications, I don't think the chart/plot itself will necessarily be responding to those notifications, so it may well not know it needs to be redrawn.
I'm not familiar with OxyPlot, but I had a quick crack at following a tutorial, and from a quick scan of the Plot
class, I found a method called InvalidatePlot()
, which seems to force the plot to redraw itself - it may well be the case that you need to call that if you're intend on making changes to the plot data. It certainly worked when I tried it in a small sample project.
I didn't find a huge amount of example usage, however these links may help:
http://oxyplot.codeplex.com/discussions/398856
http://oxyplot.codeplex.com/discussions/352003
And this is the example referred to in the second of those links:
Edit:
It looks like the intended approach may well be to create a PlotModel
and bind the Model
property of your plot to it, you can then notify the UI when the PlotModel
changes:
oxyplot.codeplex.com/SourceControl/latest#Source/Examples/WPF/WpfExamples/Examples/RealtimeDemo/
You can do:
<oxy:Plot InvalidateFlag="{Binding DataPoints.Count, Delay=20}">
<oxy:Plot.Series>
<oxy:LineSeries ItemsSource="{Binding DataPoints}"/>
</oxy:Plot.Series>
</oxy:Plot>
In your case the Delay is not needed but it can be useful at times.
来源:https://stackoverflow.com/questions/21914469/cant-update-oxyplot-graph-after-initialisation