【问题标题】:DynamicDataDisplay ChartPlotter remove all plotsDynamicDataDisplay ChartPlotter 删除所有绘图
【发布时间】:2012-10-30 15:21:17
【问题描述】:

在我的 WPF 应用程序中,我有一个 D3 ChartPlotter,我可以在其中绘制 4 个 LineGraphs。这是 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>

其中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");

我现在要做的是删除此图并使用一组不同的点重新绘制它。不幸的是,我在 D3 的(糟糕的)文档和网络中都找不到任何朝着这个方向发展的东西。

关于做什么或去哪里看有什么建议吗?

谢谢!

【问题讨论】:

    标签: c# wpf charts dynamic-data-display


    【解决方案1】:

    我发现做到这一点的最佳方法是在您的代码中使用一个属性来表示数据源并将图表的数据源绑定到该属性。每次更新或重新分配数据源时,让您的代码实现 INotifyPropertyChanged 并调用 OnPropertyChanged。这将迫使绘图仪观察绑定并重新绘制图表。

    例子:

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

    如果您需要更多信息,我能找到的最佳资源是 D3 所在的 CodePlex 讨论: Discussions

    【讨论】:

    • 感谢您的回答,@Jason。由于我对所有 D3 事物(以及一般的 WPF)都是新手,您能否举一个要使用的 XAML 代码示例?我必须绑定单个“xCoord”和“yCoord”,还是 CompositeDataSource 就足够了?
    • 好的,我想我已经解决了,深入讨论@Jason Higgins 提到的: private void ClearPlotter() { foreach (var elem in this.plotter.Children.Where( e => e is LineGraph || e is MarkerPointsGraph).ToArray()) { this.plotter.Children.Remove(elem); } } 你认为这是一个好的解决方案吗?
    • 如果您的意思是清除绘图以显示新数据,则无需删除所有元素!只需将您创建的 DataSource 属性设置为新集合,调用 Property Changed 事件,然后它将自动更新您的绑定。如果您只想清除图表而不显示任何数据,请将 DataSource 设置为空集合。 IE。 D3DataSource = new EnumerableDataSource(new object[0]);
    【解决方案2】:

    好吧,有一个简单的方法可以做到这一点。如果您的目的是删除绘图仪中的每个图形,只需执行以下操作:

    plotterName.Children.RemoveAll((typeof(LineGraph));
    

    希望这对你有用。

    【讨论】:

      【解决方案3】:

      我有一个类似的问题。 D3 中有几种不同的 Graph 类型。例如当你使用 ElementMarkerPoints 时,你必须要 RemoveAll((typeof(MarkerPointGraph))。

      找出您正在使用的图表类型,然后您可以删除所有图表。

      编辑:

      不要从绘图仪中删除图形。当您需要清除绘图仪时,使用 ObservableDataSource 并删除这些值。当我没记错的时候,否则你会冒内存泄漏的风险,因为这些图不是垃圾收集的。 ObservableDataSource 有一个名为 Collection 的属性,只需调用 Clear 方法即可:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-24
        • 2014-12-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-18
        • 2012-08-13
        相关资源
        最近更新 更多