gpt4 book ai didi

javascript - Chart.js 的缩放功能

转载 作者:行者123 更新时间:2023-12-01 01:31:08 33 4
gpt4 key购买 nike

我有一个图表,我想使用 github 缩放功能来查找 here .

我编写了下面的代码,该代码运行时没有错误,但是当我尝试在鼠标滚轮上放大和缩小时,它不起作用。

需要更改哪些代码才能使用鼠标滚轮放大和缩小?

请参阅下面的代码片段

var ctx = document.getElementById('doughnut-chart').getContext('2d');

new Chart(ctx, {
type: 'doughnut',
data: {
labels: ["Day One", "Day Two", "Day Three", "Day Four", "Day Five"],
datasets: [
{
label: "Agi",
backgroundColor: ["#3e95cd", "#8e5ea2","#3cba9f","#e8c3b9","#c45850", "#6bcd3e"],
data: ["100", "200", "300", "400", "500" ]
}
]
},
options: {
title: {
display: true,
text: "Title"
}
},
pan: {
enabled: true,
mode: 'xy'
},
zoom: {
enabled: true,
mode: 'xy',
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.bundle.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0"></script>
<script src="https://cdn.jsdelivr.net/npm/hammerjs@2.0.8"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-zoom@0.7.3"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.css">
<canvas id="doughnut-chart" width="800" height="450"></canvas>

编辑
正如评论中所建议的,我添加了一个插件类别,代码现在如下所示 - 但缩放功能仍然无法正常工作。

   <script type="text/javascript">
var ctx = document.getElementById('doughnut-chart').getContext('2d');

new Chart(ctx, {
type: 'doughnut',
data: {
labels: ["Day One", "Day Two", "Day Three", "Day Four", "Day Five"],
datasets: [
{
label: "Test",
backgroundColor: ["#3e95cd", "#8e5ea2","#3cba9f","#e8c3b9","#c45850", "#6bcd3e"],
data: ["100", "200", "300", "400", "500" ] ,
}
]
},
options: {
title: {
display: true,
text: "Test"
},
plugins: {
zoom: {
pan: {
enabled: true,
mode: 'x',
speed: 10,
threshold: 10
},
zoom: {
enabled: true,
mode: 'y'
}
}
}
}
});
</script>

最佳答案

请参阅此plugin及其 example .

默认情况下,您可以绘制 View 框或使用鼠标滚轮进行放大或缩小。双击使图表适合 Canvas 。

关于javascript - Chart.js 的缩放功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57343566/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com