score:52

Accepted answer

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.

score:-1

"if for example, you wanted all charts created to be responsive, and resize when the browser window does, the following setting can be changed:"

chart.defaults.global.responsive = true;

"now, every time we create a chart, options.responsive will be true."

source: http://www.chartjs.org/docs/

score:0

i wrapped the in and then used this css.

.chart-container {
  box-sizing:border-box;
  margin:2%;
  width:96%;
}

this keeps it padded from the edges and responsive. i used 2% but you can use whatever percentages suit your layout.

score:3

in your jsfiddle example change the responsive attribute to false:

var options = {
    maintainaspectratio: true,
    responsive: false
};

the previous setting added a additional 30px to both the height and width when you inspect the element using chrome dev tools. because of the size of the canvas it should not be problematic when it comes to resizing of the canvas.

jsfiddle example

score:3

a better solution is to use .container {box-sizing: border-box;}

score:4

i too googled for this problem and ended up with a simple solution.

i added height so that it automatically adjust the width accordingly and show it in a nicer way. if u really want you could add width too. the canvas element is part of html5.

it does not allow you to input values with px. so ignore that and just type tha numerical value u need.

 <canvas id="mychart" height="80"></canvas>

Related Query

More Query from same tag