Reputation: 940
I am using Chart.js with the following html:
<canvas id="fisheye" width="100" height="100" style="border:1px solid;"></canvas>
And this is the Javascript part :
getChart: function () {
var ctx = document.getElementById("fisheye");
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ["json1", "json2", "json3", "json4"],
datasets: [{
label: '1.3.0 Version',
type: 'bar',
data: [52.4060092977, 90.0854057722, 196.576968515, 77.6216726434],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(255, 99, 132, 0.2)',
'rgba(255, 99, 132, 0.2)',
'rgba(255, 99, 132, 0.2)'
// 'rgba(54, 162, 235, 0.2)',
// 'rgba(255, 206, 86, 0.2)',
// 'rgba(75, 192, 192, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(255,99,132,1)',
'rgba(255,99,132,1)',
'rgba(255,99,132,1)'
// 'rgba(54, 162, 235, 1)',
// 'rgba(255, 206, 86, 1)',
// 'rgba(75, 192, 192, 1)'
],
borderWidth: 1
},
{
label: '1.3.13 Version',
type: 'bar',
data: [50.0744953192, 93.5542439586, 153.288788005, 101.402897964],
backgroundColor: [
'rgba(54, 162, 235, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(54, 162, 235, 0.2)'
// 'rgba(255, 206, 86, 0.2)',
// 'rgba(75, 192, 192, 0.2)',
// 'rgba(153, 102, 255, 0.2)'
],
borderColor: [
'rgba(54, 162, 235, 1)',
'rgba(54, 162, 235, 1)',
'rgba(54, 162, 235, 1)',
'rgba(54, 162, 235, 1)'
// 'rgba(255, 206, 86, 1)',
// 'rgba(75, 192, 192, 1)',
// 'rgba(153, 102, 255, 1)'
],
borderWidth: 1
}
]
},
options: {
title: {
display: true,
text: 'Export Chart'
},
legend: {
display: true,
},
scales: {
yAxes: [{
ticks: {
beginAtZero:true,
stepSize:50
}
}]
},
responsive: true
}
});
return myChart;
}
My problem is that my chart seems to expand to full screen height and width. I would like to restrict my chart size to the canvas size. This way I can lay charts in a grid rather than per page (Note: I have shown code for only one chart, but you can imagine it to be similar for other charts)
Upvotes: 1
Views: 260
Reputation: 3026
It might be easier to wrap the canvas in a div/span/something and set the size there. As you have set responsive: true this means that chart.js tries to fill the parent on resize and it does this by setting the canvas height and width. https://jsfiddle.net/zpvfph9m/1/
<div style="width:50%">
<canvas id="fisheye" width="400" height="400"></canvas>
</div>
Would this work for you?
Upvotes: 1