2016-10-27 11 views
0

json {month、consum}からaとbの値を割り当てる方法は?PlotlyとAngularJSを使用してREST API応答をプロットする

REST APIレスポンス:

data= { 
    "_id": { 
    "$oid": "580b8f80c2ef1645d285c884" 
    }, 
    "year": "2012", 
    "state": "Indiana", 
    "consumption": 3275, 
    "month": "january" 
    }, 
    { 
    "_id": { 
    "$oid": "580b8f81bd966f4110af0111" 
    }, 
    "year": "2012", 
    "state": "Indiana", 
    "consumption": 6348, 
    "month": "february" 
    } 

app.js:

var graphPlotterDemoApp = angular.module('graphPlotterDemoApp', ['graphPlotter']); 

graphPlotterDemoApp.controller('graphPlotterDemoCtrl', function($scope, $http) { 
    var a = ["jan", "feb", "mar", "apr"]; 
    var b = [10, 15, 13, 17]; 


    $scope.data={}; 
    $scope.refresh = function(){ 
     $http.get("https://api.mlab.com/api/1/databases/energyx/collections/energydata_2012?&apiKey=6JzRAzvdeqyLKAfQAqMOsLYLvvIdJG6A") 
     .success(function(response) { 
      $scope.data = response.data; 
     }); 

    }; 


    var trace1 = { 
     x: a, 
     y: b, 
     type: 'scatter' 
    }; 


    $scope.refresh(); 

    $scope.graphPlots = [trace1]; 
}); 
+0

$ scope.data._id [0] .consumptionからデータを取得したいですあなたはあなたが全体のデータを使いたいと思うように、0の位を得るforeach get all – Pravin

答えて

0

使用は、JSON

var a=$scope.data.map(function(a) {return a.month;}) 
var b=$scope.data.map(function(a) {return a.consumption;}) 
関連する問題