最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Width and Height in Chart.js - Stack Overflow

programmeradmin1浏览0评论

I was using Chart.js and found that it recognizes the following two styles differently—<canvas style=""> works well, while <style>canvas{} distorts the charts. Here're two examples.

<!doctype html>
<html>

<canvas id=Figure1 style="width:640px;height:480px"></canvas>

<script src=.js/2.7.3/Chart.min.js></script>
<script>
new Chart(document.getElementById("Figure1"),{type:"scatter",data:{datasets:[{data:[{x:1,y:1},{x:2,y:2},{x:3,y:3}]}]},options:{responsive:false}});
</script>
</html>

This code doesn't twist the image, so I wanted to apply the settings globally with the code below.

<!doctype html>
<html>

<style>canvas{width:640px;height:480px}</style>

<canvas id=Figure1></canvas>
<script src=.js/2.7.3/Chart.min.js></script>
<script>
new Chart(document.getElementById("Figure1"),{type:"scatter",data:{datasets:[{data:[{x:1,y:1},{x:2,y:2},{x:3,y:3}]}]},options:{responsive:false}});
</script>
</html>

I just relocated width:640px;height:480px in this code, but it stretches the image weirdly. Must I always use <canvas style=""> to resize the Chart.js images?

I was using Chart.js and found that it recognizes the following two styles differently—<canvas style=""> works well, while <style>canvas{} distorts the charts. Here're two examples.

<!doctype html>
<html>

<canvas id=Figure1 style="width:640px;height:480px"></canvas>

<script src=https://cdnjs.cloudflare./ajax/libs/Chart.js/2.7.3/Chart.min.js></script>
<script>
new Chart(document.getElementById("Figure1"),{type:"scatter",data:{datasets:[{data:[{x:1,y:1},{x:2,y:2},{x:3,y:3}]}]},options:{responsive:false}});
</script>
</html>

This code doesn't twist the image, so I wanted to apply the settings globally with the code below.

<!doctype html>
<html>

<style>canvas{width:640px;height:480px}</style>

<canvas id=Figure1></canvas>
<script src=https://cdnjs.cloudflare./ajax/libs/Chart.js/2.7.3/Chart.min.js></script>
<script>
new Chart(document.getElementById("Figure1"),{type:"scatter",data:{datasets:[{data:[{x:1,y:1},{x:2,y:2},{x:3,y:3}]}]},options:{responsive:false}});
</script>
</html>

I just relocated width:640px;height:480px in this code, but it stretches the image weirdly. Must I always use <canvas style=""> to resize the Chart.js images?

Share Improve this question asked Feb 22, 2019 at 18:05 Junyong KimJunyong Kim 2891 gold badge3 silver badges15 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 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./ajax/libs/Chart.js/2.7.3/Chart.bundle.js"></script>

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

发布评论

评论列表(0)

  1. 暂无评论