-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathForm1.cs
82 lines (77 loc) · 3.35 KB
/
Form1.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using DevExpress.Utils;
using DevExpress.XtraCharts;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
namespace RealTimeChartUpdates {
public partial class Form1 : Form {
const int ViewportPointCount = 5000;
int counter = 0;
Thread dataAcquisitionThread;
bool isEnabled = true;
int lastRawDataIndex = 0;
List<DataPoint> rawData = new List<DataPoint>();
ObservableCollection<DataPoint> viewportData = new ObservableCollection<DataPoint>();
public Form1() { InitializeComponent(); }
void Form1_Load(object sender, EventArgs e) {
dataAcquisitionThread = new Thread(new ThreadStart(AcquireData));
dataAcquisitionThread.Start();
chartControl1.Titles.Add(new ChartTitle { Text = "Real-Time Charting" });
Series series = new Series();
series.ChangeView(ViewType.Line);
series.DataSource = viewportData;
series.DataSourceSorted = true;
series.ArgumentDataMember = "Argument";
series.ValueDataMembers.AddRange("Value");
chartControl1.Series.Add(series);
LineSeriesView seriesView = (LineSeriesView)series.View;
seriesView.LastPoint.LabelDisplayMode = SidePointDisplayMode.SeriesPoint;
seriesView.LastPoint.Label.TextPattern = "{V:f2}";
XYDiagram diagram = (XYDiagram)chartControl1.Diagram;
diagram.AxisX.DateTimeScaleOptions.ScaleMode = ScaleMode.Continuous;
diagram.AxisX.Label.ResolveOverlappingOptions.AllowRotate = false;
diagram.AxisX.Label.ResolveOverlappingOptions.AllowStagger = false;
diagram.AxisX.VisualRange.EndSideMargin = 200;
diagram.DependentAxesYRange = DefaultBoolean.True;
diagram.AxisY.WholeRange.AlwaysShowZeroLevel = false;
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
timer.Interval = 100;
timer.Start();
timer.Tick += Timer_Tick;
}
void AcquireData() {
while (isEnabled) {
Thread.Sleep(15);
lock (rawData) {
for (int i = 0; i < 50; i++)
rawData.Add(new DataPoint(DateTime.Now, GenerateValue(counter++)));
}
}
}
double GenerateValue(double x) { return Math.Sin(x / 1000.0) * 3 * x + x / 2 + 5; }
void Timer_Tick(object sender, EventArgs e) {
lock (rawData) {
for (int i = Math.Max(lastRawDataIndex, rawData.Count - ViewportPointCount); i < rawData.Count; i++)
viewportData.Add(rawData[i]);
lastRawDataIndex = rawData.Count;
while (viewportData.Count > ViewportPointCount)
viewportData.RemoveAt(0);
}
}
protected override void OnClosing(CancelEventArgs e) {
isEnabled = false;
base.OnClosing(e);
}
public class DataPoint {
public DataPoint(DateTime argument, double value) {
Argument = argument;
Value = value;
}
public DateTime Argument { get; set; }
public double Value { get; set; }
}
}
}