Chartjs 销毁方法不影响图表
Chartjs destroy method not affecting the chart
嗨,我的 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);
嗨,我的 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);