Bar widths rendering too wide (in indeterministic way)
Unfortunately, this issue is not reproducible reliably and it's only my best guess that it's an issue with the React rendering lifecycle and not chart.js itself. I'm hoping someone has some ideas on how to make the chart behave more deterministically or debugging tips.
I have a bar chart with time for the x-axis. It's a responsive chart and the bug happens sporadically when rendered on a mobile browser (small screen). The bug is with the bar widths: sometimes they are rendered too wide and overlapping. It happens sporadically and in some cases the widths even vary across bars. Below are 3 examples (1 good, 2 bad), all with the same exact dataset. They just happen to look differently each time the page loads.
My best guess is that somehow the library code for calculating the width of the bars is running before the dom has stabilized? So it thinks it is wider than it is, thus resulting in in bars that are too wide. Does anyone know what the problem might be or if there is a technique to make the chart render more reliably?
I'm using a responsive UI library (material-UI) with a grid system based on flexbox.
Code for the Bar chart:
<CardContent>
{!isEmpty(data) &&
<div className={classes.chartContainer}>
<Bar
data={chartData}
options={options}
/>
</div>
}
</CardContent>
The style on that wrapping <div> is:
chartContainer: {
height: 400,
position: 'relative'
},
The chart config is:
export const options = {
responsive: true,
maintainAspectRatio: false,
legend: { display: false },
cornerRadius: 20,
tooltips: {
enabled: true,
mode: 'index',
intersect: false,
},
layout: { padding: 0 },
scales: {
xAxes: [
{
type: 'time',
time: {
tooltipFormat: 'MMM YYYY',
minUnit: 'month',
},
offset: true,
ticks: {
fontColor: palette.text.secondary
},
gridLines: {
display: false,
drawBorder: false
}
}
],
yAxes: [
{
ticks: {
fontColor: palette.text.secondary,
beginAtZero: true,
min: 0,
precision: 0, // Prevent decimal ticks
},
gridLines: {
borderDash: [2],
borderDashOffset: [2],
color: palette.divider,
drawBorder: false,
zeroLineBorderDash: [2],
zeroLineBorderDashOffset: [2],
zeroLineColor: palette.divider
}
}
]
}
};
Good
This is how I expect it to look. I do see it render like most of the time.

Bad 1
Bars too wide, and overlapping. Happens seemingly randomly.

Bad 2
Bars even wider. Happens seemingly randomly.

I updated to the latest version of this library along with chartjs and the problem seems to have gone away from my initial testing.