简体   繁体   English

Chart.JS 条形图的 mousein 和 mouseout 上的事件?

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

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!).我已经搜索了条形图事件文档(我觉得这有点令人困惑),但我一直无法弄清楚在将鼠标悬停在条形图的条形(不是图例!)上时如何处理 mousein 和 mouseout 事件。 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?这可能吗?

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;
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM