-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathGraph.ts
472 lines (391 loc) · 15.5 KB
/
Graph.ts
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
import Misc from "./Misc"
import { ObjectCache, CanvasBase } from "./CanvasBase"
class Margin {
readonly left: number;
readonly top: number;
readonly right: number;
readonly bottom: number;
constructor(top: number, right: number, bottom: number, left: number) {
this.top = top;
this.right = right;
this.bottom = bottom;
this.left = left;
}
}
class Rect {
x: number;
y: number;
width: number;
height: number;
constructor(x = 0, y = 0, width = 0, height = 0) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
get topEdge(): number {
return this.y;
}
get bottomEdge(): number {
return this.y + this.height;
}
get leftEdge(): number {
return this.x;
}
get rightEdge(): number {
return this.x + this.width;
}
Move(x: number, y: number) {
this.x = x;
this.y = y;
}
Resize(width: number, height: number) {
this.width = width;
this.height = height;
}
Pad(padding: number): Rect {
return new Rect(this.x + padding, this.y + padding,
this.width - 2 * padding, this.height - 2 * padding);
}
Outline(context: CanvasRenderingContext2D) {
context.save();
context.strokeStyle = "#000000";
context.strokeRect(this.x, this.y, this.width, this.height);
context.restore();
}
DrawText(context: CanvasRenderingContext2D, text: string, fillStyle: string, font: string) {
context.save();
context.font = font;
context.fillStyle = fillStyle;
let measurement = context.measureText(text);
let textWidth = measurement.width * window.devicePixelRatio;
let textHeight = (measurement.actualBoundingBoxAscent + measurement.actualBoundingBoxDescent) * window.devicePixelRatio
let x = this.x + 0.5 * (this.width - textWidth);
let y = this.y + 0.5 * (this.height + textHeight);
context.fillText(text, x, y);
context.restore();
}
}
class Layout {
top: Rect | Layout;
bottom: Rect | Layout;
left: Rect | Layout;
right: Rect | Layout;
center: Rect | Layout;
x: number;
y: number;
width: number;
height: number;
get topEdge(): number {
return this.y;
}
get bottomEdge(): number {
return this.y + this.height;
}
get leftEdge(): number {
return this.x;
}
get rightEdge(): number {
return this.x + this.width;
}
constructor({ top = new Rect(),
right = new Rect(),
bottom = new Rect(),
left = new Rect(),
center = new Rect() }: { top?: Rect | Layout, right?: Rect | Layout, bottom?: Rect | Layout, left?: Rect | Layout, center?: Rect | Layout } = {}) {
this.x = this.y = this.width = this.height = 0;
this.top = top;
this.right = right;
this.bottom = bottom;
this.left = left;
this.center = center;
}
Move(x: number, y: number) {
this.top.Move(x + this.top.x - this.x, y + this.top.y - this.y);
this.bottom.Move(x + this.bottom.x - this.x, y + this.bottom.y - this.y);
this.left.Move(x + this.left.x - this.x, y + this.left.y - this.y);
this.right.Move(x + this.right.x - this.x, y + this.right.y - this.y);
this.center.Move(x + this.center.x - this.x, y + this.center.y - this.y);
this.x = x;
this.y = y;
}
Outline(context: CanvasRenderingContext2D) {
let panels = [this.top, this.bottom, this.left, this.right, this.center];
for (let panel of panels) {
panel.Outline(context);
}
}
Layout(width: number, height: number) {
if (this.top instanceof Layout) {
this.top.Layout(0, 0);
}
this.top.Move(0, 0);
if (this.left instanceof Layout) {
this.left.Layout(0, 0);
}
this.left.Move(0, this.top.bottomEdge);
if (this.right instanceof Layout) {
this.right.Layout(0, 0);
}
this.right.Move(this.left.rightEdge, this.top.bottomEdge);
if (this.bottom instanceof Layout) {
this.bottom.Layout(0, 0);
}
this.bottom.Move(0, this.height);
let centerWidth = Math.max(0, width - this.left.width - this.right.width);
let centerHeight = Math.max(Math.max(this.left.height, this.right.height), height - this.top.height - this.bottom.height);
if (this.center instanceof Layout) {
this.center.Layout(centerWidth, centerHeight);
} else {
this.center.Resize(centerWidth, centerHeight);
}
this.width = this.left.width + this.center.width + this.right.width;
this.height = this.top.height + this.center.height + this.bottom.height;
this.center.Move(this.left.rightEdge, this.top.bottomEdge);
this.top.Move(0.5 * (this.width - this.top.width), 0);
this.left.Move(0, 0.5 * (this.height - this.left.height));
this.right.Move(this.center.rightEdge, 0.5 * (this.height - this.right.height));
this.bottom.Move(0.5 * (this.width - this.bottom.width), this.center.bottomEdge);
}
}
class VerticalRule {
frame: number;
strokeStyle: string;
lineWidth: number;
constructor(frame: number, strokeStyle: string, lineWidth: number) {
this.frame = frame;
this.strokeStyle = strokeStyle;
this.lineWidth = lineWidth;
}
}
class Sparkline {
static readonly MARKER_RADIUS = 4;
values: Float32Array;
name: string;
strokeStyle: string;
lineWidth: number;
minValue: number;
maxValue: number;
verticalRules: VerticalRule[];
constructor(name: string, values: Float32Array, strokeStyle: string, lineWidth: number, verticalRules: VerticalRule[]) {
this.name = name;
this.values = values;
this.strokeStyle = strokeStyle;
this.lineWidth = lineWidth;
this.minValue = Math.min(...values);
this.maxValue = Math.max(...values);
this.verticalRules = verticalRules;
}
Draw(context: CanvasRenderingContext2D, box: Rect, frameIndex: number) {
let xScale = box.width / this.values.length;
let yScale = 0;
let yOffset = box.y + 0.5 * box.height;
if (this.minValue != this.maxValue) {
yOffset = box.y + box.height;
yScale = box.height / (this.maxValue - this.minValue);
}
for (let rule of this.verticalRules) {
let x = box.x + rule.frame * xScale;
context.beginPath();
context.moveTo(x, box.y);
context.lineTo(x, box.y + box.height);
context.save()
if (rule.strokeStyle != null && rule.lineWidth > 0) {
context.strokeStyle = rule.strokeStyle;
context.lineWidth = rule.lineWidth;
}
context.stroke();
context.restore();
}
let x = box.x;
let y = yOffset - (this.values[0] - this.minValue) * yScale;
context.beginPath();
context.moveTo(x, y);
for (let value of this.values.slice(1)) {
x += xScale;
y = yOffset - (value - this.minValue) * yScale;
context.lineTo(x, y);
}
context.save()
if (this.strokeStyle != null && this.lineWidth > 0) {
context.strokeStyle = this.strokeStyle;
context.lineWidth = this.lineWidth;
}
context.stroke();
context.restore();
x = box.x + xScale * frameIndex;
y = yOffset - (this.values[frameIndex] - this.minValue) * yScale;
context.beginPath();
context.arc(x, y, Sparkline.MARKER_RADIUS * window.devicePixelRatio, 0.0, 2.0 * Math.PI, false);
context.save();
if (this.strokeStyle != null) {
context.fillStyle = this.strokeStyle;
}
context.fill();
context.restore();
}
ValueAt(frameIndex: number): number {
return this.values[frameIndex];
}
}
export default class Graph extends CanvasBase {
context: CanvasRenderingContext2D = null; // Rendering context
backgroundStyle: string; // Background color
fontFamily: string;
nameSizeInPixels: number;
nameAlign: string;
valueSizeInPixels: number;
valueAlign: string;
sparklines: Sparkline[] = []; // The sparklines
margin: Margin = null;
static readonly PADDING = 7;
constructor(canvasId: string, public frameRate: number, public width: number, public height: number, objectCache: ObjectCache, public SetStatus: (status: string) => void, public SetWarning: (message: string) => void, public RequestRedraw: () => void, public ReportFrameIdChange: (canvasId: string, frameId: string) => void) {
// Base class constructor
super(canvasId, frameRate, width, height, objectCache, SetStatus, SetWarning, RequestRedraw, ReportFrameIdChange);
// Setup 2D context for drawing
this.context = this.htmlCanvas.getContext('2d');
this.backgroundStyle = "#000000";
this.margin = new Margin(10, 10, 10, 10);
this.nameAlign = "left";
this.valueAlign = "right";
// Start render loop
this.StartRenderLoop();
}
// Execute a single canvas command
ExecuteCanvasCommand(command: any) {
switch (command["CommandType"]) {
case "SetBackgroundStyle":
this.backgroundStyle = String(command["Value"]);
break;
case "SetTextStyle":
this.fontFamily = String(command["FontFamily"])
this.nameSizeInPixels = Number(command["NameSizeInPixels"])
this.nameAlign = String(command["NameAlign"])
this.valueSizeInPixels = Number(command["ValueSizeInPixels"])
this.valueAlign = String(command["ValueAlign"])
break;
case "SetMargin":
let margin = command["Value"];
let top = Number(margin["Top"]);
let right = Number(margin["Right"]);
let bottom = Number(margin["Bottom"]);
let left = Number(margin["Left"]);
this.margin = new Margin(top, right, bottom, left);
break;
case "AddSparkline":
let name = String(command["Name"]);
let values = Misc.Base64ToFloat32Array(command["ValueBuffer"]);
let strokeStyle = String(command["StrokeStyle"]);
let lineWidth = Number(command["LineWidth"]);
let verticalRules: VerticalRule[] = []
for (let rule of command["VerticalRules"]) {
verticalRules.push(new VerticalRule(Number(rule["FrameIndex"]), String(rule["StrokeStyle"]), Number(rule["LineWidth"])));
}
if (this.sparklines.length == 0) {
for (let i = 0; i < values.length; ++i) {
this.AddFrame(i.toString());
}
}
this.sparklines.push(new Sparkline(name, values, strokeStyle, lineWidth, verticalRules));
break;
default:
super.ExecuteCanvasCommand(command);
break;
}
}
AllocateFrame() {
}
DeallocateFrame(frameIndex: number) {
}
// Render scene method
Render() {
// Clear
this.context.fillStyle = this.backgroundStyle;
this.context.fillRect(0, 0, this.htmlCanvas.clientWidth * window.devicePixelRatio, this.htmlCanvas.clientHeight * window.devicePixelRatio);
let width = this.htmlCanvas.clientWidth - this.margin.left - this.margin.right;
let height = this.htmlCanvas.clientHeight - this.margin.top - this.margin.bottom;
let left = this.margin.left * window.devicePixelRatio;
let top = this.margin.top * window.devicePixelRatio;
width *= window.devicePixelRatio;
height *= window.devicePixelRatio;
let layout = new Layout();
layout.center = new Rect();
let nameFont = this.nameSizeInPixels + "px " + this.fontFamily;
let valueFont = this.valueSizeInPixels + "px " + this.fontFamily;
let nameColumnWidth = 0;
let valueColumnWidth = 0;
for (let sparkline of this.sparklines) {
this.context.font = nameFont;
let nameSize = this.context.measureText(sparkline.name).width;
this.context.font = valueFont;
let minValSize = this.context.measureText(sparkline.minValue.toFixed(2)).width;
let maxValSize = this.context.measureText(sparkline.maxValue.toFixed(2)).width;
nameColumnWidth = Math.max(nameSize, nameColumnWidth);
valueColumnWidth = Math.max(minValSize, maxValSize, valueColumnWidth);
}
nameColumnWidth *= window.devicePixelRatio;
let nameHeight = this.nameSizeInPixels * window.devicePixelRatio;
valueColumnWidth *= window.devicePixelRatio;
let valueHeight = this.valueSizeInPixels * window.devicePixelRatio;
let rowHeight = height / this.sparklines.length;
let rowTop = top;
for (let sparkline of this.sparklines) {
let layout = new Layout();
let nameRect = new Rect(0, 0, nameColumnWidth, nameHeight);
let nameLayout = new Layout({ left: nameRect });
switch (this.nameAlign) {
case "top":
layout.top = nameLayout;
break;
case "bottom":
layout.bottom = nameLayout;
break;
case "left":
layout.left = nameLayout;
break;
case "right":
layout.right = nameLayout;
break;
}
let valueRect = new Rect(0, 0, valueColumnWidth, valueHeight);
switch (this.valueAlign) {
case "top":
if (layout.top instanceof Layout) {
layout.top.right = valueRect;
} else {
layout.top = new Layout({ right: valueRect })
}
break;
case "bottom":
if (layout.bottom instanceof Layout) {
layout.bottom.right = valueRect;
} else {
layout.bottom = new Layout({ right: valueRect })
}
break;
case "left":
if (layout.left instanceof Layout) {
layout.left.right = valueRect;
} else {
layout.left = new Layout({ right: valueRect })
}
break;
case "right":
if (layout.right instanceof Layout) {
layout.right.right = valueRect;
} else {
layout.right = new Layout({ right: valueRect })
}
break;
}
layout.Layout(width, rowHeight);
layout.Move(left, rowTop);
nameRect.DrawText(this.context, sparkline.name, sparkline.strokeStyle, nameFont);
valueRect.DrawText(this.context, sparkline.ValueAt(this.currentFrameIndex).toFixed(2),
sparkline.strokeStyle, valueFont);
sparkline.Draw(this.context, (layout.center as Rect).Pad(Graph.PADDING * window.devicePixelRatio),
this.currentFrameIndex);
rowTop += rowHeight;
}
}
}