【发布时间】:2018-04-27 12:16:48
【问题描述】:
我正在使用 chart.cs 来尝试图表。到目前为止它工作正常。现在我想动态更改数据,但 update() 函数不起作用。我哪里错了?
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.bundle.min.js"></script>
</head>
<body>
<button onclick="changedata()">changedata</button>
<canvas id="testChart"></canvas>
<script>
var testChart;
var test_labels = ['06:00', '06:10', '06:20', '06:30', '06:40', '06:50', '07:00'];
var test_data = ['1', '2', '3', '4', '3', '2', '1'];
var ctx = document.getElementById("testChart").getContext('2d');
testChart = new Chart(ctx, {
type: 'line',
data: {
labels: test_labels,
datasets: [{
data: test_data,
borderColor: "#ff0000",
borderWidth: 1,
}]
}
});
function changedata(){
test_data = ['4', '3', '2', '1', '2', '3', '4'];
testChart.update();
}
</script>
</body>
</html>
【问题讨论】:
标签: chart.js