2016-10-27 4 views
0

Wie a und b Werte aus dem Json {Monat, Verbrauch} zuweisen?Plot REST API-Antwort mit Plotly und AngularJS

REST API-Antwort:

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 erhalten Sie bekommen '0' th Position wie Sie wollen ganze Daten verwenden foreach bekommen alle – Pravin

Antwort

0

verwenden wie Daten von json

var a=$scope.data.map(function(a) {return a.month;}) 
var b=$scope.data.map(function(a) {return a.consumption;})