console error when using getElementsAtEventForMode

Viewed 52

I'm trying to call the function getElementsAtEventForMode() in my chart.js doughnut chart, but i keep getting this error message in console: chart.getElementsAtEventsForMode is not a function.

       // Pass the canvas ID
        let myChart = Chart.getChart('myChart');

        //updating chart with new values each time a value is selected
        if (myChart) {
            myChart.data.labels = legend;
            myChart.data.datasets[0].data = stats;
            myChart.config.options.plugins.title.text = "Top 5 causes of death in " + country;
            myChart.update();
        } else {
            new Chart('myChart', config);
        }

        //detecting click on doughnut sections for modal
        this.clickHandler = function (onclick) {
            const points = myChart.getElementsAtEventForMode(onclick, 'nearest', { intersect: true }, true);

            if (points[0]) {
                modal.classList.toggle('hide');
            }
        }

        addEventListener("click", this.clickHandler);
2 Answers

You can't use the global chart instance, you need to use your chart instance to call the function like so:

const chartInstance = Chart.getChart(canvasId);

const points = chartInstance.getElementsAtEventForMode("click", 'nearest', { intersect: true }, true);

In Chartjs, it has getElementsAtEventForMode not eventsForMode.. use

Chart.getElementsAtEventForMode

instead of

Chart.getElementsAtEventsForMode
Related