|
1 | 1 | import {isNullOrUndef, resolve} from '../helpers'; |
2 | 2 |
|
| 3 | +function lttbDecimation(data, availableWidth, options) { |
| 4 | + /** |
| 5 | + * Implementation of the Largest Triangle Three Buckets algorithm. |
| 6 | + * |
| 7 | + * This implementation is based on the original implementation by Sveinn Steinarsson |
| 8 | + * in https://github.com/sveinn-steinarsson/flot-downsample/blob/master/jquery.flot.downsample.js |
| 9 | + * |
| 10 | + * The original implementation is MIT licensed. |
| 11 | + */ |
| 12 | + const samples = options.samples || availableWidth; |
| 13 | + const decimated = []; |
| 14 | + |
| 15 | + const bucketWidth = (data.length - 2) / (samples - 2); |
| 16 | + let sampledIndex = 0; |
| 17 | + let a = 0; |
| 18 | + let i, maxAreaPoint, maxArea, area, nextA; |
| 19 | + decimated[sampledIndex++] = data[a]; |
| 20 | + |
| 21 | + for (i = 0; i < samples - 2; i++) { |
| 22 | + let avgX = 0; |
| 23 | + let avgY = 0; |
| 24 | + let j; |
| 25 | + const avgRangeStart = Math.floor((i + 1) * bucketWidth) + 1; |
| 26 | + const avgRangeEnd = Math.min(Math.floor((i + 2) * bucketWidth) + 1, data.length); |
| 27 | + const avgRangeLength = avgRangeEnd - avgRangeStart; |
| 28 | + |
| 29 | + for (j = avgRangeStart; j < avgRangeEnd; j++) { |
| 30 | + avgX = data[j].x; |
| 31 | + avgY = data[j].y; |
| 32 | + } |
| 33 | + |
| 34 | + avgX /= avgRangeLength; |
| 35 | + avgY /= avgRangeLength; |
| 36 | + |
| 37 | + const rangeOffs = Math.floor(i * bucketWidth) + 1; |
| 38 | + const rangeTo = Math.floor((i + 1) * bucketWidth) + 1; |
| 39 | + const {x: pointAx, y: pointAy} = data[a]; |
| 40 | + |
| 41 | + // Note that this is changed from the original algorithm which initializes these |
| 42 | + // values to 1. The reason for this change is that if the area is small, nextA |
| 43 | + // would never be set and thus a crash would occur in the next loop as `a` would become |
| 44 | + // `undefined`. Since the area is always positive, but could be 0 in the case of a flat trace, |
| 45 | + // initializing with a negative number is the correct solution. |
| 46 | + maxArea = area = -1; |
| 47 | + |
| 48 | + for (j = rangeOffs; j < rangeTo; j++) { |
| 49 | + area = 0.5 * Math.abs( |
| 50 | + (pointAx - avgX) * (data[j].y - pointAy) - |
| 51 | + (pointAx - data[j].x) * (avgY - pointAy) |
| 52 | + ); |
| 53 | + |
| 54 | + if (area > maxArea) { |
| 55 | + maxArea = area; |
| 56 | + maxAreaPoint = data[j]; |
| 57 | + nextA = j; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + decimated[sampledIndex++] = maxAreaPoint; |
| 62 | + a = nextA; |
| 63 | + } |
| 64 | + |
| 65 | + // Include the last point |
| 66 | + decimated[sampledIndex++] = data[data.length - 1]; |
| 67 | + |
| 68 | + return decimated; |
| 69 | +} |
| 70 | + |
3 | 71 | function minMaxDecimation(data, availableWidth) { |
4 | 72 | let avgX = 0; |
5 | 73 | let countX = 0; |
@@ -141,6 +209,9 @@ export default { |
141 | 209 | // Point the chart to the decimated data |
142 | 210 | let decimated; |
143 | 211 | switch (options.algorithm) { |
| 212 | + case 'lttb': |
| 213 | + decimated = lttbDecimation(data, availableWidth, options); |
| 214 | + break; |
144 | 215 | case 'min-max': |
145 | 216 | decimated = minMaxDecimation(data, availableWidth); |
146 | 217 | break; |
|
0 commit comments