wpf DynamicDataDisplay ChartPlotter 删除所有绘图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13142173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DynamicDataDisplay ChartPlotter remove all plots
提问by Cynical
In my WPF application I have a D3 ChartPlotter where I was able to plot 4 LineGraphs. This is the XAML code:
在我的 WPF 应用程序中,我有一个 D3 ChartPlotter,我可以在其中绘制 4 个 LineGraph。这是 XAML 代码:
<d3:ChartPlotter Name="plotter">
<d3:ChartPlotter.HorizontalAxis>
<d3:HorizontalAxis Name="timeAxis" />
</d3:ChartPlotter.HorizontalAxis>
<d3:ChartPlotter.VerticalAxis>
<d3:VerticalAxis Name="accelerationAxis" />
</d3:ChartPlotter.VerticalAxis>
</d3:ChartPlotter>
where d3is the namespace for DinamicDataDisplay, and this is (relevant part of) the code behind.
哪里d3是 DynamicDataDisplay 的命名空间,这是(相关部分)背后的代码。
var x = new List<int>();
var y = new List<int>();
for (var t = 0; t <= 10; t = t + 1) {
x.Add(t);
y.Add(Math.Pow(t,2));
}
var xCoord = new EnumerableDataSource<int>(x);
xCoord.SetXMapping(t => t);
var yCoord = new EnumerableDataSource<int>(y);
yCoord.SetYMapping(k => k);
CompositeDataSource plotterPoints = new CompositeDataSource(xCoord, yCoord);
plotter.AddLineGraph(plotterPoints, Brushes.Red.Color , 2, "MyPlot");
What I want to do now is remove this plot and redraw it using a different set of points. Unfortunately I'm unable to find anything that goes in that direction both in the (poor) documentation of D3 and in the web.
我现在想要做的是删除这个图并使用一组不同的点重新绘制它。不幸的是,我在 D3 的(糟糕的)文档和网络中都找不到任何朝这个方向发展的东西。
Any suggestion about what to do or where to look?
关于做什么或在哪里看的任何建议?
Thanks!
谢谢!
采纳答案by Jason Higgins
The best way that I have found to do this, is to have a Property in your code behind that represents the DataSource and bind the chart's DataSource to that property. Have your code behind implement INotifyPropertyChanged and call OnPropertyChanged every time you update or re-assign your data source. This will force the plotter to observe the binding and redraw your graph.
我发现这样做的最佳方法是在您的代码背后有一个属性来表示数据源并将图表的数据源绑定到该属性。让您的代码背后实现 INotifyPropertyChanged 并在每次更新或重新分配数据源时调用 OnPropertyChanged。这将强制绘图仪观察绑定并重新绘制图形。
Example:
例子:
EnumerableDataSource<Point> m_d3DataSource;
public EnumerableDataSource<Point> D3DataSource {
get {
return m_d3DataSource;
}
set {
//you can set your mapping inside the set block as well
m_d3DataSource = value;
OnPropertyChanged("D3DataSource");
}
}
protected void OnPropertyChanged(PropertyChangedEventArgs e) {
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) {
handler(this, e);
}
}
protected void OnPropertyChanged(string propertyName) {
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
If you require more information, the best resource I could find were the CodePlex discussions where D3 is Located: Discussions
如果您需要更多信息,我能找到的最佳资源是 D3 所在的 CodePlex 讨论: 讨论
回答by Spartan
well there is an easy way to do this. If your purpose is to delete every graph in the plotter simply do this:
那么有一个简单的方法来做到这一点。如果您的目的是删除绘图仪中的每个图形,只需执行以下操作:
plotterName.Children.RemoveAll((typeof(LineGraph));
Hope this is useful to you.
希望这对你有用。
回答by Max Dobbelstein
I had a similar Issue. There are several different Graph types in D3. For example when you use ElementMarkerPoints, you have to RemoveAll((typeof(MarkerPointGraph)).
我有一个类似的问题。D3 中有几种不同的图表类型。例如,当您使用 ElementMarkerPoints 时,您必须 RemoveAll((typeof(MarkerPointGraph))。
Find out what Graph Type you are using, then you can remove all plots.
找出您正在使用的图形类型,然后您可以删除所有绘图。
EDIT:
编辑:
Don't remove the Graphs from the plotter. Use an ObservableDataSource and remove the values when you need to clear the Plotter. When I remember right, you would risk a memory leak otherwise, because the graphs are not garbage collected. The ObservableDataSource has a property called Collection, just call the Clear method and you are good :)
不要从绘图仪中删除图形。当您需要清除绘图仪时,使用 ObservableDataSource 并删除值。如果我没记错的话,否则您将面临内存泄漏的风险,因为这些图不是垃圾收集的。ObservableDataSource 有一个名为 Collection 的属性,只需调用 Clear 方法即可:)

