MCPcopy Create free account
hub / github.com/chartjs/Chart.js / minMaxDecimation

Function minMaxDecimation

src/plugins/plugin.decimation.js:82–154  ·  view source on GitHub ↗
(data, start, count, availableWidth)

Source from the content-addressed store, hash-verified

80}
81
82function minMaxDecimation(data, start, count, availableWidth) {
83 let avgX = 0;
84 let countX = 0;
85 let i, point, x, y, prevX, minIndex, maxIndex, startIndex, minY, maxY;
86 const decimated = [];
87 const endIndex = start + count - 1;
88
89 const xMin = data[start].x;
90 const xMax = data[endIndex].x;
91 const dx = xMax - xMin;
92
93 for (i = start; i < start + count; ++i) {
94 point = data[i];
95 x = (point.x - xMin) / dx * availableWidth;
96 y = point.y;
97 const truncX = x | 0;
98
99 if (truncX === prevX) {
100 // Determine `minY` / `maxY` and `avgX` while we stay within same x-position
101 if (y < minY) {
102 minY = y;
103 minIndex = i;
104 } else if (y > maxY) {
105 maxY = y;
106 maxIndex = i;
107 }
108 // For first point in group, countX is `0`, so average will be `x` / 1.
109 // Use point.x here because we're computing the average data `x` value
110 avgX = (countX * avgX + point.x) / ++countX;
111 } else {
112 // Push up to 4 points, 3 for the last interval and the first point for this interval
113 const lastIndex = i - 1;
114
115 if (!isNullOrUndef(minIndex) && !isNullOrUndef(maxIndex)) {
116 // The interval is defined by 4 points: start, min, max, end.
117 // The starting point is already considered at this point, so we need to determine which
118 // of the other points to add. We need to sort these points to ensure the decimated data
119 // is still sorted and then ensure there are no duplicates.
120 const intermediateIndex1 = Math.min(minIndex, maxIndex);
121 const intermediateIndex2 = Math.max(minIndex, maxIndex);
122
123 if (intermediateIndex1 !== startIndex && intermediateIndex1 !== lastIndex) {
124 decimated.push({
125 ...data[intermediateIndex1],
126 x: avgX,
127 });
128 }
129 if (intermediateIndex2 !== startIndex && intermediateIndex2 !== lastIndex) {
130 decimated.push({
131 ...data[intermediateIndex2],
132 x: avgX
133 });
134 }
135 }
136
137 // lastIndex === startIndex will occur when a range has only 1 point which could
138 // happen with very uneven data
139 if (i > 0 && lastIndex !== startIndex) {

Callers 1

Calls 1

isNullOrUndefFunction · 0.85

Tested by

no test coverage detected