实时更新Oxyplot图并与WPF表单加载分开(c#)

时间:2016-11-28 15:10:42

标签: c# wpf visual-studio oxyplot

我刚刚从Windows窗体转移到WPF(c#),并且在从ZedGraphs转换之后,我正在努力掌握OxyPlot。经过几个小时的互联网搜索和观看教程,我... ...

我目前有一张图表可以读取串行数据并在10秒内绘制数据。我也是XAML的新手,所以感谢您的耐心等待。

当我测试程序时,我等待10秒,同时图表获取所有数据,然后绘制并打开应用程序。我希望自己管理打开图表,而不是在启动时自动打开。我还需要实时查看图表,而不必等待所有数据添加,然后在最后加载。

由于

<Window x:Class="Graph_test_5.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:oxy="http://oxyplot.org/wpf"
    xmlns:local="clr-namespace:Graph_test_5"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
    <local:MainViewModel/>
</Window.DataContext>
<Grid>
    <oxy:PlotView x:Name="Graph1" Model="{Binding MyModel}"/>
    </Grid>

public class MainViewModel
    {
         SerialPort myPort = new SerialPort();
         public MainViewModel()
         {
             Stopwatch stopwatch = new Stopwatch();
             stopwatch.Start();

             myPort.BaudRate = 9600;
             myPort.PortName = "COM4";
             myPort.Open();
             MyModel = new PlotModel { Title = "Line" };
             var myModel = new LineSeries { };

             while (stopwatch.Elapsed < TimeSpan.FromSeconds(3))
             {

                 string Number = myPort.ReadLine();
                 double Num;
                 bool isNum = double.TryParse(Number, out Num); // Is the incomming serial data a number?
                 if (isNum) // If it is a number...
                 {
                     int IncommingInt = Convert.ToInt32(Number);
                     long x = stopwatch.ElapsedMilliseconds;
                     myModel.Points.Add(new DataPoint(x, IncommingInt));
                 }
             }
            MyModel.Series.Add(myModel);
         }

         public PlotModel MyModel { get; private set; }
     }

0 个答案:

没有答案