简体   繁体   English

响应式Chart.js画布barplot高度?

[英]Responsive Chart.js Canvas barplot height?

I have having difficulties setting the height of my Chart.js barplot correctly (see screenshot). 我在正确设置Chart.js条形图的高度时遇到困难(请参见屏幕截图)。 The

responsive:true,
maintainAspectRatio: false,

does not seem to be taken into account here. 这里似乎没有考虑到。 I am expecting to have the plot height to take 100% of the box. 我希望地块高能占据100%的盒子。

Could anyone advice, as I scanned around, and didn't really found much. 在我四处搜寻时,没有人能提供任何建议,但真的没发现什么。

在此处输入图片说明

 <div class="col-md-4" style="flex-direction: inherit"> <div class="box box-primary"> <div class="box-header with-border"> <i class="fa fa-line-chart"></i> <h3 class="box-title">dataset growth</h3> </div> <div id="boby" class="box-body"> <div class="chart-container" style="position: relative"> <div class="chart"> <canvas id="barChart" width="100%" height="100%"></canvas> </div> </div> </div> </div> </div> <!-- page script --> <script> var numberWithCommas = function(x) { return x.toString().replace(/\\B(?=(\\d{3})+(?!\\d))/g, ","); }; var dataHsap = [395, 2829, 6498]; var dataAtha = [0, 0, 1272]; var dates = ["2015", "2018", "2020"]; var bar_ctx = document.getElementById('barChart'); var boby = document.getElementById('boby'); var data = { labels: dates, datasets: [{ label: 'Human', data: dataHsap, backgroundColor: "#3c8dbc", hoverBackgroundColor: "#77b2d4", hoverBorderWidth: 0 }, { label: 'Thaliana', data: dataAtha, backgroundColor: "#00a65a", hoverBackgroundColor: "#00cc70", hoverBorderWidth: 0 }, ] }; var options = { responsive: true, maintainAspectRatio: false, animation: { duration: 10, }, tooltips: { mode: 'label', callbacks: { label: function(tooltipItem, data) { return data.datasets[tooltipItem.datasetIndex].label + ": " + numberWithCommas(tooltipItem.yLabel); } } }, scales: { xAxes: [{ stacked: true, gridLines: { display: false }, }], yAxes: [{ stacked: true, ticks: { callback: function(value) { return numberWithCommas(value); }, }, }], }, legend: { display: true } }; var bar_chart = new Chart(bar_ctx, { type: 'bar', data: data, options: options }); </script> <div class="col-md-4" style="flex-direction: inherit"> <div class="box box-primary"> <div class="box-header with-border"> <i class="fa fa-line-chart"></i> <h3 class="box-title">ReMap dataset growth</h3> </div> <div id="boby" class="box-body"> <div class="chart-container" style="position: relative"> <div class="chart"> <canvas id="barChart" width="100%" height="100%"></canvas> </div> </div> </div> </div> </div> <!-- page script --> <script> var numberWithCommas = function(x) { return x.toString().replace(/\\B(?=(\\d{3})+(?!\\d))/g, ","); }; var dataHsap = [395, 2829, 6498]; var dataAtha = [0, 0, 1272]; var dates = ["2015", "2018", "2020"]; var bar_ctx = document.getElementById('barChart'); var boby = document.getElementById('boby'); var data = { labels: dates, datasets: [{ label: 'Human', data: dataHsap, backgroundColor: "#3c8dbc", hoverBackgroundColor: "#77b2d4", hoverBorderWidth: 0 }, { label: 'Thaliana', data: dataAtha, backgroundColor: "#00a65a", hoverBackgroundColor: "#00cc70", hoverBorderWidth: 0 }, ] }; var options = { responsive: true, maintainAspectRatio: false, animation: { duration: 10, }, tooltips: { mode: 'label', callbacks: { label: function(tooltipItem, data) { return data.datasets[tooltipItem.datasetIndex].label + ": " + numberWithCommas(tooltipItem.yLabel); } } }, scales: { xAxes: [{ stacked: true, gridLines: { display: false }, }], yAxes: [{ stacked: true, ticks: { callback: function(value) { return numberWithCommas(value); }, }, }], }, legend: { display: true } }; var bar_chart = new Chart(bar_ctx, { type: 'bar', data: data, options: options }); </script> 

Your chart doesn't know how big it should be. 您的图表不知道应该有多大。 You should add a height-property to the div that contains your canvas. 您应该在包含画布的div中添加一个高度属性。

<div class="chart" style="height: 300px">
  <canvas id="barChart" width="100%" height="100%"></canvas>
</div>

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

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