在wpf应用程序上使用oxyplot在图表上绘制一个点

时间:2017-02-03 09:45:32

标签: c# wpf graph oxyplot

在我的项目中,我想在图表等于某个值时在实时图形上绘制一个点。 我不知道那是怎么回事。 这是我用来显示实时图表的代码:

 public class MainViewModel
{
    public PlotModel DataPlot { get; set; }        
    public DispatcherTimer graphTimer;
    private double _xValue = 10;

    public MainViewModel()
    {
        DataPlot = new PlotModel();
        DataPlot.Series.Add(new LineSeries());

        graphTimer = new DispatcherTimer();
        graphTimer.Interval = TimeSpan.FromMilliseconds(MainWindow.timerRefreshMs);
        graphTimer.Tick += dispatcherTimer_Tick;
        graphTimer.Start();    

    }        

    public void dispatcherTimer_Tick(object sender, EventArgs e)
    {
        ScatterSeries series = new ScatterSeries();
        Dispatcher.CurrentDispatcher.Invoke(() =>
        {
            (DataPlot.Series[0] as LineSeries).Points.Add(new DataPoint(_xValue, MainWindow.z));     
            //DataPlot.InvalidatePlot(true);
            //_xValue++;
            if(MainWindow.z == 900)
            {
              //ADD A POINT  

            }
            DataPlot.InvalidatePlot(true);

            _xValue++;

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


}

1 个答案:

答案 0 :(得分:0)

您应该使用以下模式添加或删除数据:

    int _xValue = 0;
    public void dispatcherTimer_Tick(object sender, EventArgs e)
    { 
        Dispatcher.CurrentDispatcher.Invoke(() =>
        {
            LineSeries ser = plotModel.Series[0] as LineSeries;
            if (ser != null)
            {
                // check your conditions and caclulate the Y value of the point
                double yValue = 1;
                ser.Points.Add(new DataPoint(_xValue, yValue));
                _xValue++;
            } 
            if (ser.Points.Count > 80) //show only 10 last points
                ser.Points.RemoveAt(0); //remove first point
            plotModel.InvalidatePlot(true);
        });
    }

如果有什么不起作用,请告诉我。