如何使用_.map在图表中显示数据

时间:2016-07-05 15:47:18

标签: javascript angularjs graph lodash

问题: 响应数据返回一个包含数组内对象的数组。我正在尝试创建一个条形图,显示为每个日期创建的问题数量。我已经尝试使用lodash来映射数据以替换当前数据但继续收到错误。

data: _.map(response.data, index) 
  [
    [response.data[index].createdAt, response.data[index].datecnt]
  ]

控制器:

angular.module('myApp').controller('DailyQuestionsController', function($scope, $log, $rootScope, $http, $state, $interval, $stateParams, NgTableParams, $filter) {
  return $http.get('/api/v1/questions/questionChart').then(function(response) {
    console.log(response.data);
    console.log(response.data[0].createdAt);
    console.log(response.data[0].datecnt);
    return $scope.barGraph = {
      options: {
        chart: {
          type: 'column'
        }
      },
      title: {
        text: 'Questions Created Per Day'
      },
      xAxis: {
        type: 'category'
      },
      yAxis: {
        min: 0,
        title: {
          text: 'Number of Questions'
        }
      },
      series: [
        {
          data: [['first date', 587], ['second date', 60], ['third date', 12], ['fourth date', 8], ['fifth date', 104]]
        }
      ]
    };
  });
});

服务器控制器:

getQuestionChart: function(req, res) {
    return db.sequelize.query('SELECT COUNT(CAST("createdAt" AS DATE)) AS dateCnt, CAST("createdAt"                         AS DATE) FROM "Questions" q GROUP BY CAST("createdAt" AS DATE)                      ORDER BY "createdAt" ASC')
   .spread(function(response) {
        return res.json(response.data);
    });
  }
});

1 个答案:

答案 0 :(得分:1)

试试这个:

data: _.map(response.data, function(d) {
    return [d.createdAt, d.datecnt];
})