Jason Jason - 1 year ago 266
AngularJS Question

how to use _.map to show data in graph

response data returns an array with objects inside the array. I’m trying to create a bar graph that will show the number of questions created for each date. I’ve tried using lodash to map data to replace the current data but keep on getting error.

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) {
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]]

Server Controller:

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);

Answer Source

Try this:

data: _.map(response.data, function(d) {
    return [d.createdAt, d.datecnt];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download