Skip to content

Commit ee7e928

Browse files
authored
Enable applying of gradients and pattern on line segments (#11217)
* Enable applying of gradients and pattern on line segments * add test case * improve replacer
1 parent aec1c6d commit ee7e928

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

src/helpers/helpers.segment.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {_angleBetween, _angleDiff, _isBetween, _normalizeAngle} from './helpers.math.js';
22
import {createContext} from './helpers.options.js';
3+
import {isPatternOrGradient} from './helpers.color.js';
34

45
/**
56
* @typedef { import('../elements/element.line.js').default } LineElement
@@ -346,5 +347,18 @@ function readStyle(options) {
346347
}
347348

348349
function styleChanged(style, prevStyle) {
349-
return prevStyle && JSON.stringify(style) !== JSON.stringify(prevStyle);
350+
if (!prevStyle) {
351+
return false;
352+
}
353+
const cache = [];
354+
const replacer = function(key, value) {
355+
if (!isPatternOrGradient(value)) {
356+
return value;
357+
}
358+
if (!cache.includes(value)) {
359+
cache.push(value);
360+
}
361+
return cache.indexOf(value);
362+
};
363+
return JSON.stringify(style, replacer) !== JSON.stringify(prevStyle, replacer);
350364
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const getGradient = (context) => {
2+
const {chart, p0, p1} = context;
3+
const ctx = chart.ctx;
4+
const {x: x0} = p0.getProps(['x'], true);
5+
const {x: x1} = p1.getProps(['x'], true);
6+
const gradient = ctx.createLinearGradient(x0, 0, x1, 0);
7+
gradient.addColorStop(0, p0.options.backgroundColor);
8+
gradient.addColorStop(1, p1.options.backgroundColor);
9+
return gradient;
10+
};
11+
12+
module.exports = {
13+
config: {
14+
type: 'line',
15+
data: {
16+
datasets: [{
17+
data: [{x: 0, y: 0}, {x: 1, y: 1}, {x: 2, y: 2}, {x: 3, y: 3}, {x: 4, y: 4}, {x: 5, y: 5}, {x: 6, y: 6}],
18+
pointBackgroundColor: ['red', 'yellow', 'green', 'green', 'blue', 'pink', 'blue'],
19+
pointBorderWidth: 0,
20+
pointRadius: 10,
21+
borderWidth: 5,
22+
segment: {
23+
borderColor: getGradient,
24+
}
25+
}]
26+
},
27+
options: {
28+
scales: {
29+
x: {type: 'linear', display: false},
30+
y: {display: false}
31+
}
32+
}
33+
}
34+
};
Loading

0 commit comments

Comments
 (0)