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>