[Chartjs]-Width and Height in Chart.js

5👍

There are many ways to specify the canvas’ height and width. But what’s the difference between the two methods you tried? Well, the full explanation can be found here
Canvas is stretched when using CSS but normal with "width" / "height" properties

P.S You can make the chart responsive by having responsive: true and putting your canvas inside a div. That way, the canvas will always take its container’s dimensions.

let line_chart = document.getElementById("line-chart");

    new Chart(line_chart, {
    type: 'scatter',
    data: {
      datasets: [{ 
            label: 'Sample Data',
            data: [{ x: 1, y: 1 }, { x: 2, y: 2 }, { x: 3, y: 3 }] }],
      },
    options: {
      responsive:true,
    }
  });
/* If you change the wrapper's height and width, the chart will follow the wrapper's dimensions */
.wrapper{
  height: 200px;
  width: 300px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.bundle.js"></script>

<div class="wrapper">
  <canvas id="line-chart"></canvas>
</div>

Leave a comment