user3711521
user3711521

Reputation: 305

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

I'm using Chart.js and in my xAxis I have an array of some dates with gaps like [2016:08:06,2016:08:10] and their matching values [20,40]

the problem is that Chart.js are displaying days between the given array of dates. I don't want to set my array to [20,0,0,0,40] since I have a gap of 3 days. how can I set autoatically their matching values in the yAxis to 0.

Upvotes: 6

Views: 572

Answers (1)

Maksim.T
Maksim.T

Reputation: 531

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

Upvotes: 1

Related Questions