I am trying to render bar chart with time series data but it is not rendering properly, I am getting spaces in between stack bars, I have commented out the sample data which I am receiving from web server via AJAX call.
Here is my code:
let config = {
type: 'bar',
labels: res['labels'],
data: {
datasets: [{
label: "Success",
data: _.map(res['success'], (obj) => { return { x: new Date(obj.x), y: obj.y}; }),
// data: [{
// x: new Date('2017-03-01'),
// y: 1
// }, {
// x: new Date('2017-03-02'),
// y: 2
// }, {
// x: new Date('2017-03-03'),
// y: 3
// }, {
// x: new Date('2017-03-04'),
// y: 4
// }],
backgroundColor: "green"
}, {
label: "Failure",
data: _.map(res['failed'], (obj) => { return { x: new Date(obj.x), y: obj.y}; }),
// data: [{
// x: new Date('2017-03-01'),
// y: 1
// }, {
// x: new Date('2017-03-02'),
// y: 2
// }, {
// x: new Date('2017-03-03'),
// y: 3
// }, {
// x: new Date('2017-03-04'),
// y: 4
// }],
backgroundColor: "red"
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
legend: {
display: false,
},
tooltips: {
callbacks: {
title: function(tooltipItem, data) {
const [month, year] = tooltipItem[0].label.split(',', 2);
return `${month}, ${year}`;
}
}
},
scales: {
xAxes: [{
type: "time",
stacked: true,
offset: true,
categoryPercentage: 1.0,
barPercentage: 1.0,
barThickness: 20,
maxBarThickness: 25,
minBarLength: 50,
gridLines: {
offsetGridLines: true
},
time: {
unit: 'day',
round: 'day',
displayFormats: {
day: 'MMM D'
}
}
}],
yAxes: [{
stacked: true,
ticks: {
beginAtZero: true
}
}]
}
}
}
let chartEl = $('.bart-activity-graph-canvas');
return new Chart(chartEl, config);
----------------- UPDATE --------------------
Solution: The issue is because I used _.map() function which does not guarantee the position in the array.