-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpoly-graph.html
293 lines (245 loc) · 7.84 KB
/
poly-graph.html
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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-resizable-behavior/iron-resizable-behavior.html">
<!--
`poly-graph` is a live multi-timeseries graphing element
Key features:
* * Live animated timeseries graph
* * Supporting multiple 'pens' drawing lines or dots
* * Pens can have custom color, line width, name (TODO: render name)
* * New data points added with a timestamp or defaults to Date.now()
Wishlist (coming soon):
* * It should be possible to 'lift the pen' ~ pause
* * Adding limits/thresholds with labels
* * Pan (mouse or finger drag) and zoom (mouse scroll or pinch) on both live and static graphs
* * Pause/play timeline
* * Import data for static graph (e.g. post recording)
@demo demo/index.html
@polymerBehavior Polymer.IronResizableBehavior
-->
<dom-module id="poly-graph">
<template>
<style>
:host {
display: block;
-webkit-user-select: none;
-moz-user-select: none;
user-select: none;
}
canvas {
display: block;
}
</style>
<canvas id="polygraph" width="[[_width]]" heigh="[[_height]]"></canvas>
</template>
<script>
Polymer({
is: 'poly-graph',
properties: {
pens: {
type: Object,
value: function() { return {} },
},
guides: {
type: Object,
value: function() { return {} },
},
_width: {
type: Number,
notify: true
},
_height: {
type: Number,
notify: true
},
min: {
type: Number,
value: -1
},
max: {
type: Number,
value: 1
},
topmargin: {
type: Number,
value: 8
},
bottommargin: {
type: Number,
value: 8
},
running: {
type: Boolean,
value: true
},
/**
* Pixels per second (x-axis resolution)
*/
resolution: {
type: Number,
value: 50
}
},
behaviors: [
Polymer.IronResizableBehavior
],
listeners: {
'iron-resize': '_onIronResize',
'zoom-changed': '_onZoomChanged'
},
attached: function() {
var mdown = false;
// TODO: proper pan/zoom detect...
// this.$.polygraph.addEventListener('mousedown', function(e) {
// console.log('[mousedown]', e);
// mdown = true;
// }, true);
// this.$.polygraph.addEventListener('mouseup', function(e) {
// console.log('[mouseup]', e);
// mdown = false;
// }, true);
// this.$.polygraph.addEventListener('mousemove', function(e) {
// //if(mdown)console.log('[mousedrag]', e);
// },true);
// document.addEventListener('mouseup', function(e) {
// console.log('[mouseup (shadow)]', e);
// mdown = false;
// });
this.async(this.notifyResize, 1);
this._gameLoop();
},
addPen: function(id, options) {
if(this.pens[id]) {
console.log("Pen already added: ", id);
}
this.pens[id] = {
color:options.color ? options.color : '#000',
name:options.name ? options.name : "",
line:!options.dotsOnly,
lineWidth:options.lineWidth ? options.lineWidth : 1,
data:[]
};
},
addGuide: function(id, options) {
if(this.guides[id]) {
console.log("Guide already added: ", id);
return;
}
if(!options || options.min === undefined) {
console.log("Guide needs at least a 'min' value defined");
return;
}
this.guides[id] = {
color:options.color ? options.color : '#888',
name:options.name ? options.name : "",
min:options.min,
height:options.height
};
},
addValue: function(id, value, timestamp) {
var pen = this.pens[id];
if(!pen)
return;
if(!timestamp) timestamp = Date.now(); // Maybe: performance.timing.navigationStart + performance.now();
// time must go forward
if(pen.data.length > 0 && pen.data[pen.data.length-1].t >= timestamp)
return;
pen.data.push({v:value, t:timestamp});
},
_onIronResize: function() {
if(this.offsetWidth == this._width && this.offsetHeight == this._height)
return;
// TBD: resize shouldn't happen too often - maybe debounce is overkill to prevent too much flickering, etc.
this.debounce('resizeDebounce', function() {
console.log(this.offsetWidth, this.offsetHeight);
this.$.polygraph.width = this.offsetWidth;
this.$.polygraph.height = this.offsetHeight;
this._recalcFactors();
}.bind(this), 100);
},
_onZoomChanged: function(val) {
this._pixelsPerSecond = 100 * this.zoom;
},
_recalcFactors: function() {
this._ya = -(this.$.polygraph.height - this.bottommargin - this.topmargin) / (this.max - this.min);
this._yb = this.topmargin - this._ya * this.max;
},
_calcY: function(val) {
return this._ya * val + this._yb
},
_calcPos: function(point, pos, now) {
pos.x = this.$.polygraph.width - 5 - (now - point.t) * 0.001 * this.resolution;
pos.y = this._ya * point.v + this._yb;
},
_gameLoop: function(ts) {
if(this.offsetWidth == this._width && this.offsetHeight == this._height)
this._repaint(Date.now()); // Maybe: performance.timing.navigationStart + ts
else {
this._width = this.offsetWidth;
this._height = this.offsetHeight;
this._onIronResize();
}
if(this.running)
requestAnimationFrame(this._gameLoop.bind(this));
},
_repaint: function(now) {
var canvas = this.$.polygraph;
var ctx = canvas.getContext('2d');
ctx.clearRect(0,0,canvas.width, canvas.height);
var id, pen, guide, ptr, point, pos = {}, dots = [];
// Draw guides
for(id in this.guides) {
guide = this.guides[id];
if(guide.height !== undefined) {
ctx.fillStyle = guide.color;
ctx.fillRect(0,this._calcY(guide.min),canvas.width,this._ya * guide.height);
} else {
ctx.beginPath();
ctx.strokeStyle = guide.color;
ctx.moveTo(0, this._calcY(guide.min));
ctx.lineTo(canvas.width, this._calcY(guide.min));
ctx.stroke();
}
}
// Draw pens
for(id in this.pens) {
pen = this.pens[id];
if(pen.data && pen.data.length > 1) {
ctx.beginPath();
ctx.lineWidth = pen.lineWidth;
ptr = pen.data.length-1;
this._calcPos(pen.data[ptr], pos, now);
if(pen.line) {
ctx.strokeStyle = pen.color;
ctx.moveTo(pos.x, pos.y);
// if(now - pos.t < 100) {
dots.push({x:canvas.width-5,y:pos.y,color:pen.color});
// }
} else {
ctx.fillStyle = pen.color;
}
while(ptr >= 0 && pos.x > 0) {
this._calcPos(pen.data[ptr], pos, now);
if(pen.line)
ctx.lineTo(pos.x, pos.y);
else {
ctx.beginPath();
ctx.arc(pos.x,pos.y,4,0,Math.PI*2,false);
ctx.fill();
}
ptr--;
}
if(pen.line)
ctx.stroke();
}
}
ctx.lineWidth = 1.0;
dots.forEach(function(dot) {
ctx.beginPath();
ctx.arc(dot.x,dot.y,4,0,Math.PI*2,false);
ctx.fillStyle = dot.color;
ctx.fill();
})
}
});
</script>
</dom-module>