繁体   English   中英

将点绑定到Interactive Data Display WPF库中的图表

[英]Binding points to chart from Interactive Data Display WPF library

我想在WPF中绘制类似于示波器的动态折线图,然后找到了这个库:从该库中出现的Interactive Data DisplayD3 Dynamic Data Display 优点是它的光对我很重要。

从示例程序中,我可以看到它们没有将LineGraph.Points与任何集合绑定,并且当我尝试不起作用时,Graph对象上也没有Refresh或Update方法。 当前,每次我要更新图形时都不得不使用LineGraph.PlotY()方法。

有谁知道是否可以以MVVM方式使用此库?

样例代码:

       double[] x = new double[200];
        for (int i = 0; i < x.Length; i++)
            x[i] = 3.1415 * i / (x.Length - 1);

        for (int i = 0; i < 25; i++)
        {
            var lg = new LineGraph();
            lines.Children.Add(lg);
            lg.Stroke = new SolidColorBrush(Color.FromArgb(255, 0, (byte)(i * 10), 0));
            lg.Description = String.Format("Data series {0}", i + 1);
            lg.StrokeThickness = 2;
            lg.Plot(x, x.Select(v => Math.Sin(v + i / 10.0)).ToArray());
        }

XAML:

        <d3:Chart Name="plotter">
        <d3:Chart.Title>
            <TextBlock HorizontalAlignment="Center" FontSize="18" Margin="0,5,0,5">Line graph legend sample</TextBlock>
        </d3:Chart.Title>
        <d3:Chart.LegendContent>
            <d3:LegendItemsPanel>
                <d3:LegendItemsPanel.Resources>
                    <DataTemplate x:Key="InteractiveDataDisplay.WPF.LineGraph">
                        <StackPanel Orientation="Horizontal">
                            <CheckBox IsChecked="{Binding Path=Visibility, Converter={StaticResource VisibilityToCheckedConverter}, Mode=TwoWay}"/>
                            <Line Width="15" Height="15" X1="0" Y1="0" X2="15" Y2="15" Stroke="{Binding Path=Stroke}" StrokeThickness="2"/>
                            <TextBlock Margin="5,0,0,0" Text="{Binding Path=Description}"/>
                        </StackPanel>
                    </DataTemplate>
                </d3:LegendItemsPanel.Resources>
            </d3:LegendItemsPanel>
        </d3:Chart.LegendContent>
        <Grid Name="lines"/>
    </d3:Chart>

Plot基类中,已经为Points属性注册了一个依赖项属性。 作为“快速修复”,我将其添加到LineGraph类中:

    public ObservableCollection<Point> ObservablePoints
    {
        get { return (ObservableCollection<Point>)GetValue(ObservablePointsProperty); }
        set { SetValue(ObservablePointsProperty, value); }
    }

    public static readonly DependencyProperty ObservablePointsProperty =
        DependencyProperty.RegisterAttached(
            "ObservablePoints",
            typeof(ObservableCollection<Point>),
            typeof(LineGraph),
            new PropertyMetadata(
                new ObservableCollection<Point>(),
                (d, e) =>
                    {
                        var linePlot = (LineGraph)d;
                        var updateAction = new NotifyCollectionChangedEventHandler(
                            (o, args) =>
                                {
                                    if (linePlot != null)
                                    {
                                        InteractiveDataDisplay.WPF.Plot.SetPoints(linePlot.polyline, new PointCollection((ObservableCollection<Point>)o));
                                    }
                                });

                        if (e.OldValue != null)
                        {
                            var coll = (INotifyCollectionChanged)e.OldValue;
                            coll.CollectionChanged -= updateAction;
                        }

                        if (e.NewValue != null)
                        {
                            var coll = (INotifyCollectionChanged)e.NewValue;
                            coll.CollectionChanged += updateAction;
                            if (linePlot != null)
                            {
                                InteractiveDataDisplay.WPF.Plot.SetPoints(linePlot.polyline, new PointCollection((ObservableCollection<Point>)e.NewValue));
                            }
                        }
                    }));

然后,将我的点集合绑定到ObservablePoints属性:

<d3:LineGraph Description="MyGraph" 
              ObservablePoints="{Binding Path=PointsFromMyDatamodel}"/>

缺点是重新绘制了所有点的图形-因此“快速修复”。 仅重画添加,修改或删除的点将需要对基础基类进行更多更改...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM