Skip to content

Commit 137b51d

Browse files
authored
Fix line segments with alignToPixel (#9042)
1 parent 1a1e677 commit 137b51d

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

src/helpers/helpers.segment.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ export function _boundSegment(segment, points, bounds) {
100100
}
101101

102102
value = normalize(point[property]);
103+
104+
if (value === prevValue) {
105+
continue;
106+
}
107+
103108
inside = between(value, startBound, endBound);
104109

105110
if (subStart === null && shouldStart()) {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
module.exports = {
2+
config: {
3+
type: 'line',
4+
data: {
5+
datasets: [
6+
{
7+
data: [
8+
{x: 0, y: 0},
9+
{x: 1, y: 20},
10+
{x: 1.00001, y: 30},
11+
{x: 2, y: 100},
12+
{x: 2.00001, y: 100}
13+
],
14+
backgroundColor: '#FF000070',
15+
borderColor: 'black',
16+
radius: 0,
17+
segment: {
18+
borderDash: ctx => ctx.p0.parsed.x > 1 ? [10, 5] : undefined,
19+
},
20+
fill: true
21+
}
22+
]
23+
},
24+
options: {
25+
plugins: {
26+
legend: false
27+
},
28+
scales: {
29+
x: {
30+
type: 'linear',
31+
alignToPixels: true,
32+
display: false
33+
},
34+
y: {
35+
display: false
36+
}
37+
}
38+
}
39+
},
40+
options: {
41+
canvas: {
42+
width: 300,
43+
height: 240
44+
}
45+
}
46+
};
5.78 KB
Loading

test/specs/helpers.segment.tests.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,13 @@ describe('helpers.segments', function() {
4949
{start: 3, end: 4, loop: false, style: undefined},
5050
]);
5151
});
52+
53+
it('should find correct segments when there are multiple points with same property value', function() {
54+
const repeatedPoints = [{x: 1, y: 5}, {x: 1, y: 6}, {x: 2, y: 5}, {x: 2, y: 6}, {x: 3, y: 5}, {x: 3, y: 6}, {x: 3, y: 7}];
55+
expect(_boundSegment({start: 0, end: 6, loop: false}, repeatedPoints, {property: 'x', start: 1, end: 1.1})).toEqual([{start: 0, end: 2, loop: false, style: undefined}]);
56+
expect(_boundSegment({start: 0, end: 6, loop: false}, repeatedPoints, {property: 'x', start: 2, end: 2.1})).toEqual([{start: 2, end: 4, loop: false, style: undefined}]);
57+
expect(_boundSegment({start: 0, end: 6, loop: false}, repeatedPoints, {property: 'x', start: 2, end: 3.1})).toEqual([{start: 2, end: 6, loop: false, style: undefined}]);
58+
expect(_boundSegment({start: 0, end: 6, loop: false}, repeatedPoints, {property: 'x', start: 0, end: 8})).toEqual([{start: 0, end: 6, loop: false, style: undefined}]);
59+
});
5260
});
5361
});

0 commit comments

Comments
 (0)