How to make Chart JS responsive?

 ̄綄美尐妖づ 提交于 2019-12-12 12:26:52

问题


I am using Chart.js with Twitter Bootstrap template. The Charts are not sized correctly if I don't mention a height and width for Canvas. But if I specify width and height then the charts are not responsive. How can I solve this issue?


回答1:


Chart.js has a property (responsive) that you can configure at the global or chart level via options that will make the chart responsive. See http://www.chartjs.org/docs/#getting-started-global-chart-configuration > responsive

// Boolean - whether or not the chart should be responsive and resize when the browser does.
responsive: true,

true makes it responsive. false (default) makes it non-responsive.


Example usage (only the relevant part, check out the CodePen for the full exam)

var ctx = $("#bar").get(0).getContext("2d");
var myChart = new Chart(ctx).Bar(barData, {
  responsive: true
});

CodePen - http://codepen.io/anon/pen/bdrGVx

You might also want to check out the maintainAspectRatio option, if you want to maintain the ratio of width to height.



来源:https://stackoverflow.com/questions/30821223/how-to-make-chart-js-responsive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!