I have this stacked bar chart created using the <Bar />
component from react-chartjs-2 charting library.
I used React to do this. But answers considering the equivalent in vanilla JS would also be very helpful!
Now what I want is, when I hover over the orange section of the 1st Jun stack, then the orange section in all the stacks should be highlighted. Either the opacity of all sections except orange decreases, or the orange sections slightly enlarge. If these are not possible, atleast a border should be drawn around the orange sections.
I've searched through the chartjs documentation and searched through chartjs plugins in npm registry. But I found nothing related to this.
What should be done in order to achieve this?
With reference to @LeeLenalee's solution, I've come up with the answer for the React implementation
options: {
onHover: (e, activeEls) => {
if (activeEls.length === 0) {
this.chart.chartInstance.config.data.datasets.forEach((dataset) => {
dataset.backgroundColor =
dataset.backgroundColor.length === 9
? dataset.backgroundColor.slice(0, -2)
: dataset.backgroundColor;
});
this.chart.chartInstance.update();
return;
}
const hoveredEl = this.chart.chartInstance.getDatasetAtEvent(e)[0];
this.chart.chartInstance.config.data.datasets.forEach((dataset, i) => {
const [boldColor, lightColor] =
dataset.backgroundColor.length === 9
? [dataset.backgroundColor.slice(0, -2), dataset.backgroundColor]
: [dataset.backgroundColor, dataset.backgroundColor + "4D"];
dataset.backgroundColor =
hoveredEl._datasetIndex === i ? boldColor : lightColor;
});
this.chart.chartInstance.update();
}
}
where this.chart
is the ref to my <Bar />
component.
Notice that I have
const [boldColor, lightColor] =
dataset.backgroundColor.length === 9
? [dataset.backgroundColor.slice(0, -2), dataset.backgroundColor]
: [dataset.backgroundColor, dataset.backgroundColor + "4D"];
dataset.backgroundColor =
hoveredEl._datasetIndex === i ? boldColor : lightColor;
from line-17 to line-20. This helps to avoid the unnecessary glitch that I saw in @LeeLenalee's code snippet when the bar's opacity was changing.