Skip to content
Advertisement

HorizontalBar chart is displayed below canvas when I set height with style attribute for chart div (Chart.js 2.9.4)

If I set height with style attribute for chart div

<div id="chartDiv" style="height: 100px; border: 1px solid red;"><canvas id="chart"></canvas></div>

chart is looking like that (chart is displayed below canvas):

JavaScript
JavaScript

How to fix that?

Advertisement

Answer

It’s showing like this because canvas’s aspect ratio is changing automatically with window size but you’ve set your div to a specific height of 100px. That’s why canvas has placed outside of the div while window size is extending. Just to get rid of that problem, set maintainAspectRatio: false in your option in this way:

JavaScript
JavaScript
Advertisement