简体   繁体   English

Chart.js 的自动高度

[英]Automatic Height for Chart.js

I'm following the documentation correctly with Chart.js and my Bar Chart is appearing too big until I click inspect element, then the chart seems to resize and snap into the correct position.我正在使用 Chart.js 正确跟踪文档,并且我的条形图看起来太大,直到我单击检查元素,然后图表似乎调整大小并卡入正确的位置。

Quick Screencast of Issue问题的快速截屏

There's no inline styles on my canvas element, the following styles come into play once I go to Inspect Element.我的画布元素上没有内联样式,一旦我转到 Inspect Element,就会使用以下样式。 I'm not sure where or how the chart is getting those inline styles.我不确定图表在哪里或如何获得这些内联样式。

canvas{
 width: 100% !important;
 max-width: 800px;
 height: auto !important;}

Try尝试

var barChart = new Chart(ctx, {
  type: 'bar',
  responsive: true,
  maintainAspectRatio: false,
  data: {
    ...
  },
  options: {
    animation: {
      onComplete: function (animation) {
        this.options.animation.onComplete = null; // disable after the first render
        animation.chartInstance.resize()
      }
    }
  }
});

as an alternative, why do you set the width to 100% but not the height ?作为替代方案,为什么将width设置为 100% 而不是height In your screen vid it's inside of another div anyways so I'd try playing w/ the height too.在您的屏幕视频中,它无论如何都在另一个div内,所以我也会尝试使用height播放。 Finally, I'd refrain from using the global canvas CSS selector -- rather use canvas#barChart because otherwise it'll be applied to all canvases, not just the chartjs ones.最后,我将避免使用全局canvas CSS 选择器——而是使用canvas#barChart ,否则它将应用于所有画布,而不仅仅是chartjs画布。

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

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