我是图表/角色的新手,自从我使用javascript以来已经很久了。我正在尝试使用Angular中的工厂从服务器获取数据并使用它来填充图表(使用Angular Charts,一个围绕Charts.js的包装)。我最初可以用假数据填充它,但是根本改变它会导致图表变成空白。我在这里有某种范围问题吗?这是相关的代码:
.controller("LineCtrl", ["$scope", "HRDataPointFactory", function ($scope, HRDataPointFactory) {
//The following lines cause the chart to populate, but obviously I dont care about this fake data.
//$scope.labels = ["January", "February", "March", "April", "May", "June", "July"];
//$scope.series = ["Series A", "Series B"];
//$scope.data = [
// [65, 59, 80, 81, 56, 55, 40],
// [28, 48, 40, 19, 86, 27, 90]
//];
$scope.onClick = function(points, evt) {
console.log(points, evt);
};
$scope.data = [[]];
$scope.labels = [];
$scope.series = ["Department"];
$scope.getData = function() {
HRDataPointFactory.getData($scope.department, $scope.dt1, $scope.dt2)
.then(function (data) {
var i;
for (i = 0; i < data.data.length; i++) {
$scope.data[0].push(data.data[i].EmployeeCount);
}
for (i = 0; i < data.data.length; i++) {
$scope.labels.push(data.data[i].Date);
}
});
}
/*unrelated stuff here*/
}
])
.factory("HRDataPointFactory", function HRDataPointFactory($http) {
var exports = {};
exports.getData = function(deptKey, start, end) {
var config = {
params: { departmentKey: deptKey, startTime: start, endTime: end }
}
return $http.get("/HR/Data", config)
.then(function(response) {
console.log(response);
return response;
},function(data) {
console.log("There was an error!", data);
return response;
});
};
return exports;
});
和html:
<canvas id="line" class="chart chart-line" chart-data="data"
chart-labels="labels" chart-legend="true" chart-series="series"
chart-click="onClick">
</canvas>
感谢您的帮助!
答案 0 :(得分:2)
我创建了Plunker with a working example。为了简化,我只提取数据(而不是标签等),但这很容易完成。
在控制器中,我们为此分配vm,并在函数getData中引用vm.data。之前你有$ scope,它是你指定为空白数组的函数中不同的$ scope。
.controller('MyCtrl', ['$scope', 'HRDataPointFactory', function($scope, HRDataPointFactory) {
var vm = this;
//The following lines cause the chart to populate, but obviously I dont care about this fake data.
vm.labels = ["January", "February", "March", "April", "May", "June", "July"];
vm.series = ["Series A", "Series B"];
// vm.data = [
// [65, 59, 80, 81, 56, 55, 40],
// [28, 48, 40, 19, 86, 27, 90]
// ];
vm.onClick = function(points, evt) {
console.log(points, evt);
};
vm.data = [
[]
];
vm.getData = function() {
HRDataPointFactory.getData($scope.department, $scope.dt1, $scope.dt2)
.then(function(success) {
console.log(JSON.stringify(success));
vm.data = success.data;
});
}
vm.getData();
/*unrelated stuff here*/
}])
我在控制台中打印收到的数据:
{"data":[[65,59,80,81,56,55,40],[28,48,40,19,86,27,90]],"status":200,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"params":{},"url":"data","headers":{"Accept":"application/json, text/plain, */*"}},"statusText":""}
数据也在plunker上托管,使用它作为格式:
[
[65, 59, 80, 81, 56, 55, 40],
[28, 48, 40, 19, 86, 27, 90]
]
请注意,我还使用&#34;控制器作为&#34; HTML中的语法以遵循最佳做法...请参阅john papa's article on this subject。
<div ng-controller="MyCtrl as vm">
<canvas id="line" class="chart chart-line" chart-data="vm.data"
chart-labels="vm.labels" chart-legend="true" chart-series="vm.series"
chart-click="onClick">
</canvas>
</div>
告诉我们。