<script>
const ctx = document.getElementById('myChart').getContext('2d');
const data = {
labels: Array.from({length: 40}, (_, i) => i.toString()),
datasets: [{
label: '动态数据',
borderColor: 'rgba(75, 192, 192, 1)',
backgroundColor: 'rgba(75, 192, 192, 0.2)',
data: Array.from({length: 40}, () => Math.random() * 100)
}]
};
const config = {
type: 'line',
data: data,
options: {
animation: {
duration: 0
}
}
};
const myChart = new Chart(ctx, config);
function updateData() {
data.datasets[0].data.shift();
data.datasets[0].data.push(Math.random() * 100);
myChart.update();
}
setInterval(updateData, 1000);
</script>