Skip to content

Why doesn't the chart maintain its width/height?

A chart is responsive by default: Chart.js takes its size from the parent element and keeps the aspect ratio, so the width and height props only set the size the canvas starts with.

To give the chart a size of your own, set maintainAspectRatio to false and put the chart in a container of its own, with position: relative and the size you need:

<div style={{ position: 'relative', height: '300px' }}>
<Bar
data={data}
options={{ maintainAspectRatio: false }}
/>
</div>

The chart takes the height of this container, so the container needs a height of its own and must hold nothing but the chart. When the chart shares its parent element with other content, such as a button next to it, the parent grows with the chart, and the chart grows without end.

For a fixed size that does not follow the page, turn responsive off, and the width and height props set the size:

<Bar
data={data}
width={400}
height={200}
options={{ responsive: false }}
/>

See Responsive Charts in the Chart.js docs for details.