-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
44 lines (43 loc) · 1.67 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using DevExpress.Xpf.Charts;
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Threading;
namespace RealtimeChartMvvm {
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
}
private void chart_BoundDataChanged(object sender, RoutedEventArgs e) {
// Adjust the visual range.
AxisX2D axisX = ((XYDiagram2D)chart.Diagram).ActualAxisX;
DateTime maxRangeValue = (DateTime)axisX.ActualWholeRange.ActualMaxValue;
axisX.ActualVisualRange.SetMinMaxValues(maxRangeValue.AddSeconds(-10), maxRangeValue);
}
}
public class ChartViewModel {
const int MaxPointCount = 3000;
readonly DispatcherTimer timer = new DispatcherTimer();
public ObservableCollection<DataPoint> DataPoints { get; } = new ObservableCollection<DataPoint>();
public ChartViewModel() {
timer.Tick += Timer_Tick;
timer.Interval = TimeSpan.FromMilliseconds(100);
timer.Start();
}
private void Timer_Tick(object sender, System.EventArgs e) {
DataPoints.Add(new DataPoint(DateTime.Now, GenerateValue(DataPoints.Count)));
if (DataPoints.Count == MaxPointCount) { timer.Stop(); }
}
private double GenerateValue(double x) {
return Math.Sin(x) * 3 + x / 2 + 5;
}
}
public class DataPoint {
public DateTime Argument { get; set; }
public double Value { get; set; }
public DataPoint(DateTime argument, double value) {
this.Argument = argument;
this.Value = value;
}
}
}