[Chartjs]-Can't make chart js responsive

18👍

The answer is :

1. add a div around the canvas

for example :

<div class="wrapper">
    <canvas width="600" height="250"></canvas>
</div>

2. add height to the div class in the css

.wrapper {
height: 500px !important;
}

(adjust height as you wish the responsive rendering)

16👍

maintainAspectRatio: false sorted me out on a similar problem. The default value is true.

11👍

Bootstrap has an example page, which has a chart (using chartjs) that is responsive. So, I would point to that URL, which can be downloaded and we have a responsive chart.

Dasbhoard Example showing Reponsive Chart

11👍

I know this is an old post, but recently came across it.

Please check
https://www.chartjs.org/docs/latest/general/responsive.html#important-note

It basically says that,

  1. Detecting when the canvas size changes can not be done directly from the canvas element. Chart.js uses its parent container to update the canvas render and display sizes. However, this method requires the container to be relatively positioned and dedicated to the chart canvas only. Responsiveness can then be achieved by setting relative values for the container size”.

  2. Note that in order for the above code to correctly resize the chart height, the maintainAspectRatio option must also be set to false.”

For Example,

<div class="chart-container" style="position: relative; height:40vh; width:80vw">
    <canvas id="chart"></canvas>
</div>

6👍

You can make the container divs responsive using CSS and make svg width: 100% and height:100%

Here is updated fiddle fiddle.net

6👍

I had a similar situation and based on chartjs documentation I had to add responsive: true in my options section, but it didn’t solve my problem! Finally I noticed that parent element of canvas is flexbox! Removing that solved my problem!

P.S Official documentation says that adding some style or width/height attributes to canvas tag is invalid! Below is snippet from docs

The following examples do not work:

<canvas height="40vh" width="80vw">: invalid values, the canvas
doesn’t resize (example) <canvas style="height:40vh; width:80vw">:
invalid behavior, the canvas is resized but becomes blurry

Hope my answer can help someone!

5👍

As I saw in fiddle, you have given fixed height and width to canvas tag.

Try to give both height and width 70% or 80% for canvas tag. This may solve your problem.

100% may be give full large charts bt try to limit them to 70 to 80.

2👍

Instead of giving height and width attribute, set them through style:

<canvas id="myChart" style="height: 20rem"></canvas>

1👍

I’ve found that with Chart.js, you need a containing div where you manipulate the css(height/width), which will help the responsiveness – but not fully. There are some constraints especially with the width, as the containing div might leave a lot of whitespace on the bottom/sides, since the canvas won’t fully adjust to the containing width as a percentage. It isn’t perfect, but it works.

So, as a solution: Make a containing div for each chart, and place it using CSS styles for this div 🙂

1👍

i tried all what the documents said but no chance,so i managed by my self:

<section style="padding: 0px">    
 <div class="chart-container" style="position: relative; margin: auto; height:167px; width: 1492px;">
         <canvas id="Graph"></canvas>
     </div>
 </section>
    <script>
    $(window).load(function() {
      $( window ).resize(function() {
         $('#Graph').closest('.chart-container').css('width',$('#Graph').closest('section').width());
      });
    });
    </script>

the idea here to change the width or the height or both when the window resized.

0👍

Because of can’t use media queries to set width and height, I set width and height attribute by detect mobile or desktop, like below

HTML

<canvas id="chart" width="{{width}}" height="{{height}}"></canvas>

javascript – angularjs

$scope.width = '';
$scope.height = '120';
if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
   $scope.width = '100';
   $scope.height = '100';
}

0👍

from chart.js

Responsiveness can then be achieved by setting relative values for the
container size:

<div class="chart-container" style="position: relative; height:40vh; width:80vw">
    <canvas id="chart"></canvas>
</div>

The chart can also be programmatically resized by modifying the
container size: chart.canvas.parentNode.style.height = '128px';

Leave a comment