设置宽度和高度

2022-01-22 00:00:00 html css chart.js

我正在尝试 Chart.js 的示例代码 given在文档中.

I'm trying out the example code for Chart.js given in the docs.

在 400px/400px 的画布元素上内联指定宽度和高度.

Width and height is specified inline on the canvas element at 400px/400px.

但在渲染图表时,它会被放大到整个页面宽度,并切断最右端.

But when rendering the chart it's blown up to full page width, and cuts off the far right end.

我应该如何/在哪里控制图表的宽度/高度?

How/where am I supposed to control the width/height of the chart?

推荐答案

可以覆盖画布样式宽度!important ...

You can override the canvas style width !important ...

canvas{

  width:1000px !important;
  height:600px !important;

}

还有

在选项下指定 responsive:true, 属性..

specify responsive:true, property under options..

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

更新选项下添加:maintainAspectRatio: false,

链接:http://codepen.io/theConstructor/pen/KMpqvo

相关文章