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

I use Chart.js, and in my xAxis I have an array of some dates with spaces like [2016: 08: 06,2016: 08: 10] and their corresponding values ​​[20,40]

the problem is that Chart.js displays days between a given date array. I do not want to set my array to [20,0,0,0,40], since I have a space of 3 days. how can i automatically set their matching values ​​in yAxis with 0.

+4
source share
1 answer

"" , javascript-.
 1. .  2. .
 3. ;

, , :

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

Source: https://habr.com/ru/post/1652936/


All Articles