From a6abc172280e3c37dbb5097d968fbb22628f221d Mon Sep 17 00:00:00 2001 From: syneffort Date: Tue, 21 May 2024 13:39:24 +0900 Subject: [PATCH] RT plot --- OxyPlotExample/GettingStarted/MainWindow.xaml | 5 +- .../GettingStarted/MainWindow.xaml.cs | 9 ++- .../ViewModels/MainViewModel3.cs | 55 ++++++++++++++++++- 3 files changed, 63 insertions(+), 6 deletions(-) diff --git a/OxyPlotExample/GettingStarted/MainWindow.xaml b/OxyPlotExample/GettingStarted/MainWindow.xaml index 63662fc..87c43b9 100644 --- a/OxyPlotExample/GettingStarted/MainWindow.xaml +++ b/OxyPlotExample/GettingStarted/MainWindow.xaml @@ -8,7 +8,8 @@ Title="MainWindow" Width="800" Height="450" - mc:Ignorable="d"> + mc:Ignorable="d" + Closing="Window_Closing"> @@ -18,6 +19,6 @@ - + diff --git a/OxyPlotExample/GettingStarted/MainWindow.xaml.cs b/OxyPlotExample/GettingStarted/MainWindow.xaml.cs index 1f1a3fc..443a3ae 100644 --- a/OxyPlotExample/GettingStarted/MainWindow.xaml.cs +++ b/OxyPlotExample/GettingStarted/MainWindow.xaml.cs @@ -1,4 +1,5 @@ -using System.Text; +using GettingStarted.ViewModels; +using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; @@ -20,5 +21,11 @@ namespace GettingStarted { InitializeComponent(); } + + private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) + { + if (vcRT.DataContext is MainViewModel3 vm) + vm.OnClosing(); + } } } \ No newline at end of file diff --git a/OxyPlotExample/GettingStarted/ViewModels/MainViewModel3.cs b/OxyPlotExample/GettingStarted/ViewModels/MainViewModel3.cs index 7521d5c..e66318c 100644 --- a/OxyPlotExample/GettingStarted/ViewModels/MainViewModel3.cs +++ b/OxyPlotExample/GettingStarted/ViewModels/MainViewModel3.cs @@ -9,6 +9,7 @@ using System.Text; using System.Threading.Tasks; using System.Timers; using System.Windows; +using OxyPlot.Axes; namespace GettingStarted.ViewModels { @@ -22,25 +23,67 @@ namespace GettingStarted.ViewModels private LineSeries _lineSeries; + [ObservableProperty] + private double _cutOffSecond; + + + public MainViewModel3() { - MyModel = new PlotModel() { Title = "Getting Started with RT Plot" }; + MyModel = new PlotModel() { Title = "Getting Started with RT Plot", PlotMargins = new OxyThickness(50, 10, 50, 50) }; LineSeries = new LineSeries() { Title = "Dynamic Data" }; + DateTimeAxis timeAxis = new DateTimeAxis() + { + Position = AxisPosition.Bottom, + StringFormat = "HH:mm:ss", + Title = "Time", + IntervalType = DateTimeIntervalType.Seconds, + IntervalLength = 60, + IsZoomEnabled = false, + IsPanEnabled = false, + }; + MyModel.Axes.Add(timeAxis); + + LinearAxis linearAxis = new LinearAxis() + { + Position = AxisPosition.Left, + Title = "Value" + }; + MyModel.Axes.Add(linearAxis); + MyModel.Series.Add(LineSeries); + CutOffSecond = 10d; + _timer = new System.Timers.Timer(); - _timer.Interval = 50; + _timer.Interval = 10; _timer.Elapsed -= _timer_Elapsed; _timer.Elapsed += _timer_Elapsed; } private int _lastX = 0; + private Random _rand = new Random(); + private double _noiseLevel = 0.3; private void _timer_Elapsed(object? sender, ElapsedEventArgs e) { Application.Current.Dispatcher.Invoke(() => { - LineSeries.Points.Add(new DataPoint(_lastX, Math.Sin(_lastX * 0.1))); + DateTime now = DateTime.Now; + + double noise = (_rand.NextDouble() - 0.5) * _noiseLevel; + //LineSeries.Points.Add(new DataPoint(_lastX, Math.Sin(_lastX * 0.1) + noise)); + LineSeries.Points.Add(new DataPoint(DateTimeAxis.ToDouble(DateTime.Now), Math.Sin(_lastX * 0.1) + noise)); + + DateTime cutOff = now.AddSeconds(-1 * CutOffSecond); + LineSeries.Points.RemoveAll(d => DateTimeAxis.ToDateTime(d.X) < cutOff); + + if (MyModel.Axes[0] is DateTimeAxis timeAxis) + { + timeAxis.Minimum = DateTimeAxis.ToDouble(cutOff); + timeAxis.Maximum = DateTimeAxis.ToDouble(now); + } + MyModel.InvalidatePlot(true); _lastX++; }); @@ -63,5 +106,11 @@ namespace GettingStarted.ViewModels _isTimerStarted = false; } } + + public void OnClosing() + { + _timer.Stop(); + _timer.Dispose(); + } } }