要使chart.js条形图可滚动,可以通过以下步骤实现:
<div style="height: 400px; overflow: auto;">
<canvas id="barChart"></canvas>
</div>
var ctx = document.getElementById('barChart').getContext('2d');
var chart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['Label 1', 'Label 2', 'Label 3', 'Label 4', 'Label 5', 'Label 6'],
datasets: [{
label: 'Bar Chart',
data: [10, 20, 30, 40, 50, 60],
backgroundColor: 'rgba(0, 123, 255, 0.5)'
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
y: {
beginAtZero: true
}
}
}
});
var container = document.querySelector('.container');
container.addEventListener('scroll', function() {
chart.options.plugins.zoom.pan.enabled = true;
chart.options.plugins.zoom.pan.rangeMin = { x: null, y: null };
chart.options.plugins.zoom.pan.rangeMax = { x: null, y: null };
chart.update();
});
通过以上步骤,就可以实现chart.js条形图的可滚动效果。当容器元素的内容超出容器高度时,将显示滚动条,并可以通过滚动条来查看完整的条形图内容。
领取专属 10元无门槛券
手把手带您无忧上云