简体   繁体   English

带有 Bootstrap 的 Chart.JS 全宽响应式圆环图

[英]Chart.JS full-width, responsive doughnut chart with Bootstrap

I'm using the Angular.js variant but the core of Chart.js is exactly the same.我使用的是Angular.js 变体,Chart.js的核心是完全一样的。

I have a simple doughnut chart, that I'm using within a responsive Bootstrap container:我有一个简单的圆环图,我在响应式 Bootstrap 容器中使用它:

<div class="row">
    <div class="col-xs-8">
       <div class="chart-container">
          <canvas id="doughnut" 
                  chart-data="data" 
                  chart-labels="labels" 
                  chart-options="mainDonut" 
                  class="chart chart-doughnut">
          </canvas>
       </div>
    </div>
</div>

Nothing fancy.没有什么花哨。 My controller options are:我的控制器选项是:

 $scope.mainDonut ={
    percentageInnerCutout : 90,
    responsive: true
}

It seems like the width of the chart is limited to whatever the height of the div is, which for me is magically being set at about 300px by ChartJS for some reason.似乎图表的宽度仅限于 div 的高度,对我来说,出于某种原因,ChartJS 神奇地将其设置为大约 300 像素。 Here's how it renders:这是它的呈现方式:

chart.js 响应式引导程序

I want the height to be responsive to whatever my .col-xs-8 's width is, and fill the entire div.我希望高度能够响应我的.col-xs-8的宽度,并填充整个 div。 I understand that I can do this with Javascript by watching for window resize events, but I want to avoid that if possible.我知道我可以通过观察窗口调整大小事件来使用 Javascript 来做到这一点,但如果可能的话,我想避免这种情况。

Any solutions here?这里有什么解决方案吗?

If you want to make it responsive and preserve aspect ratio, 2 things to consider:如果您想让它具有响应性并保持纵横比,需要考虑两件事:

1) In options : 1) 在options

{
   responsive: true,
   maintainAspectRatio: true, 
}

2) Define the aspect ratio by setting width and height attributes to the canvas . 2) 通过为canvas设置widthheight属性来定义纵横比。 In your case they're probably equal:在您的情况下,它们可能相等:

<canvas id="doughnut" 
    width="100"
    height="100"
    chart-data="data" 
    chart-labels="labels" 
    chart-options="mainDonut" 
    class="chart chart-doughnut">
</canvas>

It seems like the width of the chart is limited to whatever the height of the div is, which for me is magically being set at about 300px by ChartJS for some reason.似乎图表的宽度仅限于 div 的高度,对我来说,出于某种原因,ChartJS 神奇地将其设置为大约 300 像素。

That's the default width of the canvas element ( http://www.w3.org/TR/html5/scripting-1.html#the-canvas-element ).这是画布元素的默认宽度( http://www.w3.org/TR/html5/scripting-1.html#the-canvas-element )。

This means that your responsive option is not actually doing what it's supposed to.这意味着您的responsive选项实际上并没有做它应该做的事情。 The most likely reason is that the the wrapping element does not have a (measurable) size when the chart is being initialized.最可能的原因是在初始化图表时包装元素没有(可测量的)大小。 For Angular, the most common reason is the wrapping (parent) elements not being in a fully rendered state when the chart is being initialized (eg. it has an ng-show or it's a hidden tab, etc.).对于 Angular,最常见的原因是在初始化图表时包装(父)元素未处于完全呈现状态(例如,它有一个ng-show或它是一个隐藏的选项卡等)。

CSS on the parent element could also be a problem - eg the parent elements don't have a size (but looking at your markup this seems a bit unlikely)父元素上的 CSS 也可能是一个问题 - 例如父元素没有大小(但查看您的标记似乎不太可能)


Note - even though you don't do a chart resize on window resize, Chart.js automatically does it for all charts that are configured as responsive.注意 - 即使您没有在窗口调整大小时调整图表大小,Chart.js 也会自动为所有配置为响应式的图表执行此操作。

function initChart() {
    var canvas = document.getElementById('chart-canvas');
    canvas.width = parent.offsetWidth;
    canvas.height = parent.offsetWidth; ... 
}

+css parent block {
   width: 100%;
   min-height: 300px;
}

Hope this will helps you.希望这会帮助你。

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

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