嗨,我的 React 应用程序中有此代码:
this.chart = new Chart(node, options);
// adding data to the chart ...
this.chart.destroy();
this.chart = null;
this.chart = new Chart(node, options);
// adding data to the chart ...
第二次添加数据后,第一个数据集仍然出现在图表上。我也尝试删除 Canvas 节点,但得到相同的结果。有人知道为什么会发生这种情况吗?
最佳答案
var ctx = document.getElementById("barChart");
var barChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ["Dog", "Cat", "Pangolin"],
datasets: [{
backgroundColor: '#00ff00',
label: '# of Votes 2016',
data: [12, 19, 3]
}]
}
});
function addData(chart, label, color, data) {
chart.data.datasets=[];
chart.data.datasets.push({
label: label,
backgroundColor: color,
data: data
});
chart.update();
}
//2秒后更改新数据集
setTimeout(function() {
addData(barChart, '# of Votes 2017', '#ff0000', [16, 14, 8]);
}, 2000);
关于javascript - Chartjs destroy 方法不影响图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48223773/