Jason Jason - 5 months ago 107
AngularJS Question

how to use _.map to show data in graph

Question:
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]
]


Controller:

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


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

SAM SAM
Answer

Try this:

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