MCPcopy
hub / github.com/chartjs/Chart.js / doSplitByStyles

Function doSplitByStyles

src/helpers/helpers.segment.js:279–335  ·  view source on GitHub ↗

* @param {LineElement} line * @param {Segment[]} segments * @param {PointElement[]} points * @param {object} [segmentOptions] * @return {Segment[]}

(line, segments, points, segmentOptions)

Source from the content-addressed store, hash-verified

277 * @return {Segment[]}
278 */
279function doSplitByStyles(line, segments, points, segmentOptions) {
280 const chartContext = line._chart.getContext();
281 const baseStyle = readStyle(line.options);
282 const {_datasetIndex: datasetIndex, options: {spanGaps}} = line;
283 const count = points.length;
284 const result = [];
285 let prevStyle = baseStyle;
286 let start = segments[0].start;
287 let i = start;
288
289 function addStyle(s, e, l, st) {
290 const dir = spanGaps ? -1 : 1;
291 if (s === e) {
292 return;
293 }
294 // Style can not start/end on a skipped point, adjust indices accordingly
295 s += count;
296 while (points[s % count].skip) {
297 s -= dir;
298 }
299 while (points[e % count].skip) {
300 e += dir;
301 }
302 if (s % count !== e % count) {
303 result.push({start: s % count, end: e % count, loop: l, style: st});
304 prevStyle = st;
305 start = e % count;
306 }
307 }
308
309 for (const segment of segments) {
310 start = spanGaps ? start : segment.start;
311 let prev = points[start % count];
312 let style;
313 for (i = start + 1; i <= segment.end; i++) {
314 const pt = points[i % count];
315 style = readStyle(segmentOptions.setContext(createContext(chartContext, {
316 type: 'segment',
317 p0: prev,
318 p1: pt,
319 p0DataIndex: (i - 1) % count,
320 p1DataIndex: i % count,
321 datasetIndex
322 })));
323 if (styleChanged(style, prevStyle)) {
324 addStyle(start, i - 1, segment.loop, prevStyle);
325 }
326 prev = pt;
327 prevStyle = style;
328 }
329 if (start < i - 1) {
330 addStyle(start, i - 1, segment.loop, prevStyle);
331 }
332 }
333
334 return result;
335}
336

Callers 1

splitByStylesFunction · 0.85

Calls 6

readStyleFunction · 0.85
createContextFunction · 0.85
styleChangedFunction · 0.85
addStyleFunction · 0.85
setContextMethod · 0.80
getContextMethod · 0.45

Tested by

no test coverage detected