2017-03-28 89 views
0

我有下面的JSON數據,通過它我嘗試使用AngularJS創建折線圖。但是使用地圖功能,我能夠獲得$scope.labels,但無法形成$scope.series$scope.data使用json數據創建圖表

這裏是我的JS代碼:

var source = [ 
    {"DE":2,"NP":20,"BD":28,"TW":1, 
    "end_time":"2016-07-05T07:00:00+0000","date":"01-01-2011"}, 
    {"DE":5,"NP":11,"BD":22,"BE":2,"FJ":2, 
    "end_time":"2016-07-06T07:00:00+0000","date":"01-09-2011"}, 
    {"DE":5,"NP":24,"BD":29, 
    "end_time":"2016-07-07T07:00:00+0000","date":"01-11-2011"} 
] 
var $scope = {}; 
$scope.labels = []; 
$scope.data = []; 
$scope.series = []; 


source.forEach(function(item){ 
    $scope.labels.push(item.end_time) 
}) 

如何來從JSON數據,形成一系列像下面和數據。

$scope.series = ["DE","NP","BD","TW"] 

$scope.data = [] ---> //Stuck here in forming data 

JSfiddle

+0

您應該只查看angular的文檔。有一個名爲$ http的模塊,並且有很多示例如何做你想要的。 以下是$ http的地址doc https://docs.angularjs.org/api/ng/service/$http –

回答

0

如果你改變你遍歷數據的方式,這應該成爲相當微不足道:

angular.forEach(source, function(item){ 
    $scope.labels.push(item.end_time); 
    angular.forEach(item, function(val, key) { 
    if(key === 'end_time') return; 
    $scope.data.push(val); 
    $scope.series.push(key); 
    }); 
}) 

Updated Fiddle

角的forEach將幫助您在對象迭代。

+0

感謝您關注此問題。但我需要$ scope.series作爲二維數組來創建圖表。示例 - $ scope.data = [ [65,59,80,81,56,55,40], [28,48,40,19,86,27,90] ];和$ scope.data有重複的值 – user4324324