问题
I've scoured the bar chart and event docs (which I find a bit confusing) and I've been unable to figure out how to act on a mousein and mouseout event when hovering over a Bar chart's bars (not the legend!). Currently the closest I've been able to come is utilizing the callback on the tooltip like so:
options.tooltips = {
backgroundColor: 'rgba(0,0,0,0)',
fontColor: 'rgba(0,0,0,0)',
callbacks: {
label: function (tooltipItem) {
// flipping a bool here
}
}
};
This solution doesn't work well because without knowing when the pointer leaves the bar I don't know when to flip the bool back. Is this possible?
回答1:
Here's a solution to my issue:
options.tooltips = {
// Hide the tooltips
backgroundColor: 'rgba(0,0,0,0)',
displayColors: false,
callbacks: {
labelTextColor: function () {
return 'rgba(0,0,0,0)';
},
labelColor: function () {
return {
borderColor: 'rgba(0, 0, 0, 0)',
backgroundColor: 'rgba(0, 0, 0, 0)'
}
}
},
// Highlight the HTML elements on bar hover
custom: function(tooltipModel) {
if (tooltipModel.body === undefined) {
// flip bool false
return;
}
if (/* ... */) {
// flip bool true
}
return;
}
};
来源:https://stackoverflow.com/questions/48770303/event-on-chart-jss-bar-charts-mousein-and-mouseout