diff options
author | Chaoyi Zha <summermontreal@gmail.com> | 2017-04-16 00:08:36 +0300 |
---|---|---|
committer | Chaoyi Zha <summermontreal@gmail.com> | 2017-04-16 00:08:36 +0300 |
commit | b712395651bbef4276f5a1142d418ca465bf3197 (patch) | |
tree | 063627e542160daa640f7c610b7f54b6f3411e6e | |
parent | f92b2931267697ecde4df658f639832591c7232b (diff) |
Populate empty dayss in range with zeroes before graphing data
-rw-r--r-- | public/js/StatsCtrl.js | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/public/js/StatsCtrl.js b/public/js/StatsCtrl.js index d1dfaf9..98a4e76 100644 --- a/public/js/StatsCtrl.js +++ b/public/js/StatsCtrl.js @@ -11,8 +11,47 @@ polr.controller('StatsCtrl', function($scope, $compile) { $scope.refererData = refererData; $scope.countryData = countryData; + $scope.populateEmptyDayData = function () { + // Populate empty days in $scope.dayData with zeroes + + // Number of days in range + var numDays = moment(datePickerRightBound).diff(moment(datePickerLeftBound), 'days'); + var i = moment(datePickerLeftBound); + + var daysWithData = {}; + + // Generate hash map to keep track of dates with data + _.each($scope.dayData, function (point) { + var dayDate = point.x; + daysWithData[dayDate] = true; + }); + + // Push zeroes for days without data + _.each(_.range(0, numDays), function () { + var formattedDate = i.format('YYYY-MM-DD'); + + if (!(formattedDate in daysWithData)) { + // If day does not have data, fill in with 0 + $scope.dayData.push({ + x: formattedDate, + y: 0 + }) + } + + i.add(1, 'day'); + }); + + // Sort dayData from least to most recent + // to ensure Chart.js displays the data correctly + $scope.dayData = _.sortBy($scope.dayData, ['x']) + } + $scope.initDayChart = function () { var ctx = $("#dayChart"); + + // Populate empty days in dayData + $scope.populateEmptyDayData(); + $scope.dayChart = new Chart(ctx, { type: 'line', data: { |