I have written a piece of code that counts the occurrences of each date in an array:
let month = [];
let current;
let count = 0;
chartDates = chartDates.sort()
for (var i = 0; i < chartDates.length; i++) {
month.push(chartDates[i].split('-')[1]);
if (chartDates[i] != current) {
if (count > 0) {
console.log(current + ' times ' + count);
}
current = chartDates[i];
count = 1;
} else {
count++;
}
}
if (count > 0) {
console.log(current + 'times ' + count);
}
This is the output I am getting:
2010-02-08 times 1
2010-02-11 times 1
2010-03-05 times 1
2010-03-08 times 1
2017-09-19 times 3
2017-12-26 times 1
Now, I want to use this data to create a bar chart using chart.js. The "labels" should be the dates and the "data" should represent how many times each date occurs. I tried using year = [];
and then year.push(current);
to avoid repeating equal dates within each loop iteration, but it did not work.
Can anyone help me fix this issue?
Here is my chart's configuration:
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: chartDates,
datasets: [{
label: month,
data: month,
.....