Chart.js ignoring canvas height amp; width(Chart.js 忽略画布高度和宽度)
问题描述
遵循 Chart.js 文档我正在尝试绘制一个小图表:
Following the Chart.js documentation I am trying to draw a small chart:
<canvas id="myChart" width="400" height="400"></canvas>
<script>
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels:['15-2016','16-2016','17-2016',],
datasets:[
{
label:'Yes',
data:[3.4884,1.1628,1.3514,],
backgroundColor:'rgba(75,192,192,1)',
borderColor:'rgba(75,192,192,1)',
pointRadius:0
},
{
label:'Maybe',
data:[0.5571,1.3298,0.791,],
backgroundColor:'rgba(255,206,86,1)',
borderColor:'rgba(255,206,86,1)',
pointRadius:0
},
{
label:'No',
data:[0,0,0,],
backgroundColor:'rgba(255,99,132,1)',
borderColor:'rgba(255,99,132,1)',
pointRadius:0
}
]
}
});
</script>
但是,当页面被渲染时,画布会以某种方式变成
However, when the page is rendered the canvas somehow becomes
<canvas style="height: 929px; width: 929px;" id="myChart" width="1858" height="1858"></canvas>
这对我的需要来说太大了.如何将画布的宽度和高度设置为我想要的?
which is waaaaay too big for my needs. How can I set the width and height of the canvas to be what I want?
推荐答案
我可以通过关闭响应来硬编码图表的大小:
I was able to hardcode the size of the graph by turning off responsiveness:
options: {
responsive: false
}
这篇关于Chart.js 忽略画布高度和宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Chart.js 忽略画布高度和宽度


基础教程推荐
- 我什么时候应该在导入时使用方括号 2022-01-01
- 动态更新多个选择框 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 在for循环中使用setTimeout 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01