Chart.js: How to set a fixed height, the default height is 50% of the width。

Created on 28 Jun 2016  Â·  3Comments  Â·  Source: chartjs/Chart.js

How to set a fixed height, the default height is 50% of the width。
aaa

Most helpful comment

To get a fixed height and variable width…

  • remove any width or height on the canvas element.
  • set the desired css height on parent html container.
  • set chart options to include maintainAspectRatio: false,

Here is a demo:
https://codepen.io/beau/pen/rvJRzb/

All 3 comments

I you want height fixed at 350px, modify the <canvas> that hosts the chart with a height attribute:

<canvas height="350px"></canvas>

Then when you set the chart options, make it non-responsive so it won't auto-resize:

var options = {
    responsive:false
}

@RoxKilly is correct

To get a fixed height and variable width…

  • remove any width or height on the canvas element.
  • set the desired css height on parent html container.
  • set chart options to include maintainAspectRatio: false,

Here is a demo:
https://codepen.io/beau/pen/rvJRzb/

Was this page helpful?
0 / 5 - 0 ratings