[Chartjs]-Implementing hover callback in Chart.js

2👍

You should implement the onHover callback inside the hover options so you can specify your options. The event hover is fire for every other event because they have a hover in them, i.e. the event click, has a hover and then a click.

var chart = new Chart(document.getElementById('chart').getContext('2d'), {
                options: {
                    hover: {
                        mode: 'nearest',
                        intersect: false,
                        onHover: function (e, item) {
                            if (item.length) {
                                const data = item[0]._chart.config.data.datasets[0].data[item[0]._index];
                                console.log(item, data);
                            }
                        }
                    }
                }
            });

Leave a comment