题:
响应数据返回一个数组,该数组中包含对象。我正在尝试创建一个条形图,以显示每个日期创建的问题数。我曾尝试使用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);
    });
  }
});

最佳答案

尝试这个:

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

09-11 17:50
查看更多