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

Function fastPathSegment

src/elements/element.line.js:118–178  ·  view source on GitHub ↗

* Create path from points, grouping by truncated x-coordinate * Points need to be in order by x-coordinate for this to work efficiently * @param {CanvasRenderingContext2D|Path2D} ctx - Context * @param {LineElement} line * @param {object} segment * @param {number} segment.start - start index of

(ctx, line, segment, params)

Source from the content-addressed store, hash-verified

116 * @param {number} params.end - limit segment to points ending at `start` + `count` index
117 */
118function fastPathSegment(ctx, line, segment, params) {
119 const points = line.points;
120 const {count, start, ilen} = pathVars(points, segment, params);
121 const {move = true, reverse} = params || {};
122 let avgX = 0;
123 let countX = 0;
124 let i, point, prevX, minY, maxY, lastY;
125
126 const pointIndex = (index) => (start + (reverse ? ilen - index : index)) % count;
127 const drawX = () => {
128 if (minY !== maxY) {
129 // Draw line to maxY and minY, using the average x-coordinate
130 ctx.lineTo(avgX, maxY);
131 ctx.lineTo(avgX, minY);
132 // Line to y-value of last point in group. So the line continues
133 // from correct position. Not using move, to have solid path.
134 ctx.lineTo(avgX, lastY);
135 }
136 };
137
138 if (move) {
139 point = points[pointIndex(0)];
140 ctx.moveTo(point.x, point.y);
141 }
142
143 for (i = 0; i <= ilen; ++i) {
144 point = points[pointIndex(i)];
145
146 if (point.skip) {
147 // If there is a skipped point inside a segment, spanGaps must be true
148 continue;
149 }
150
151 const x = point.x;
152 const y = point.y;
153 const truncX = x | 0; // truncated x-coordinate
154
155 if (truncX === prevX) {
156 // Determine `minY` / `maxY` and `avgX` while we stay within same x-position
157 if (y < minY) {
158 minY = y;
159 } else if (y > maxY) {
160 maxY = y;
161 }
162 // For first point in group, countX is `0`, so average will be `x` / 1.
163 avgX = (countX * avgX + x) / ++countX;
164 } else {
165 drawX();
166 // Draw line to next x-position, using the first (or only)
167 // y-value in that group
168 ctx.lineTo(x, y);
169
170 prevX = truncX;
171 countX = 0;
172 minY = maxY = y;
173 }
174 // Keep track of the last y-value in group
175 lastY = y;

Callers

nothing calls this directly

Calls 3

pathVarsFunction · 0.85
pointIndexFunction · 0.85
drawXFunction · 0.85

Tested by

no test coverage detected