-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLiveGraph.pde
362 lines (303 loc) · 10.8 KB
/
LiveGraph.pde
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// =============================================
// CLASS LIVE GRAPH (Chart Object for the Demo GUI)
// by Guillem Benavent (Updated: 28/8/2023)
// For Research Project at Taradell School
// =============================================
// A LiveGraph shows a dinamic chart ploted from a stream of data.
// It allows an undefined length of data through automatic scroll.
// It also allows to plot multiple channels if they share the same Y axis range.
// =============================================
class LiveGraph
{
PApplet window;
float PIX_MARGIN = 60;
FloatList[] dataPoints;
// Position and Sizes of the LiveGraph
float xPos;
float yPos;
float windowWidth;
float windowHeight;
float graphWidth;
float graphHeight;
// Axis limit values and parameters
float tMax;
float yMax;
float yMin;
float timeIncrement;
int maxPoints;
String yUnit;
// Graph line properties
color backgroundColor = color(255);
color lineColor;
float lineThickness;
float margin = 15; //??
// Grid parameters
float tGridIncrement;
float yGridIncrement;
color gridColor;
// Text Properties
float textSize;
int decimalPlaces;
String title;
float titleSize;
String channelNames[];
color textColor;
// Other
float zeroYPos;
int numOfExport = 0;
// Constructors ==============================================
LiveGraph(float xPos, float yPos, float windowWidth, float windowHeight, PApplet window)
{
this(xPos, yPos, windowWidth, windowHeight, window, 1);
}
LiveGraph(float xPos, float yPos, float windowWidth, float windowHeight, PApplet window, int numOfChannels)
{
this.window = window;
this.xPos = xPos;
this.yPos = yPos;
this.windowWidth = windowWidth;
this.windowHeight = windowHeight;
this.graphHeight = this.windowHeight-2*PIX_MARGIN;
this.graphWidth = this.windowWidth-4*PIX_MARGIN;
// If is multichannel, let's keep space for the legend:
//if (numOfChannels>=1) { this.graphWidth = this.graphWidth -2*PIX_MARGIN; }
this.channelNames = new String[numOfChannels];
for (int i=0; i<numOfChannels; i++)
{
this.channelNames[i] = "Channel #"+i;
}
dataPoints = new FloatList[numOfChannels];
resetData();
}
// Properties Setters: Axis, Graph, Grid, Text ===========================================
void setAxisProperties(float tMax, float yMin, float yMax, float timeIncrement)
{
setAxisProperties(tMax, yMin, yMax, timeIncrement, "");
}
void setAxisProperties(float tMax, float yMin, float yMax, float timeIncrement, String yUnit)
{
this.tMax = tMax;
this.yMax = yMax;
this.yMin = yMin;
this.timeIncrement = timeIncrement;
this.yUnit = yUnit;
this.maxPoints = int(nf(tMax/timeIncrement , 0, 0));
this.zeroYPos = map(0, yMax, yMin, margin, windowHeight);
}
void setGraphProperties(color backgroundColor, color lineColor)
{
setGraphProperties(backgroundColor, lineColor, 1);
}
void setGraphProperties(color backgroundColor, color lineColor, float lineThickness)
{
this.backgroundColor = backgroundColor;
this.lineColor = lineColor;
this.lineThickness = lineThickness;
}
void setGridProperties(float tGridIncrement, float yGridIncrement, color gridColor)
{
this.tGridIncrement = tGridIncrement;
this.yGridIncrement = yGridIncrement;
this.gridColor = gridColor;
}
void setTextProperties(float textSize, int decimalPlaces)
{
setTextProperties(textSize, decimalPlaces, "", 1, null, color(0));
}
void setTextProperties(float textSize, int decimalPlaces, String title, float titleSize)
{
setTextProperties(textSize, decimalPlaces, title, titleSize, null, color(0));
}
void setTextProperties(float textSize, int decimalPlaces, String title, float titleSize, String[] channelNames, color textColor)
{
this.textSize = textSize;
this.decimalPlaces = decimalPlaces;
this.title = title;
this.titleSize = titleSize;
this.textColor = textColor;
if (channelNames!=null)
{
this.channelNames = channelNames;
}
}
// Data functions ========================================================
void addPoint(float num)
{
addPoint(new float[]{num});
}
void addPoint(float[] nums)
{
for (int i = 0; i < dataPoints.length; i++)
{
dataPoints[i].append(nums[i]);
}
}
void resetData()
{
int numOfChannels = dataPoints.length;
for (int i = 0; i < numOfChannels; i++)
{
dataPoints[i] = new FloatList();
}
}
void export(String tableName)
{
PrintWriter output;
output = createWriter(tableName+".csv");
String header = new String("time (s)");
for (int i = 0; i < dataPoints.length; i++)
{
header = header + ", " + channelNames[i];
}
output.println(header);
for (int i = 0; i < dataPoints[0].size(); i++)
{
String newLine = new String(str(timeIncrement*i));
for (int j = 0; j < dataPoints.length; j++)
{
newLine = newLine + ", " + str(dataPoints[j].get(i));
}
output.println(newLine);
}
output.flush();
output.close();
}
// Display functions =====================================================
void display()
{
PVector pix, endPix;
//PVector val, endVal;
String text;
//graph shift
int iShift = 0;
float tShift = 0.0;
if(dataPoints[0].size() >= maxPoints)
{
iShift = dataPoints[0].size() - maxPoints;
tShift = iShift * timeIncrement;
}
// Window
window.fill(backgroundColor);
window.noStroke();
window.rectMode(CORNER);
window.rect(xPos, yPos, windowWidth, windowHeight);
// Graph area
pix = unitsToWindow(0, yMax, 0);
window.fill(gridColor);
window.noStroke();
window.rectMode(CORNER);
window.rect(pix.x, pix.y, graphWidth, graphHeight);
window.fill(backgroundColor);
window.rect(pix.x+1, pix.y+1, graphWidth-1, graphHeight-1);
// Y axis
window.strokeWeight(2);
window.stroke(gridColor);
//0 Line
if (yMax>0 && yMin<0)
{
pix = unitsToWindow(0, 0, 0);
endPix = unitsToWindow(tMax, 0, 0);
window.line(pix.x, pix.y, endPix.x, endPix.y);
}
// Y grid
window.strokeWeight(1);
window.stroke(gridColor);
float yRange = yMax - yMin;
float numberOfDivisions = floor(yRange/yGridIncrement);
float zeroOffset = ((yMin / yGridIncrement)-floor(yMin / yGridIncrement)) * yGridIncrement;
for (int i=0; i<=numberOfDivisions+1; i++)
{
float y = yMin + i*yGridIncrement - zeroOffset;
float yBottomLineValue;
if ((y>=yMin) && (y<=yMax))
{
pix = unitsToWindow(0, y);
endPix = unitsToWindow(tMax, y);
window.fill(gridColor);
window.line(pix.x, pix.y, endPix.x, endPix.y);
pix = unitsToWindow(0, y);
endPix = unitsToWindow(0, 0);
yBottomLineValue = floor((endPix.y-pix.y)/yGridIncrement);
window.textAlign(CENTER);
window.textSize(textSize);
window.fill(textColor);
text = nf(y, 0, decimalPlaces);
window.text(text, xPos+PIX_MARGIN - textWidth(text)*(textSize/100) - 5, pix.y+textSize/4);
}
} //2 thirds because it looks good
window.text(yUnit, xPos+(PIX_MARGIN)- textWidth(yUnit)*(textSize/100) -3, yPos+(2*PIX_MARGIN/3));
// X grid
numberOfDivisions = floor(tMax/tGridIncrement);
float dShift = floor(tShift/tGridIncrement);
float zeroTimeOffset = ((tShift / tGridIncrement)-dShift) * tGridIncrement;
for (int i=0; i<=numberOfDivisions+1; i++)
{
float t = i*tGridIncrement - zeroTimeOffset;
if (t<=tMax && t>=0)
{
pix = unitsToWindow(t, yMin);
endPix = unitsToWindow(t, yMax);
window.fill(gridColor);
window.line(pix.x, pix.y, endPix.x, endPix.y);
window.textAlign(CENTER);
window.textSize(textSize);
window.fill(textColor);
text = nf((i+dShift) * tGridIncrement, 0, decimalPlaces);
window.text(text, pix.x, yPos + PIX_MARGIN + graphHeight + textSize);
}
}
window.textSize(textSize);
window.text("s", xPos+PIX_MARGIN + graphWidth + textSize/2 + textSize, yPos + PIX_MARGIN + graphHeight + textSize);
// Data lines
window.strokeWeight(lineThickness);
for(int i = 0; i < dataPoints.length; i++)
{
window.stroke(lerpColor(lineColor, color(255), float(i)/float(dataPoints.length)));
for(int j = iShift+1; j < dataPoints[i].size(); j++)
{
pix = unitsToWindow(j*timeIncrement, dataPoints[i].get(j), tShift);
endPix = unitsToWindow((j-1)*timeIncrement, dataPoints[i].get(j-1), tShift);
float yPixelValueMax = yPos+PIX_MARGIN;
if(pix.y<yPixelValueMax) {pix.y = yPixelValueMax;}
if(endPix.y<yPixelValueMax) {endPix.y = yPixelValueMax;}
float yPixelValueMin = yPos+PIX_MARGIN+graphHeight;
if(pix.y>yPixelValueMin) {pix.y = yPixelValueMin;}
if(endPix.y>yPixelValueMin) {endPix.y = yPixelValueMin;}
window.line(pix.x, pix.y, endPix.x, endPix.y);
}
}
// Title
window.textSize(titleSize);
window.textAlign(CENTER);
window.fill(textColor);
window.text(title, xPos + windowWidth/2, yPos + PIX_MARGIN/2 + titleSize/3);
// Legend
if (channelNames!=null && channelNames.length>0)
{
float incY = graphHeight / (1+channelNames.length);
float absX = xPos + PIX_MARGIN + graphWidth + PIX_MARGIN/2;
float absY = yPos + PIX_MARGIN + graphHeight - incY;
float squareSize = PIX_MARGIN/3;
if (squareSize>=incY) { squareSize=incY-1; }
window.noStroke();
window.rectMode(CENTER);
for(int i = 0; i < channelNames.length; i++)
{
window.fill(lerpColor(lineColor, color(255), float(i)/float(channelNames.length)));
window.rect(absX, absY-(incY*i), squareSize, squareSize);
window.textSize(textSize);
window.text(channelNames[i], absX + squareSize + 2*textSize, absY-(incY*(0.25+i)) + textSize);
}
}
}
PVector unitsToWindow(float t, float y)
{
return unitsToWindow(t, y, 0);
}
PVector unitsToWindow(float t, float y, float tShift)
{
float pixX = xPos + (PIX_MARGIN) + map(t-tShift, 0, tMax, 0, graphWidth);
float pixY = yPos + windowHeight - (PIX_MARGIN) - map(y, yMin, yMax, 0, graphHeight);
return(new PVector(pixX, pixY));
}
}