draw a point on a graph using oxyplot on wpf application

好久不见. 提交于 2019-12-25 09:26:14

问题


In my project I want to draw a point on a real-time graph whenever the graph is equal to a certain value. I don't know how do that. This is the code that I use to show the real time graph:

 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:


You should use the following pattern for adding or removing data:

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

Let me know if anything is not working.



来源:https://stackoverflow.com/questions/42021245/draw-a-point-on-a-graph-using-oxyplot-on-wpf-application

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!