问题
In my WPF application I have a D3 ChartPlotter where I was able to plot 4 LineGraphs. This is the XAML code:
<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 d3
is the namespace for DinamicDataDisplay, and this is (relevant part of) the code behind.
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.
Any suggestion about what to do or where to look?
Thanks!
回答1:
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.
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
回答2:
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.
回答3:
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)).
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 :)
来源:https://stackoverflow.com/questions/13142173/dynamicdatadisplay-chartplotter-remove-all-plots