[Chartjs]-Chart.js – consolidating days to month totals along the x (time) axis?

2πŸ‘

βœ…

I solved this by doing the rollup myself during the assembly of the underlying dataset which is then supplied to the chart.

var dayDate = new Date($scope.insights.locationMetrics[lm].metricValues[metric].dimensionalValues[dim].timeDimension.timeRange.startTime);
var monthDate = dayDate.getFullYear() + "-" + (dayDate.getMonth() + 1);

var hitCount = {
    y: $scope.safeNumber($scope.insights.locationMetrics[lm].metricValues[metric].dimensionalValues[dim].value),
    x: monthDate
}
var alreadyRecorded = hits[labelIdx].findIndex(obj => obj.x == hitCount.x)

if (alreadyRecorded > -1) {
    hits[labelIdx][alreadyRecorded].y += Number(hitCount.y);
}
else {
    hits[labelIdx].push(hitCount);
}
  1. Extract the date from the underlying data source
  2. Extract yyyy-mm from the date
  3. Create the hitCount object
  4. Check if the hitCount object is already in the array
  5. If the object is already in the array then increment the hitCount (y) within the array.
  6. Otherwise, push the object into the array.

Leave a comment