Reputation: 514
I use canvas width and highth as follow
<canvas id="myChart" height="400" width="400" ></canvas>
However, when the page is rendered the canvas somehow becomes
<canvas height="1643" id="myChart" width="1643" style="display: block; width: 1643px; height: 1643px;"></canvas>
I found similar problem and such solution works for me too,
options: {
responsive: false
}
But I'm not sure that that is proper solution just to turn off responsive option becaouse that is one of the cool thing in chart.js
my code
<Html>
<head>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.2.2/Chart.js" ></script>
</head>
<Body>
<canvas id="myChart" height="400" width="400" ></canvas>
</Body>
<script>
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Steps',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
},
{
label: "Goal Line",
type:'line',
data: [{x:0,y:10},{x:0,y:10},{x:0,y:10},{x:0,y:10},{x:0,y:10},{x:0,y:10}],
fill: false,
pointRadius:[0,0,0,0,0,0,0],
borderWidth: 5,
borderColor: "rgba(243, 52, 52,1)",
borderJoinStyle: 'miter',
}
]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true
},
}],
xAxes: [{
ticks: {
beginAtZero:true
},
}]
},
}
});
</script>
</Html>
Upvotes: 1
Views: 2388
Reputation: 11
Note: You don't need to set a width via css. You can place the element within a grid column. Charts are responsive, so they will adjust the width to the size of the parent.
<div class="col-md-5">
<canvas id="myChart" ></canvas>
</div>
Upvotes: 1
Reputation: 4096
You can use css max-width and min-width if you want to avoid your chart from being too big or too small:
<canvas id="myChart" height="400" width="400" style="min-width: 200px; max-width: 600px"></canvas>
Upvotes: 4