首页 文章

初始化后无法更新OxyPlot图

提问于
浏览
3

我有一个在我的XAML中定义的OxyPlot图表,如下所示:

<oxy:Plot Height="336">
    <oxy:Plot.Series>
        <oxy:LineSeries ItemsSource="{Binding Chart}"/>
    </oxy:Plot.Series>
</oxy:Plot>

在viewModel中我有以下内容:

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));          
        }
    }
}

我可以看到为最初的两个硬编码DataPoints绘制的初始图形 .

但是在一切都结束并且 PriceChange() 方法被触发后,新的DataPoints不是't drawn on the chart. Since its an onbervableCollection it should notify the UI automatically, isn't?或者我错过了什么?

顺便说一下,我在文档中遵循了这个example .

2 回答

  • 4

    虽然 Chart ObservableCollection将提供适当的通知,但我认为图表/图表本身不一定会响应这些通知,因此可能不知道它需要重新绘制 .

    我对OxyPlot并不熟悉,但是我快速了解了一个教程,并且通过快速扫描 Plot 类,我找到了一个名为 InvalidatePlot() 的方法,这似乎迫使情节重绘自己 - 它很可能是如果您打算更改绘图数据,则需要调用它 . 当我在一个小样本项目中尝试它时,它确实有效 .

    我没有找到大量的示例用法,但这些链接可能会有所帮助:

    http://oxyplot.codeplex.com/discussions/398856

    http://oxyplot.codeplex.com/discussions/352003

    这是第二个链接中提到的示例:

    http://oxyplot.codeplex.com/SourceControl/latest#Source/Examples/WPF/WpfExamples/Examples/CoupledAxesDemo/

    编辑:

    看起来预期的方法可能是创建 PlotModel 并将绘图的 Model 属性绑定到它,然后您可以在 PlotModel 更改时通知UI:

    oxyplot.codeplex.com/SourceControl/latest#Source/Examples/WPF/WpfExamples/Examples/RealtimeDemo/

  • 1

    你可以做:

    <oxy:Plot InvalidateFlag="{Binding DataPoints.Count, Delay=20}">
        <oxy:Plot.Series>
            <oxy:LineSeries ItemsSource="{Binding DataPoints}"/>
        </oxy:Plot.Series>
    </oxy:Plot>
    

    在您的情况下,不需要延迟,但它有时很有用 .

相关问题