admin管理员组文章数量:1320648
I was using Chart.js and found that it recognizes the following two style
s 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 style
s 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?
1 Answer
Reset to default 5There 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>
本文标签: javascriptWidth and Height in ChartjsStack Overflow
版权声明:本文标题:javascript - Width and Height in Chart.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742066878a2418889.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论