![](/img/trans.png)
[英]When updating Chart.js chart the data is updated but canvas is emptied
[英]colors are not updated when updating chart
由於版本 2.9.0+ colors 在更新圖表時沒有更新。 直到 2.8.0 版本才有效。 如何處理 2.9.0+ 版本的更新?
這就是我嘗試更新圖表的方式。 chart = 圖表 d = 我的 get 請求中的數據
function addBarData(chart, d) {
var data = [];
data.backgroundColor = [
color(window.chartColors.red).alpha(0.5).rgbString(),
color(window.chartColors.blue).alpha(0.5).rgbString(),
color(window.chartColors.green).alpha(0.5).rgbString()
];
data.borderColor = [
window.chartColors.red,
window.chartColors.blue,
window.chartColors.green
];
data.borderWidth = 1;
data.data = d.values;
data.label = d.label;
chart.data.datasets.push(data);
chart.update();
}
我的完整代碼: https://jsfiddle.net/wge1bj80/
在您的代碼中, data
實際上是一個dataset
,必須定義為 object 而不是數組。 這個問題可以通過改變var data = [];
到var data = {};
.
function addBarData(chart, d) {
var data = {};
...
}
function addPieData(chart, d) {
var data = {};
...
}
請查看您修改后的JSFiddle 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.