使用chart js并使其执行我需要的操作,即从Web服务中提取数据并使用$interval
对其进行更新。当我使用内置的更新功能更新图表时,它会再次按预期显示,但其中也会包含旧数据。我以为我要从数组中清除数据,我是角度较新的人,所以也许我忽略了一些东西。
这是发生了什么事的js。
angular.module('myapp', [])
.controller('MainCtrl', function($scope, $http, $interval) {
$scope.doughnutData = [];
var myDoughnut = '';
onload = function() {
$http.get('https://api.myjson.com/bins/59zvx').success(function(returnedData) {
$scope.username = returnedData.name;
$scope.totalscore = returnedData.totalScore.toFixed(3);
returnedData.models.forEach(function(x) {
var score = x.score.toFixed(3);
console.debug(score);
if (score >= 75) {
$scope.doughnutData.push({
'value': x.score,
'color': '#F7464A',
'highlight': '#FF5A5E',
'label': x.name
});
} else if (score >= 50) {
$scope.doughnutData.push({
'value': x.score,
'color': '#FDB45C',
'highlight': '#FFC870',
'label': x.name
});
} else {
$scope.doughnutData.push({
'value': x.score,
'color': '#424242',
'highlight': '#686868',
'label': x.name
});
}
});
$scope.doughnutData = sortByKey($scope.doughnutData, 'value');
var ctx = document.getElementById("chart-area").getContext("2d");
myDoughnut = new Chart(ctx).Doughnut($scope.doughnutData, {
responsive: true
});
});
mainInterval = $interval(function() {
doughnutData = '';
$http.get('https://api.myjson.com/bins/59zvx').success(function(returnedData) {
$scope.username = returnedData.name;
$scope.totalscore = returnedData.totalScore.toFixed(3);
returnedData.models.forEach(function(x) {
var score = x.score.toFixed(3);
console.debug(score);
if (score >= 75) {
$scope.doughnutData.push({
'value': x.score,
'color': '#F7464A',
'highlight': '#FF5A5E',
'label': x.name
});
} else if (score >= 50) {
$scope.doughnutData.push({
'value': x.score,
'color': '#FDB45C',
'highlight': '#FFC870',
'label': x.name
});
} else {
$scope.doughnutData.push({
'value': x.score,
'color': '#424242',
'highlight': '#686868',
'label': x.name
});
}
});
$scope.doughnutData = sortByKey($scope.doughnutData, 'value');
myDoughnut.segments = doughnutData;
myDoughnut.update();
});
}, 5000);
$scope.$apply();
};
function sortByKey(array, key) {
return array.sort(function(a, b) {
var x = a[key];
var y = b[key];
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
});
}
});
因此,它以9个值开始,更新时有18个值,等等。当前没有要更新的内容(当我将其移至生产Web服务时,我现在就在那里进行更新)。我以为
doughnutData = '';
部分中的$interval
可以清除它?想要包含html以便更好地了解发生了什么:
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="../Chart.js"></script>
<script src="../src/Chart.Doughnut.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="../script.js"></script>
</head>
<body ng-app="myapp" ng-controller="MainCtrl">
<div id="canvas-holder">
<canvas id="chart-area" width="50" height="50"></canvas>
</div>
</body>
</html>
最佳答案
尝试更改mainInterval函数
doughnutData = '';
与
$scope.doughnutData = [];
在angular中,您可以通过$ scope对象访问字段
关于javascript - 甜甜圈图更新了两倍的数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30080258/