初始化后无法更新 OxyPlot 图 [英] Can't update OxyPlot graph after initialisation
问题描述
我在 XAML 中定义了一个 OxyPlot 图表,如下所示:
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>
在 viewModel 中,我有以下内容:
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));
}
}
}
我可以看到为最初的两个硬编码 DataPoint
绘制的初始图形.
I can see the initial graph drawn for the initial two hardcoded DataPoint
s.
但在一切就绪且 PriceChange()
方法被触发后,新的 DataPoint
不会绘制在图表上.因为它是一个 ObservableCollection
它应该自动通知 UI,不是吗?或者我错过了什么?
But after everything is up and the PriceChange()
method is firing, the new DataPoint
s aren't drawn on the chart. Since it's an ObservableCollection
it should notify the UI automatically, shouldn't it? Or what am I missing?
顺便说一句,我在文档中遵循了这个 example.
BTW I have following this example on the documentation.
推荐答案
虽然 Chart
ObservableCollection 将提供适当的通知,但我认为图表/绘图本身不一定会响应那些通知,所以它很可能不知道它需要重新绘制.
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.
我对 OxyPlot 不熟悉,但我在学习教程后快速入门,并通过对 Plot
类的快速扫描,我发现了一个名为 InvalidatePlot() 的方法
,这似乎迫使绘图自行重绘 - 如果您打算更改绘图数据,很可能需要调用它.当我在一个小型示例项目中尝试它时,它确实有效.
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:
看起来预期的方法很可能是创建一个 PlotModel
并将绘图的 Model
属性绑定到它,然后您可以在 PlotModel
属性时通知 UIcode>PlotModel 变化:
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/
oxyplot.codeplex.com/SourceControl/latest#Source/Examples/WPF/WpfExamples/Examples/RealtimeDemo/
这篇关于初始化后无法更新 OxyPlot 图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!