我正在使用这个函数来更新我图表的某个数据集,但它似乎不起作用,我不明白为什么 功能:
`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)`
我很笨,没有注意到,但是我复制的时候没有改变变量 它应该是这样的::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)`