-
Notifications
You must be signed in to change notification settings - Fork 3
/
jquery.flot.historybuffer.numeric.js
406 lines (320 loc) · 12.3 KB
/
jquery.flot.historybuffer.numeric.js
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
/* history buffer data structure for charting.
Copyright (c) 2007-2015 National Instruments
Licensed under the MIT license.
*/
/*globals CBuffer, SegmentTree, module*/
/**
# HistoryBuffer
> A historyBuffer is a data structure that enables efficient charting operations
on a sliding window of data points.
In the case of large data buffers it is inefficient to draw every point of the
chart. Doing this results in many almost vertical lines drawn over the same
stripe of pixels over and over again. Drawing a line on a canvas is an expensive
operation that must be avoided if possible.
One method of avoiding the repeated drawing is to reduce the amount of data points
we draw on the chart by sub-sampling the data, also called decimation.
There are many ways to decimate the data; the one this history buffer implements
is to divide data into "1 pixel wide buckets" and then for each bucket select the
maximum and minimum as subsamples. This method results in a drawing that looks
visually similar with the one in which all samples are drawn.
The history buffer is a circular buffer holding the chart data accompanied by an
acceleration structure - a segment tree of min/max values.
The segment tree is only enabled for big history buffers.
Example:
```javascript
var hb1 = new HistoryBuffer(1024);
// in a history buffer with width 1 we can push scalars
hb1.push(1);
hb1.push(2);
console.log(hb1.toArray()); //[1, 2]
// as well as 1 elements arrays
hb1.push([3]);
hb1.push([4]);
console.log(hb1.toArray()); //[1, 2, 3, 4]
// or append an array
hb1.appendArray([5, 6]);
console.log(hb1.toArray()); //[1, 2, 3, 4, 5, 6]
```
The history buffer is able to store multiple "parallel" data sets. Example:
```javascript
var hb2 = new HistoryBuffer(1024, 2);
// in a history buffer with width > 1 we can only push arrays
hb2.push([1, 11]);
hb2.push([2, 12]);
hb2.push([3, 13]);
console.log(hb2.toArray()); //[[1, 11], [2, 12], [3, 13]]
// or append an array of arrays
hb2.appendArray([[4, 14], [5, 15], [6, 16]]);
console.log(hb2.toArray()); //[[1, 11], [2, 12], [3, 13], [4, 14], [5, 15], [6, 16]]
```
Operations accelerated by a historyBuffer
-----------------------------------------
The common charting operations performed on a history buffer are
* inserting elements at the head
* inserting m elements at the head
* deleting elements at the tail
* deleting m elements at the tail
* compute min/max on a range
* query for a "visually interesting" data subsample on a range
*/
/** ## HistoryBuffer methods*/
(function (global) {
'use strict';
/* The branching factor determines how many samples are decimated in a tree node.
* It affects the performance and the overhead of the tree.
*/
var defaultBranchFactor = 32; // 32 for now. TODO tune the branching factor.
/** **HistoryBuffer(capacity, width)** - the History buffer constructor creates
a new history buffer with the specified capacity (default: 1024) and width (default: 1)*/
var HistoryBufferNumeric = function (capacity, width) {
this.capacity = capacity || 1024;
this.width = width || 1;
this.lastUpdatedIndex = 0;
this.firstUpdatedIndex = 0;
this.branchFactor = defaultBranchFactor;
this.buffers = []; // circular buffers for data
this.trees = []; // segment trees
for (var i = 0; i < this.width; i++) {
this.buffers.push(new CBuffer(capacity));
this.trees.push(new SegmentTree(this, this.buffers[i]));
}
this.buffer = this.buffers[0];
this.tree = this.trees[0];
this.count = 0;
this.callOnChange = undefined;
this.changed = false;
};
HistoryBufferNumeric.prototype.setBranchingFactor = function (b) {
this.branchFactor = b;
this.rebuildSegmentTrees();
};
HistoryBufferNumeric.prototype.getDefaultBranchingFactor = function () {
return defaultBranchFactor;
};
HistoryBufferNumeric.prototype.rebuildSegmentTrees = function () {
this.trees = []; // new segment trees
for (var i = 0; i < this.width; i++) {
this.trees.push(new SegmentTree(this, this.buffers[i]));
}
this.tree = this.trees[0];
this.firstUpdatedIndex = this.startIndex();
this.lastUpdatedIndex = this.firstUpdatedIndex;
this.updateSegmentTrees();
};
/** **clear()** - clears the history buffer */
HistoryBufferNumeric.prototype.clear = function () {
for (var i = 0; i < this.width; i++) {
this.buffers[i].empty();
}
this.count = 0; // todo fire changes and upate lastindex, startindex
this.rebuildSegmentTrees();
this.changed = true;
if (this.callOnChange) {
this.callOnChange();
}
};
/** **setCapacity(newCapacity)** changes the capacity of the History Buffer and clears all the data inside it */
HistoryBufferNumeric.prototype.setCapacity = function (newCapacity) {
if (newCapacity !== this.capacity) {
this.capacity = newCapacity;
this.buffers = []; // circular buffers for data
for (var i = 0; i < this.width; i++) {
this.buffers.push(new CBuffer(newCapacity));
}
this.buffer = this.buffers[0];
this.count = 0; // todo fire changes and upate lastindex, startindex
this.rebuildSegmentTrees();
this.changed = true;
if (this.callOnChange) {
this.callOnChange();
}
}
};
/** **setWidth(newWidth)** - changes the width of the History Buffer and clears
all the data inside it */
HistoryBufferNumeric.prototype.setWidth = function (newWidth) {
if (newWidth !== this.width) {
this.width = newWidth;
this.buffers = []; // clear the circular buffers for data. TODO reuse the buffers
for (var i = 0; i < this.width; i++) {
this.buffers.push(new CBuffer(this.capacity));
}
this.buffer = this.buffers[0];
this.count = 0; // todo fire changes and upate lastindex, startindex
this.rebuildSegmentTrees();
this.changed = true;
if (this.callOnChange) {
this.callOnChange();
}
}
};
/* store an element in the history buffer, don't update stats */
HistoryBufferNumeric.prototype.pushNoStatsUpdate = function (item) {
if (typeof item === 'number' && this.width === 1) {
this.buffer.push(item);
} else {
if (Array.isArray(item) && item.length === this.width) {
for (var i = 0; i < this.width; i++) {
this.buffers[i].push(item[i]);
}
}
}
};
/** **push(item)** - adds an element to the history buffer */
HistoryBufferNumeric.prototype.push = function (item) {
this.pushNoStatsUpdate(item);
this.count++;
this.changed = true;
if (this.callOnChange) {
this.callOnChange();
}
};
/** **startIndex()** - returns the index of the oldest element in the buffer*/
HistoryBufferNumeric.prototype.startIndex = function () {
return Math.max(0, this.count - this.capacity);
};
/** **lastIndex()** - returns the index of the newest element in the buffer*/
HistoryBufferNumeric.prototype.lastIndex = function () {
return this.startIndex() + this.buffer.size;
};
/** **get(n)** - returns the nth element in the buffer*/
HistoryBufferNumeric.prototype.get = function (index) {
index -= this.startIndex();
if (this.width === 1) {
return this.buffer.get(index);
} else {
var res = [];
for (var i = 0; i < this.width; i++) {
res.push(this.buffers[i].get(index));
}
return res;
}
};
/** **appendArray(arr)** - appends an array of elements to the buffer*/
HistoryBufferNumeric.prototype.appendArray = function (arr) {
for (var i = 0; i < arr.length; i++) {
this.pushNoStatsUpdate(arr[i]);
}
this.count += arr.length;
this.changed = true;
if (this.callOnChange) {
this.callOnChange();
}
};
/** **toArray()** - returns the content of the history buffer as an array */
HistoryBufferNumeric.prototype.toArray = function () {
if (this.width === 1) {
return this.buffer.toArray();
} else {
var start = this.startIndex(),
last = this.lastIndex(),
res = [];
for (var i = start; i < last; i++) {
res.push(this.get(i));
}
return res;
}
};
/* update the segment tree with the newly added values*/
HistoryBufferNumeric.prototype.updateSegmentTrees = function () {
var buffer = this.buffer;
this.trees.forEach(function (tree) {
tree.updateSegmentTree();
});
this.firstUpdatedIndex = this.startIndex();
this.lastUpdatedIndex = this.firstUpdatedIndex + buffer.size;
};
/** **toDataSeries()** - returns the content of the history buffer into a
flot data series*/
HistoryBufferNumeric.prototype.toDataSeries = function (index) {
var buffer = this.buffer;
var data = [];
var start = this.startIndex();
for (var i = 0; i < buffer.size; i++) {
data.push([i + start, this.buffers[index || 0].get(i)]);
}
return data;
};
HistoryBufferNumeric.prototype.onChange = function (f) {
this.callOnChange = f;
};
/** **query(start, end, step, index)** - decimates the data set at the
provided *index*, starting at the start sample, ending at the end sample
with the provided step */
HistoryBufferNumeric.prototype.query = function (start, end, step, index) {
if (index === undefined) {
index = 0;
}
if (this.changed) {
this.updateSegmentTrees();
this.changed = false;
}
return this.trees[index].query(start, end, step);
};
/** **rangeX( index)** - returns the range of the data in the buffer*/
HistoryBufferNumeric.prototype.rangeX = function (index) {
var start = this.startIndex(),
end = this.lastIndex()-1;
if (end === start - 1) {
return {};
}
return { xmin: start,
xmax: end,
deltamin: 1
};
};
/** **rangeY(start, end, index)** - returns the range of the data
in a given interval of the buffer*/
HistoryBufferNumeric.prototype.rangeY = function (start, end, index) {
if (start === null || start === undefined){
start = this.startIndex();
}
if (end === null || end === undefined){
end = this.lastIndex()-1;
}
if (index === null || index === undefined) {
index = 0;
}
if (this.changed) {
this.updateSegmentTrees();
this.changed = false;
}
var data = this.query(start, end, end - start, index),
dataLength = data.length;
if (dataLength > 0) {
var res = {
ymin: Infinity,
ymax: -Infinity
};
for (var i = 0; i < dataLength; i+=2) {
res.ymin = Math.min(res.ymin, data[i+1]);
res.ymax = Math.max(res.ymax, data[i+1]);
}
return res;
}
return { };
};
HistoryBufferNumeric.prototype.toJSON = function() {
var serializedHb = {
data: [],
width: this.width,
capacity: this.capacity,
valueType: 'HistoryBuffer',
startIndex: this.startIndex(),
count: this.count
};
if(this.width === 1) {
serializedHb['data'] = this.buffer.toArray();
} else {
for (var i = 0; i < this.width; i++) {
serializedHb['data'].push(this.buffers[i].toArray());
}
}
return serializedHb;
};
if (typeof module === 'object' && module.exports) {
module.exports = HistoryBufferNumeric;
} else {
global.HistoryBufferNumeric = HistoryBufferNumeric;
}
})(this);