如何使用json数据输出实现角度js图

时间:2015-10-28 04:56:30

标签: json angularjs

.controller('HomeCtrl', function ($scope, $http, $stateParams, $state, FirstHiveService) {

        var samplelineJsonData = [  
                            {
                                "month": "Jan",
                                "noOfUsers": "65"
                            },
                            {
                                "month": "Feb",
                                "noOfUsers": "59"
                            },
                            {
                                "month": "Mar",
                                "noOfUsers": "80"
                            },
                            {
                                "month": "Apr",
                                "noOfUsers": "81"
                            },
                            {
                                "month": "May",
                                "noOfUsers": "56"
                            },
                            {
                                "month": "Jun",
                                "noOfUsers": "55"
                            },
                            {
                                "month": "Jul",
                                "noOfUsers": "40"
                            }
                          ];                          

    var lineLabelsData = []; //, percentage2 = [], output = [percentage1, percentage2];
    for (var i = 0; i < samplelineJsonData.length; i++) {
        lineLabelsData.push(samplelineJsonData[i].month);
        //percentage2.push(samplelineJsonData[i].percentage2);
    }

    lineLabelsData = JSON.stringify(lineLabelsData);    
    $scope.labels1 =  lineLabelsData;
    $scope.series1 = ['Users'];
    $scope.data1 =  [[65, 59, 80, 81, 56, 55, 40]]; //lineData; //[[65, 59, 80, 81, 56, 55, 40] ];      

    //Iterating thru the json output Start    

    //Iterating thru the json output End
})

上面的代码不起作用。请告诉我上面的错误...我无法将$ scope.labels1值设置为JSON.stringify(lineLabelsData);

1 个答案:

答案 0 :(得分:0)

您尝试将model用作array。您应该在init调用中首先将模型声明为数组。它应该是

$scope.labels1 = [], $scope.data1 = []; 

确保在您的控制器初始化调用中声明了此变量。之后,您已经迭代了值并将结果推送到数据中,那么您无需再次迭代。请尝试以下代码段

for (var i = 0; i < samplelineJsonData.length; i++) {
      $scope.labels1.push(JSON.stringify(samplelineJsonData[i].month));
      $scope.data1.i = parseInt(samplelineJsonData[i].noOfUsers); //Because this is an object
}