简体   繁体   English

防止画布图表JS的大小等于窗口的高度和宽度

[英]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. 我正在使用chartjs显示数据,并且图表(画布)占用了窗口的100%宽度和100%高度。

I want to reduce this to be 600px by 600px . 我想将其减少为600px x 600px To do this I tried using Chart.defaults.global.legend.fullWidth = false; 为此,我尝试使用Chart.defaults.global.legend.fullWidth = false; however this didn't work. 但是,这没有用。

I have created a jsfiddle her e for your convenience. 为了方便起见,我创建了一个jsfiddle她的 e。

See code below: 参见下面的代码:

HTML 的HTML

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

JS 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
                }
            }]
        }
    }
});

Add 'responsive: false' to the chart options 在图表选项中添加“ response:false”

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM