简体   繁体   English

使用Plotly和AngularJS绘制REST API响应

[英]Plot REST API response using Plotly and AngularJS

How to assign a and b values from the json {month,consumption}? 如何从json {month,consumption}中分配a和b值?

REST API response: 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: 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];
});

Use like to get data from json 使用喜欢从json获取数据

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM