如何刷新我用来显示一些数据的WPF图表(WPF工具包图表)

发布于 2024-09-25 13:33:13 字数 991 浏览 1 评论 0原文

当集合发生变化时如何更新 WPF 图表?我正在使用 WPF 图表(System.Windows.Controls.DataVisualization.Toolkit.dllversion 版本 3.5.50211.1)来绘制一些简单的数据。数据的类如下所示:

public class EngineMeasurementCollection : Collection<EngineMeasurement> 
{ 

} 
public class EngineMeasurement 
{ 
    public int TimeStamp { get; set;}
    public int Speed { get; set; } 
    public int Torque { get; set; } 
    public int Power { get; set; } 
}  

在我的 Mainform 中,我正在接收有关事件回调的新数据。这又回到了另一个线程。我查看数据以查看它是否有新的时间戳。如果是的话,我会将其添加到我的收藏中。然后我尽职尽责地打电话

Dispatcher.Invoke(myDelegate, DispatcherPriority.Normal,null) 

让自己回到 GUI 线程。然后我调用:

m_ctrlLineSeriesTorque.ItemsSource = m_TorqueCollection; 

尝试更新图表。我已经验证我进入了这段代码。我还验证了如果我只是将一些值放入 EngineMeasurementCollection 的构造函数中,就可以显示图表。当我向集合中添加更多值时,如何更新图表?

我在某个地方看到图表本身可能有一个“刷新”方法。我不这么认为。另外,我看到也许 EngineMeasurementCollection 应该是一个“Observable 集合”,并且 EngineMeasurement 应该实现一些接口。真的?

谢谢, 戴夫

How do you update a WPF chart when the collection changes? I am using WPF chart (System.Windows.Controls.DataVisualization.Toolkit.dllversion version 3.5.50211.1) to plot some simple data. The classes for the data look like this:

public class EngineMeasurementCollection : Collection<EngineMeasurement> 
{ 

} 
public class EngineMeasurement 
{ 
    public int TimeStamp { get; set;}
    public int Speed { get; set; } 
    public int Torque { get; set; } 
    public int Power { get; set; } 
}  

In my Mainform, I am receiving new data on an event callback. This comes back on another thread. I look at the data to see if it has a new TimeStamp. If it is, I add it to my collection. I then dutifully call

Dispatcher.Invoke(myDelegate, DispatcherPriority.Normal,null) 

to get myself back on the GUI thread. I then call:

m_ctrlLineSeriesTorque.ItemsSource = m_TorqueCollection; 

to try and get the chart to update. I've verified I get into this code. I've also verified that I can display a chart if I just throw some values into the contructor of EngineMeasurementCollection. How do I get the chart to update as I add more values to the collection?

Somewhere I saw that there might be a "Refresh" method on the chart itself. I don't see that. Also, I saw that perhaps EngineMeasurementCollection should be an "Observable collection" and EngineMeasurement should implement some interface. True?

Thanks,
Dave

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿忘心安 2024-10-02 13:33:13

是的,您的 EngineMeasurmentCollection 应该是 ObservableCollection 。然后,您只需向集合中添加(或删除)项目即可,WPF 绑定系统将负责其余操作以更新图表。这就是您应该使用可观察集合的原因。

Yes, your EngineMeasurmentCollection should be an ObservableCollection. Then you won't have anything more to do but add (or remove) items to the collection and the WPF binding system will take care of the rest to update the chart. That's the reason you should use an observable collection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文