main
syneffort 11 months ago
parent 6def087f52
commit a6abc17228
  1. 5
      OxyPlotExample/GettingStarted/MainWindow.xaml
  2. 9
      OxyPlotExample/GettingStarted/MainWindow.xaml.cs
  3. 55
      OxyPlotExample/GettingStarted/ViewModels/MainViewModel3.cs

@ -8,7 +8,8 @@
Title="MainWindow" Title="MainWindow"
Width="800" Width="800"
Height="450" Height="450"
mc:Ignorable="d"> mc:Ignorable="d"
Closing="Window_Closing">
<Grid> <Grid>
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="*"/> <RowDefinition Height="*"/>
@ -18,6 +19,6 @@
<views:MainViewControl Grid.Row="0"/> <views:MainViewControl Grid.Row="0"/>
<views:MainViewControl2 Grid.Row="1"/> <views:MainViewControl2 Grid.Row="1"/>
<views:MainViewControl3 Grid.Row="2"/> <views:MainViewControl3 Grid.Row="2" x:Name="vcRT"/>
</Grid> </Grid>
</Window> </Window>

@ -1,4 +1,5 @@
using System.Text; using GettingStarted.ViewModels;
using System.Text;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Data; using System.Windows.Data;
@ -20,5 +21,11 @@ namespace GettingStarted
{ {
InitializeComponent(); InitializeComponent();
} }
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (vcRT.DataContext is MainViewModel3 vm)
vm.OnClosing();
}
} }
} }

@ -9,6 +9,7 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Timers; using System.Timers;
using System.Windows; using System.Windows;
using OxyPlot.Axes;
namespace GettingStarted.ViewModels namespace GettingStarted.ViewModels
{ {
@ -22,25 +23,67 @@ namespace GettingStarted.ViewModels
private LineSeries _lineSeries; private LineSeries _lineSeries;
[ObservableProperty]
private double _cutOffSecond;
public MainViewModel3() 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" }; 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); MyModel.Series.Add(LineSeries);
CutOffSecond = 10d;
_timer = new System.Timers.Timer(); _timer = new System.Timers.Timer();
_timer.Interval = 50; _timer.Interval = 10;
_timer.Elapsed -= _timer_Elapsed; _timer.Elapsed -= _timer_Elapsed;
_timer.Elapsed += _timer_Elapsed; _timer.Elapsed += _timer_Elapsed;
} }
private int _lastX = 0; private int _lastX = 0;
private Random _rand = new Random();
private double _noiseLevel = 0.3;
private void _timer_Elapsed(object? sender, ElapsedEventArgs e) private void _timer_Elapsed(object? sender, ElapsedEventArgs e)
{ {
Application.Current.Dispatcher.Invoke(() => 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); MyModel.InvalidatePlot(true);
_lastX++; _lastX++;
}); });
@ -63,5 +106,11 @@ namespace GettingStarted.ViewModels
_isTimerStarted = false; _isTimerStarted = false;
} }
} }
public void OnClosing()
{
_timer.Stop();
_timer.Dispose();
}
} }
} }

Loading…
Cancel
Save