From: Jukka Kurkela Date: Sat, 3 Apr 2021 12:13:38 +0000 (+0300) Subject: Add legend events sample (#8795) X-Git-Tag: v3.1.0~43 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5af8f8859be73b65b3f6ad89735e6dca070c5ad6;p=thirdparty%2FChart.js.git Add legend events sample (#8795) * Add legend events sample * review update --- diff --git a/docs/.vuepress/config.js b/docs/.vuepress/config.js index b23375e1a..679ac299a 100644 --- a/docs/.vuepress/config.js +++ b/docs/.vuepress/config.js @@ -165,6 +165,7 @@ module.exports = { 'legend/position', 'legend/title', 'legend/point-style', + 'legend/events', ] }, { diff --git a/docs/samples/legend/events.md b/docs/samples/legend/events.md new file mode 100644 index 000000000..01a3bd0e9 --- /dev/null +++ b/docs/samples/legend/events.md @@ -0,0 +1,57 @@ +# Events + +This sample demonstrates how to use the event hooks to highlight chart elements. + +```js chart-editor + +// +const data = { + labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'], + datasets: [{ + label: '# of Votes', + data: [12, 19, 3, 5, 2, 3], + borderWidth: 1, + backgroundColor: ['#CB4335', '#1F618D', '#F1C40F', '#27AE60', '#884EA0', '#D35400'], + }] +}; +// + +// +// Append '4d' to the colors (alpha channel), except for the hovered index +function handleHover(evt, item, legend) { + legend.chart.data.datasets[0].backgroundColor.forEach((color, index, colors) => { + colors[index] = index === item.index || color.length === 9 ? color : color + '4D'; + }); + legend.chart.update(); +} +// + +// +// Removes the alpha channel from background colors +function handleLeave(evt, item, legend) { + legend.chart.data.datasets[0].backgroundColor.forEach((color, index, colors) => { + colors[index] = color.length === 9 ? color.slice(0, -2) : color; + }); + legend.chart.update(); +} +// + +// +const config = { + type: 'pie', + data: data, + options: { + plugins: { + legend: { + onHover: handleHover, + onLeave: handleLeave + } + } + } +}; +// + +module.exports = { + config +}; +```