ChartJS 使用getElementsAtEventForMode时出现控制台错误

tktrz96b  于 2022-11-06  发布在  Chart.js
关注(0)|答案(2)|浏览(197)

我尝试在chart.js圆环图中调用函数getElementsAtEventForMode(),但我在控制台中不断收到此错误消息: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);
7ivaypg9

7ivaypg91#

您不能使用全局图表示例,需要使用图表示例来调用函数,如下所示:

const chartInstance = Chart.getChart(canvasId);

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

5t7ly7z52#

在Chartjs中,它具有getElementsAtEventForMode,而不是事件ForMode ..使用

Chart.getElementsAtEventForMode

而不是

Chart.getElementsAtEventsForMode

相关问题