首页 > 解决方案 > 我要实时绘图,不耽误

问题描述

我的目标是使用 UWP(Windows10) 绘制实时折线图。

我使用 Oxyplot 作为图形绘图库。将要绘制的数据堆叠在此图表上,直到数据数量达到 500,之后,图表开始从右向左移动。

我想知道如何平滑滑动实时绘图。我的图表从右向左滑动重复停止和移动。

在我运行绘图代码 20 秒后,图形移动速度变慢,最后,图形移动暂时停止约 1 秒并开始移动 3 秒并停止约 1 秒,重复这些移动和停止。

我希望实时绘图不会延迟从右到左的滑动。保持平滑滑动图是我的理想。但我不能。

我已经给了这个 Graph 常量值。我继续在另一个线程中运行图形绘图(task.run(() => Graph()))。在继续绘图的同时,GC 始终在我的视觉工作室中工作。

这会自动 GC 导致滑动延迟吗?还是计算成本导致了这种延迟?

您能告诉我滑动延迟的原因以及如何顺利绘制吗?

public Plot_Graph()
{

    this.InitializeComponent();

    //Initialize Graph settings  
    var newMyModel = new PlotModel { Title = "Example 1" };
    MyModel = newMyModel;
    MyModel.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, Minimum = 0.0, Maximum = 300.0, Title = "X_axis" });
    MyModel.Axes.Add(new LinearAxis { Position = AxisPosition.Left, Minimum = 0.0, Maximum = 5000.0, Title = "Y_axis" });
    line = new LineSeries();
    line.Color = OxyColors.Blue;
    MyModel.Series.Add(line);

    //Start Plotting
    Task.Run(() => Graph());

}


private int cnt = 0;
private async Task Graph()
{
    while (true)
    {
        //calculate start time.
        var start_time = long.Parse(DateTime.Now.ToString("yyyyMMddHHmmssffff"));

        //this is the Graph value. I set the value as 1000.
        int number = 1000;
        line.Points.Add(new DataPoint(cnt, number));


        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
        {
            //Here is the code to show the graph on the UI.
            GraphPloting.Model = MyModel;          
        });



        if (line.Points.Count > 500)
        {
            //If the number of the graph value is up to 500,It starts moving the Graph right to left.
            MyModel.Axes.Clear(); 
            MyModel.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, Minimum = cnt - 500, Maximum = (cnt - 500) + 500 }); //setting of x axis.                                                                                                                                   

        }
        else
        {
            //Graph does not moving slide until the number of data becomes 500.
            MyModel.Axes.Clear(); 
            MyModel.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, Minimum = 0.0, Maximum = cnt }); //settings of x axis                                                                                                            

        }

        if (line.Points.Count > 500)      
        {
            //delete the value which is out of the x axis.
            line.Points.RemoveAt(0);
        }

        //increase the value of x axis.
        cnt += 1;

        //calculate finish time.
        var end_time = long.Parse(DateTime.Now.ToString("yyyyMMddHHmmssffff"));

        //I saw these different time between start and end increase gradually.
        Debug.WriteLine("Difference_time: " + (end_time - start_time));

        //Update the graph after changing graph values.
        MyModel.InvalidatePlot(true);

    }
}

标签: c#plotuwpwindows-10

解决方案


推荐阅读