Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable applying of gradients and pattern on line segments #11217

Merged
merged 3 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/helpers/helpers.segment.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {_angleBetween, _angleDiff, _isBetween, _normalizeAngle} from './helpers.math.js';
import {createContext} from './helpers.options.js';
import {isPatternOrGradient} from './helpers.color.js';

/**
* @typedef { import('../elements/element.line.js').default } LineElement
Expand Down Expand Up @@ -346,5 +347,18 @@ function readStyle(options) {
}

function styleChanged(style, prevStyle) {
return prevStyle && JSON.stringify(style) !== JSON.stringify(prevStyle);
if (!prevStyle) {
return false;
}
const cache = [];
const replacer = function(key, value) {
if (!isPatternOrGradient(value)) {
return value;
}
if (!cache.includes(value)) {
cache.push(value);
}
return cache.indexOf(value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A map could be more efficient in these. But I'm not really worried about that. My main worry is a confusion between index in the array and some other possible numeric value, which is probably a non-issue really.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking to use a map but the array could contain max 4 objects.
Not clear about the confusion. The index is used only to compare a gradient/pattern. We needed a unique value per object instance to put in the json. It seemed to me better than a string or object.

};
return JSON.stringify(style, replacer) !== JSON.stringify(prevStyle, replacer);
}
34 changes: 34 additions & 0 deletions test/fixtures/controller.line/segments/gradient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const getGradient = (context) => {
const {chart, p0, p1} = context;
const ctx = chart.ctx;
const {x: x0} = p0.getProps(['x'], true);
const {x: x1} = p1.getProps(['x'], true);
const gradient = ctx.createLinearGradient(x0, 0, x1, 0);
gradient.addColorStop(0, p0.options.backgroundColor);
gradient.addColorStop(1, p1.options.backgroundColor);
return gradient;
};

module.exports = {
config: {
type: 'line',
data: {
datasets: [{
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}],
pointBackgroundColor: ['red', 'yellow', 'green', 'green', 'blue', 'pink', 'blue'],
pointBorderWidth: 0,
pointRadius: 10,
borderWidth: 5,
segment: {
borderColor: getGradient,
}
}]
},
options: {
scales: {
x: {type: 'linear', display: false},
y: {display: false}
}
}
}
};
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.