Event on Chart.JS's Bar Chart's mousein and mouseout?

两盒软妹~` 提交于 2021-02-08 07:47:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!