forked from chartjs/Chart.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugin.decimation.js
135 lines (116 loc) · 3.23 KB
/
plugin.decimation.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
import {isNullOrUndef, resolve} from '../helpers';
function minMaxDecimation(data, availableWidth) {
let i, point, x, y, prevX, minIndex, maxIndex, minY, maxY;
const decimated = [];
const xMin = data[0].x;
const xMax = data[data.length - 1].x;
const dx = xMax - xMin;
for (i = 0; i < data.length; ++i) {
point = data[i];
x = (point.x - xMin) / dx * availableWidth;
y = point.y;
const truncX = x | 0;
if (truncX === prevX) {
// Determine `minY` / `maxY` and `avgX` while we stay within same x-position
if (y < minY) {
minY = y;
minIndex = i;
} else if (y > maxY) {
maxY = y;
maxIndex = i;
}
} else {
// Push up to 4 points, 3 for the last interval and the first point for this interval
if (minIndex && maxIndex) {
decimated.push(data[minIndex], data[maxIndex]);
}
if (i > 0) {
// Last point in the previous interval
decimated.push(data[i - 1]);
}
decimated.push(point);
prevX = truncX;
minY = maxY = y;
minIndex = maxIndex = i;
}
}
return decimated;
}
export default {
id: 'decimation',
defaults: {
algorithm: 'min-max',
enabled: false,
},
beforeElementsUpdate: (chart, args, options) => {
if (!options.enabled) {
return;
}
// Assume the entire chart is available to show a few more points than needed
const availableWidth = chart.width;
chart.data.datasets.forEach((dataset, datasetIndex) => {
const {_data, indexAxis} = dataset;
const meta = chart.getDatasetMeta(datasetIndex);
const data = _data || dataset.data;
if (resolve([indexAxis, chart.options.indexAxis]) === 'y') {
// Decimation is only supported for lines that have an X indexAxis
return;
}
if (meta.type !== 'line') {
// Only line datasets are supported
return;
}
const xAxis = chart.scales[meta.xAxisID];
if (xAxis.type !== 'linear' && xAxis.type !== 'time') {
// Only linear interpolation is supported
return;
}
if (chart.options.parsing) {
// Plugin only supports data that does not need parsing
return;
}
if (data.length <= 4 * availableWidth) {
// No decimation is required until we are above this threshold
return;
}
if (isNullOrUndef(_data)) {
// First time we are seeing this dataset
// We override the 'data' property with a setter that stores the
// raw data in _data, but reads the decimated data from _decimated
// TODO: Undo this on chart destruction
dataset._data = data;
delete dataset.data;
Object.defineProperty(dataset, 'data', {
configurable: true,
enumerable: true,
get: function() {
return this._decimated;
},
set: function(d) {
this._data = d;
}
});
}
// Point the chart to the decimated data
let decimated;
switch (options.algorithm) {
case 'min-max':
decimated = minMaxDecimation(data, availableWidth);
break;
default:
throw new Error(`Unsupported decimation algorithm '${options.algorithm}'`);
}
dataset._decimated = decimated;
});
},
destroy(chart) {
chart.data.datasets.forEach((dataset) => {
if (dataset._decimated) {
const data = dataset._data;
delete dataset._decimated;
delete dataset._data;
Object.defineProperty(dataset, 'data', {value: data});
}
});
}
};