[Chartjs]-Chart.js :set yAxis point to 0 when there is gap between two dates

1👍

I encountered the same problem not long ago and “fixed” it by writing a simple javascript hack.
1. Create new array of dates;
2. Compare it to your current array of dates;
3. Fill the corresponding gaps in your values array with zeroes;

It probably can be done simplier and prettier but here’s my code:

var minDate = new Date(date[0]).getTime(),
maxDate = new Date(date[date.length - 1]).getTime();

var newDates = [],
currentDate = minDate,
d;

while (currentDate <= maxDate) {
    d = new Date(currentDate);
    newDates.push(d.getFullYear() + '-' + ("0" + (d.getMonth() + 1)).slice(-2) + '-' + ("0" + d.getDate()).slice(-2));
    currentDate += (24 * 60 * 60 * 1000); // add one day
}

for (var i = 0; i < newDates.length; i++) {
    if (newDates[i] == dates[i]) {
        newCount.push(count[n]);
        n++;
    } else {
        newCount.push("0");
        dates.splice(i, 0, newDates[i]);            
    }
}

Leave a comment