Chart.js更新多个图表

wnavrhmk  于 2023-05-17  发布在  Chart.js
关注(0)|答案(1)|浏览(184)

我正在使用此函数更新图表的某个数据集,但它似乎不起作用,我不明白为什么函数:

`function addData(chart, label, data, i) {
    chart.data.labels.push(label);
    chart.data.datasets[i].data.push(data);
    chart.update();
   }`

当我运行下面的代码时,每个图表都应该单独更新,但它们都是相同的

`birdCount = document.getElementsByClassName("bird").length
        addData(chart, [Generation], [birdCount], 0)
        var birdCountST = document.getElementsByClassName("bird11").length + document.getElementsByClassName("bird12").length
        console.log(birdCountST,'shortthickguys')
        addData(chart, [Generation], [birdCountST], 1)
        var birdCountST = document.getElementsByClassName("bird11").length + document.getElementsByClassName("bird12").length
        addData(chart, [Generation], [birdCountST], 2)`
kh212irz

kh212irz1#

我很愚蠢,没有注意到,但我没有改变变量时,我复制它应该是这样的::facepalm:

`birdCount = document.getElementsByClassName("bird").length
    addData(chart, [Generation], [birdCount], 0)
    var birdCountST = document.getElementsByClassName("bird11").length + document.getElementsByClassName("bird12").length
    console.log(birdCountST,'shortthickguys')
    addData(chart, [Generation], [birdCountST], 1)
    var birdCountLT = document.getElementsByClassName("bird21").length + document.getElementsByClassName("bird22").length
    addData(chart, [Generation], [birdCountLT], 2)`

相关问题