Chart.js v4.4.3搭配chartjs-plugin-zoom v2.0.0时平移(Pan)功能失效求助
Chart.js v4.4.3搭配chartjs-plugin-zoom v2.0.0时平移(Pan)功能失效求助
大家好,我目前在使用Chart.js v4.4.3搭配chartjs-plugin-zoom v2.0.0开发图表功能,遇到了一个棘手的问题:滚轮缩放功能正常工作,但平移(Pan)功能完全失效,配置的onPan回调函数也从未被触发,而且浏览器控制台里没有任何报错信息,这让我完全摸不着头绪。
下面是我整理的最小复现代码,你可以直接运行来复现这个问题:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Chart.js Zoom + Pan not working</title> <style> body { font-family: sans-serif; padding: 20px; } canvas { width: 800px; height: 400px; border: 1px solid #ccc; } button { margin: 5px; } </style> <script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.3"></script> <script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-zoom@2.0.0/dist/chartjs-plugin-zoom.min.js"></script> </head> <body> <h2>Zoom ok, Pan not working</h2> <canvas id="myChart"></canvas><br> <button onclick="chart.resetZoom()"> Reset Zoom</button> <script> const ctx = document.getElementById('myChart').getContext('2d'); const chart = new Chart(ctx, { type: 'line', data: { labels: Array.from({ length: 100 }, (_, i) => i), datasets: [{ label: 'Values', data: Array.from({ length: 100 }, () => Math.random() * 100), borderColor: 'blue', pointRadius: 0 }] }, options: { responsive: false, plugins: { zoom: { pan: { enabled: true, mode: 'xy', onPan: () => console.log(' Pan triggered') }, zoom: { wheel: { enabled: true }, pinch: { enabled: true }, mode: 'xy', onZoom: () => console.log(' Zoom triggered') } } } } }); </script> </body> </html>
我已经检查过的点:
- 确认
pan.enabled已经设置为true,mode也配置了xy - 缩放功能的
onZoom回调能正常触发,说明插件本身是正常加载的 - 控制台没有任何错误提示,排除了语法或资源加载问题
有没有朋友遇到过类似的情况,或者能从我的代码里发现哪里配置错了?麻烦帮忙看看,非常感谢!
内容来源于stack exchange
相关产品推荐
相关产品推荐

