C#动态数据显示 - 更新折线图

时间:2014-05-21 14:45:15

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

在我的XAML文件中,我创建了一个ChartPlotter然后我在c#中创建了我的LineGraphs并将它们连接到我的ChartPlotter。我试图在创建后找到更新这些LineGraphs的方法,但它总是失败。

我发现的唯一解决方案是删除所有LineGraphs,用新值重新创建它们,最后将它们链接到我的ChartPlotter。

如何更新LineGraph?

for (int i = 0; i < lgs.Length; i++)
            if (lgs[i] != null)
                lgs[i].RemoveFromPlotter();

PS:lgs是我的LineGraph数组。

1 个答案:

答案 0 :(得分:3)

要更新LineGraphs,必须使用ObservableDataSource对象而不是CompositeDataSource。使用此对象,您可以使用方法AppendAsync()。

public partial class MainWindow : Window
{
    public ObservableDataSource<Point> source1 = null;

    public MainWindow()
    {
        InitializeComponent();
        this.Loaded += MainWindow_Loaded;
    }

    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        // Create source         
        source1 = new ObservableDataSource<Point>();
        // Set identity mapping of point in collection to point on plot
        source1.SetXYMapping(p => p);

        // Add the graph. Colors are not specified and chosen random
        plotter.AddLineGraph(source1, 2, "Data row");

        // Force everyting to fit in view
        plotter.Viewport.FitToView();

        // Start computation process in second thread
        Thread simThread = new Thread(new ThreadStart(Simulation));
        simThread.IsBackground = true;
        simThread.Start();
    }

    private void Simulation()
    {
        int i = 0;
        while (true)
        {
            Point p1 = new Point(i * i, i);
            source1.AppendAsync(Dispatcher, p1);

            i++;
            Thread.Sleep(1000);

        }
    }
}

您想要的只是模拟方法的时间。

source1.AppendAsync(Dispatcher, p1);