我要实时绘图,不要拖延

时间:2019-08-31 10:04:52

标签: c# plot uwp windows-10

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

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

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

运行绘图代码20秒钟后,图形移动速度逐渐变慢,最后图形移动暂时停止约1秒钟,然后开始移动3秒钟并停止了约1秒钟,重复这些移动并停止

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

我给了这个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);

    }
}

0 个答案:

没有答案