[英]Chart.js update in angular
我尝试按角度动态更新Chart.js图表,但更新方法未显示效果。 这是我的代码:
//初始化图表
$scope.init = function () {
var req = {
method: 'POST',
url: '...'
};
$http(req).then(function(res) {
$scope.state.chart = new Chart(document.getElementById("mychart").getContext('2d'), {
type: 'bar',
data: {
labels: res.data.months,
datasets: [{
label: 'All',
data: res.data.sums
}]
}
});
});
};
//更新图表
$scope.categoryChange = function () {
var req = {
method: 'POST',
url: '...'
};
$http(req).then(function(res) {
$scope.state.chart.data.labels = res.data.months;
$scope.state.chart.data.datasets.label = $scope.state.category;
$scope.state.chart.data.datasets.data = res.data.sums;
$scope.state.chart.update();
});
};
有一个主意的人为什么它不起作用?
我正在使用Ionic,但是您应该尝试执行以下操作:
// Only Change 3 values
let data = [
Math.round(Math.random() * 100),
59,
80,
Math.random() * 100,
56,
Math.random() * 100,
40
];
let clone = JSON.parse(JSON.stringify(this.barChartData));
clone[0].data = data;
this.barChartData = clone;
/**
* (My guess), for Angular to recognize the change in the dataset
* it has to change the dataset variable directly,
* so one way around it, is to clone the data, change it and then
* assign it;
*/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.