Skip to content

Commit

Permalink
Apply segment styles with only single segment (#9447)
Browse files Browse the repository at this point in the history
  • Loading branch information
kurkle committed Jul 20, 2021
1 parent ba2f158 commit 92e39a2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/helpers/helpers.segment.js
Expand Up @@ -244,14 +244,15 @@ export function _computeSegments(line, segmentOptions) {

const loop = !!line._loop;
const {start, end} = findStartAndEnd(points, count, loop, spanGaps);
const baseStyle = readStyle(line.options);

if (spanGaps === true) {
return splitByStyles([{start, end, loop}], points, segmentOptions);
return splitByStyles([{start, end, loop}], points, baseStyle, segmentOptions);
}

const max = end < start ? end + count : end;
const completeLoop = !!line._fullLoop && start === 0 && end === count - 1;
return splitByStyles(solidSegments(points, start, max, completeLoop), points, segmentOptions);
return splitByStyles(solidSegments(points, start, max, completeLoop), points, baseStyle, segmentOptions);
}

/**
Expand All @@ -260,11 +261,11 @@ export function _computeSegments(line, segmentOptions) {
* @param {object} [segmentOptions]
* @return {Segment[]}
*/
function splitByStyles(segments, points, segmentOptions) {
function splitByStyles(segments, points, baseStyle, segmentOptions) {
if (!segmentOptions || !segmentOptions.setContext || !points) {
return segments;
}
return doSplitByStyles(segments, points, segmentOptions);
return doSplitByStyles(segments, points, baseStyle, segmentOptions);
}

/**
Expand All @@ -273,14 +274,15 @@ function splitByStyles(segments, points, segmentOptions) {
* @param {object} [segmentOptions]
* @return {Segment[]}
*/
function doSplitByStyles(segments, points, segmentOptions) {
function doSplitByStyles(segments, points, baseStyle, segmentOptions) {
const count = points.length;
const result = [];
let start = segments[0].start;
let i = start;
for (const segment of segments) {
let prevStyle, style;
let prevStyle = baseStyle;
let prev = points[start % count];
let style;
for (i = start + 1; i <= segment.end; i++) {
const pt = points[i % count];
style = readStyle(segmentOptions.setContext({type: 'segment', p0: prev, p1: pt}));
Expand Down
27 changes: 27 additions & 0 deletions test/fixtures/controller.line/segments/single.js
@@ -0,0 +1,27 @@
module.exports = {
config: {
type: 'line',
data: {
labels: ['a', 'b', 'c', 'd', 'e', 'f'],
datasets: [{
data: [1, 2, 3, 3, 2, 1],
borderColor: 'black',
segment: {
borderColor: 'red',
}
}]
},
options: {
scales: {
x: {display: false},
y: {display: false}
}
}
},
options: {
canvas: {
width: 256,
height: 256
}
}
};
Binary file added test/fixtures/controller.line/segments/single.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 92e39a2

Please sign in to comment.