wpf 如何向 OxyPlot 添加新点?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23088134/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:33:17  来源:igfitidea点击:

How to add new points to OxyPlot?

c#wpfoxyplot

提问by Zgrkpnr

This is the code that Oxyplot official page shows. namespace WpfApplication2

这是 Oxyplot 官方页面显示的代码。命名空间 WpfApplication2

{
    using System.Collections.Generic;

    using OxyPlot;

    public class MainViewModel
    {
        public MainViewModel()
        {
            this.Title = "Example 2";
            this.Points = new List<DataPoint>
                              {
                                  new DataPoint(0, 4),
                                  new DataPoint(10, 13),
                                  new DataPoint(20, 15),
                                  new DataPoint(30, 16),
                                  new DataPoint(40, 12),
                                  new DataPoint(50, 12)
                              };
        }

        public string Title { get; private set; }

        public IList<DataPoint> Points { get; private set; }
    }
}

This is the XAML

这是 XAML

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:oxy="http://oxyplot.codeplex.com"
        xmlns:local="clr-namespace:WpfApplication2"
        Title="Example 2 (WPF)" Height="350" Width="525">
    <Window.DataContext>
        <local:MainViewModel/>
    </Window.DataContext>
    <Grid>
        <oxy:Plot Title="{Binding Title}">
            <oxy:Plot.Series>
                <oxy:LineSeries ItemsSource="{Binding Points}"/>
            </oxy:Plot.Series>
        </oxy:Plot>
    </Grid>
</Window>

The example just works fine and shows 6 points on the graph. What I want to do is plotting a graph of the data which comes through Serial Port. I want to add new points to graph in the DispatcherTimer's Tick event. To eliminate any misunderstanding, My question's scope is about oxyplot,(not, for example, usage of timer event should be included in answer.) Thank you in advance.

该示例运行良好,并在图表上显示了 6 个点。我想要做的是绘制通过串行端口传来的数据图。我想在 DispatcherTimer 的 Tick 事件中向图形添加新点。为了消除任何误解,我的问题的范围是关于 oxyplot,(例如,不应该在答案中包含计时器事件的使用。)在此先感谢您。

回答by BblackK

Try this code.

试试这个代码。

XAML:

XAML:

<oxy:PlotView Model="{Binding DataPlot}"/>

MainViewModel:

主视图模型:

public PlotModel DataPlot { get; set; }
private double _xValue = 1;
public MainViewModel()
{
    DataPlot = new PlotModel();
    DataPlot.Series.Add(new LineSeries());
    var dispatcherTimer = new DispatcherTimer { Interval = new TimeSpan(0, 0, 1) };
    dispatcherTimer.Tick += dispatcherTimer_Tick;
    dispatcherTimer.Start(); 
}

private void dispatcherTimer_Tick(object sender, EventArgs e)
{
    Dispatcher.CurrentDispatcher.Invoke(() =>
    {
       (DataPlot.Series[0] as LineSeries).Points.Add(new DataPoint(_xValue, Math.Sqrt(_xValue)));
        DataPlot.InvalidatePlot(true);
        _xValue ++;
    });
}

If you don′t want to accumulate all points from start to end in chart, just use this after adding every new point:

如果您不想在图表中累积从头到尾的所有点,只需在添加每个新点后使用:

if ((DataPlot.Series[0] as LineSeries).Points.Count > 10) //show only 10 last points
    (DataPlot.Series[0] as LineSeries).Points.RemoveAt(0); //remove first point

If you want a better flow of the chart (this one updates every second) I recommend using Stopwatch, putting code that add points into own method and starting that method in thread in constructor. Then i.e. use Stopwatch.ElapsedMillisecondsas x-value.

如果您想要更好的图表流程(此图表每秒更新一次),我建议使用Stopwatch,将添加点的代码放入自己的方法中,并在构造函数的线程中启动该方法。然后即Stopwatch.ElapsedMilliseconds用作 x 值。