admin管理员组文章数量:1202815
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?
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?
Share Improve this question asked Jun 1, 2016 at 20:09 wogslandwogsland 9,50820 gold badges61 silver badges97 bronze badges 1- if you can get to it, try to set a breakpoint in the core.helper.js file of chartjs and its getting its height from the canvasElementReference.parentNode so if css isnt controlling that, you could add rules for css to style its parent and let the canvasElementReference.parentNode.clientWidth and heights be based off of the css style. Thats the actual code logic if you can follow what i mean itll fix your issue for anyone else that cares – Coty Embry Commented Mar 8, 2019 at 22:31
3 Answers
Reset to default 12I was able to hardcode the size of the graph by turning off responsiveness:
options: {
responsive: false
}
I solved the problem by adding "maintainAspectRatio: false" to the options.
options : {
maintainAspectRatio: false
};
Using Both options, maintainAspectRatio: false AND responsive: false seem to do the trick of making the chart fit the canvas as sized in the canvas height and width tags.
本文标签: javascriptChartjs ignoring canvas height amp widthStack Overflow
版权声明:本文标题:javascript - Chart.js ignoring canvas height & width - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738646345a2104608.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论