phantom phantom - 1 month ago 14
Javascript Question

Prevent size of canvas chart js to equal window height and width

I am using chartjs to display data and the chart (canvas) takes up 100% width and 100% height of the window.

I want to reduce this to be

600px
by
600px
. To do this I tried using
Chart.defaults.global.legend.fullWidth = false;
however this didn't work.

I have created a jsfiddle here for your convenience.

See code below:

HTML

<canvas id="myChart" width="400" height="400"></canvas>


JS

var ctx = document.getElementById("myChart");
Chart.defaults.global.defaultFontColor = "#000";
Chart.defaults.global.legend.fullWidth = false;
var myChart = new Chart(ctx, {
type: 'pie',
data: {
labels: labels,
datasets: [{
label: '# of Followers',
data: followers,
backgroundColor: [
'rgba(255, 99, 132, 0.9)',
'rgba(54, 162, 235, 0.9)',
'rgba(255, 206, 86, 0.9)',
'rgba(75, 192, 192, 0.9)',
'rgba(153, 102, 255, 0.9)',
'rgba(255, 159, 64, 0.9)'
],
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: 5
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});

Answer

Add 'responsive: false' to the chart options

options: {
        responsive: false,
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero:true
                }
            }]
        }
    }