简体   繁体   English

chartjs-画布大小错误

[英]chartjs - canvas size error

I am using chartjs 2.0 to build charts. 我正在使用chartjs 2.0构建图表。 I specified my canvas size to be 200 x 200. However, the canvas comes out to be 1218 x 1218... Does anyone know why this is happening? 我指定的画布尺寸为200 x200。但是,画布的尺寸为1218 x 1218 ...有人知道为什么会这样吗? Here's my fiddle: 这是我的小提琴:

https://jsfiddle.net/cdxvokw0/ https://jsfiddle.net/cdxvokw0/

My html: 我的html:

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

My javascript: 我的JavaScript:

var dateArray = ['1', '2', '3', '4'];
var data_array = ['10', '20', '30', '40']
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
    type: 'line',
    data: {
        labels: dateArray,
        datasets: [{
            label: '# of Votes',
            data: data_array,
        }]
    },
    options: {
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero:true
                },
                scaleLabel: {
                    display: true,
                    labelString: 'Probability'
                }
            }]
        }
    }
});

Thanks in advance! 提前致谢!

You need to add responsive: false to you chart configuration 您需要为图表配置添加responsive: false

var myChart = new Chart(ctx, {
    type: 'line',
    data: {
        ...
    },
    options: {
        responsive: false,
        scales: {
            ...
        }
    }
});

Otherwise, the chart tries to be as large as its container 否则,图表将尝试与其容器一样大

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

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