最近我根据Chart.js从v1移动了一个旧项目到v2。
在v1中,默认启用此功能。 有谁知道我需要更改哪个选项才能存档。
到目前为止我的代码。
new Chart(document.getElementById('mainChart'), {
type: 'line',
data: {
labels: labels,
datasets: [
{ data: data, label: "Expenses", fill: false
]
},
options: {
animation: { duration: 0 },
hover: { animationDuration: 0 },
responsiveAnimationDuration: 0
}
});
答案 0 :(得分:1)
可以使用工具提示部分中的mode
选项进行设置。
将模式设置为index
可能是您要查找的模式。
new Chart(document.getElementById('mainChart'), {
type: 'line',
data: {
labels: labels,
datasets: [
{ data: data, label: "Expenses", fill: false }
]
},
options: {
animation: { duration: 0 },
hover: { animationDuration: 0 },
responsiveAnimationDuration: 0,
tooltips: { mode: 'index' }
}
});
以下mode: 'index'
的示例:
new Chart(document.getElementById('chartJSContainer'), {
type: 'line',
data: {
labels: ["1", "2", "3", "4", "5", "6"],
datasets: [{
data: [7, 11, 5, 8, 3, 7],
label: "Income",
fill: false,
backgroundColor: 'rgb(54, 162, 235)',
borderColor: 'rgb(54, 162, 235)',
}, {
data: [12, 19, 3, 5, 2, 3],
label: "Expenses",
fill: false,
backgroundColor: 'rgb(255, 99, 132)',
borderColor: 'rgb(255, 99, 132)',
}
]
},
options: {
animation: {
duration: 0
},
hover: {
animationDuration: 0
},
responsiveAnimationDuration: 0,
tooltips: {
mode: 'index'
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.bundle.min.js"></script>
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
</body>