-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathForm1.Plot.cs
More file actions
executable file
·279 lines (226 loc) · 6.42 KB
/
Form1.Plot.cs
File metadata and controls
executable file
·279 lines (226 loc) · 6.42 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
using System;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Collections.Specialized;
using System.Reflection;
namespace SerialTerminal {
public partial class Form1 {
ChartArea chartArea = new ChartArea();
Series series1 = new Series();
Series series2 = new Series();
Series series3 = new Series();
int xRangeNum;
int[] x;
double[] y1, y2, y3;
private Regex plotre1, plotre2, plotre3;
// --------------------------
private void InitPlot() {
// --------------------------
chartArea.AxisX.MajorGrid.LineColor = Color.LightGray;
chartArea.AxisY.MajorGrid.LineColor = Color.LightGray;
chartArea.AxisX.LabelStyle.Font = new Font("Consolas", 6);
chartArea.AxisY.LabelStyle.Font = new Font("Consolas", 6);
chartArea.BackColor = Color.Beige;
chartArea.AxisX.Minimum = 0;
// chartArea.Position.X = 0;
// chartArea.Position.Width = 100;
chart1.ChartAreas.Add(chartArea);
series1.ChartType = SeriesChartType.FastLine;
series1.XValueType = ChartValueType.Int32;
series1.YValueType = ChartValueType.Double;
series1.Color = Color.Maroon;
chart1.Series.Add(series1);
series2.ChartType = SeriesChartType.FastLine;
series2.XValueType = ChartValueType.Int32;
series2.YValueType = ChartValueType.Double;
series2.Color = Color.Navy;
chart1.Series.Add(series2);
series3.ChartType = SeriesChartType.FastLine;
series3.XValueType = ChartValueType.Int32;
series3.YValueType = ChartValueType.Double;
series3.Color = Color.Green;
chart1.Series.Add(series3);
scaleXRange();
chart1.Invalidate();
}
// --------------------------
private void clearPlot() {
// --------------------------
plotInputDisplay.Text = "";
plotValue1.Text = "";
plotValue2.Text = "";
plotValue3.Text = "";
scaleXRange();
}
// --------------------------
private void plotMessage(String strLine) {
// --------------------------
plotInputDisplay.Text = strLine;
try {
matchAndPlot(strLine, plotre1, plotValue1, series1, y1);
matchAndPlot(strLine, plotre2, plotValue2, series2, y2);
matchAndPlot(strLine, plotre3, plotValue3, series3, y3);
}
catch {
error("Error applying Regex highlighting");
}
}
// --------------------------
private void matchAndPlot(String text, Regex re, Label lbl, Series ser, double[] y) {
// --------------------------
lbl.Text = "";
if (re != null) {
Match m = re.Match(text);
if (m.Success) {
Group g = m.Groups[1];
lbl.Text = g.Value;
try {
double value = Double.Parse(g.Value);
// shift the line
for (int i = 0; i < xRangeNum - 1; i++)
y[i] = y[i + 1];
// add the new point
y[xRangeNum - 1] = value;
ser.Points.DataBindXY(x, y);
}
catch { };
}
}
}
// --------------------------
private void updatePlotRegex(object sender, EventArgs e) {
// --------------------------
updatePlotRegexImpl();
}
// --------------------------
private void updatePlotRegexImpl() {
// --------------------------
if(plotRegex1.Text != "") {
try {
plotre1 = new Regex(plotRegex1.Text);
plotRegex1.BackColor = default(Color);
}
catch {
plotre1 = null;
plotRegex1.BackColor = Color.LightPink;
}
}
else
plotre1 = null;
if (plotRegex2.Text != "") {
try {
plotre2 = new Regex(plotRegex2.Text);
plotRegex2.BackColor = default(Color);
}
catch {
plotre2 = null;
plotRegex2.BackColor = Color.LightPink;
}
}
else
plotre2 = null;
if (plotRegex3.Text != "") {
try {
plotre3 = new Regex(plotRegex3.Text);
plotRegex3.BackColor = default(Color);
}
catch {
plotre3 = null;
plotRegex3.BackColor = Color.LightPink;
}
}
else
plotre3 = null;
}
#region ChartRange
// --------------------------
private void yMaxText_KeyDown(object sender, KeyEventArgs e) {
// --------------------------
if (e.KeyCode == Keys.Enter) {
e.Handled = true;
e.SuppressKeyPress = true;
scaleChartRange();
}
}
// --------------------------
private void yMinText_KeyDown(object sender, KeyEventArgs e) {
// --------------------------
if (e.KeyCode == Keys.Enter) {
e.Handled = true;
e.SuppressKeyPress = true;
scaleChartRange();
}
}
// --------------------------
private void xRange_KeyDown(object sender, KeyEventArgs e) {
// --------------------------
if (e.KeyCode == Keys.Enter) {
e.Handled = true;
e.SuppressKeyPress = true;
scaleXRange();
}
}
// --------------------------
private void yMaxText_Leave(object sender, EventArgs e) {
// --------------------------
scaleChartRange();
}
// --------------------------
private void yMinText_Leave(object sender, EventArgs e) {
// --------------------------
scaleChartRange();
}
// --------------------------
private void xRange_Leave(object sender, EventArgs e) {
// --------------------------
scaleXRange();
}
// --------------------------
private void scaleChartRange() {
// --------------------------
chartArea.AxisY.Maximum = Double.NaN;
chartArea.AxisY.Minimum = Double.NaN;
try {
chartArea.AxisY.Maximum = Int32.Parse(yMaxText.Text);
}
catch { };
try {
chartArea.AxisY.Minimum = Int32.Parse(yMinText.Text);
}
catch { };
try {
chartArea.RecalculateAxesScale();
}
catch {
error("Error: Y Min range higher than Max range");
chartArea.AxisY.Minimum = Double.NaN;
chartArea.RecalculateAxesScale();
};
}
// --------------------------
private void scaleXRange() {
// --------------------------
try {
xRangeNum = Int32.Parse(xRange.Text);
}
catch {
xRangeNum = 20;
}
chartArea.AxisX.Maximum = xRangeNum;
x = new int[xRangeNum];
y1 = new double[xRangeNum];
y2 = new double[xRangeNum];
y3 = new double[xRangeNum];
for (int i = 0; i < xRangeNum; i++) {
x[i] = i;
}
series1.Points.DataBindXY(x, y1);
series2.Points.DataBindXY(x, y2);
series3.Points.DataBindXY(x, y3);
chart1.Invalidate();
}
#endregion
}
}