【问题标题】:Chart.js: chart does not updateChart.js:图表不更新
【发布时间】: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


    【解决方案1】:

    我认为您在 changedata 函数中遗漏了一些内容,例如

    function changedata(){
        testChart.data.datasets[0].data = ['4', '3', '2', '1', '2', '3', '4'];
        testChart.update();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-30
      • 1970-01-01
      • 2020-08-04
      • 1970-01-01
      • 2017-03-16
      • 1970-01-01
      相关资源
      最近更新 更多