I am trying to do the same thing provided in this SO answer, https://stackoverflow.com/a/66943726/1108057
Unfortunately that answer is for older versions of Chart.js and a lot of the needed elements are not supported in the new version, such as accessing _meta
properties directly.
I was able to figure out how to get meta of the dataset in the new version, but I can't figure out the remaining part of it as I don't know what it was referencing in the previous version.
plugins: [{
id: 'redLineLoss',
beforeRender: (x, options) => {
const c = x;
const dataset = x.data.datasets[1];
const yScale = x.scales['y'];
const yPos = yScale.getPixelForValue(0);
const gradientFill = c.ctx.createLinearGradient(0, 0, 0, c.height);
gradientFill.addColorStop(0, 'rgb(86,188,77)');
gradientFill.addColorStop(yPos / c.height, 'rgb(86,188,77)');
gradientFill.addColorStop(yPos / c.height, 'rgb(229,66,66)');
gradientFill.addColorStop(1, 'rgb(229,66,66)');
const datasetMeta = x.getDatasetMeta(1);
console.log(datasetMeta['data']);
// STUCK HERE -- what do I get from the dataset meta now?
//console.log(datasetMeta.dataset.getProps(['x', 'y']));
//console.log(model[Object.keys(x.getDatasetMeta(1))]);
//const model = x.data.datasets[1]._meta[Object.keys(dataset._meta)[1]].dataset._model;
//model.borderColor = gradientFill;
},
}],
I want to only apply this to a specific dataset in the chart, not all of them. That's why I am selecting the dataset with key 1
.