Why is chart.js canvas not respecting the padding of the container element?

Hawk picture Hawk · Jan 27, 2015 · Viewed 50.2k times · Source

I am using Chart.js with a simple line chart but the width and height properties calculated by Chart.js seem to be based on the total width and height of the parent element ignoring padding.

JSFiddle

The second container and child shows the behaviour I am expecting. Is this a bug with how Chart.js calculates the width and height of the canvas or am I making a styling mistake?

Answer

gwg picture gwg · Aug 19, 2015

I also needed a responsive canvas and had this issue. This was my fix:

<div>
    <canvas id="chart"></canvas>
</div>

Since Chart.js scales the canvas to the width of the container, ignoring the padding, I just wrapped the canvas in a div. The div scales to the container with padding, respecting the padding, and then the responsive Chart.js canvas scales to the div.