Google图表值超出了Bootstrap模式中的饼图

时间:2018-02-01 10:05:29

标签: javascript jquery twitter-bootstrap charts google-visualization

我正在使用Google图表来显示我的数据,并且我使用三维饼图在bootstrap模式中显示数据但是该值超出了我在谷歌搜索的饼图,但得到了注意问题

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
      </div>
      <div class="modal-body">
        <div class="chart" id="chart_div"></div>
      </div>
      <div class="modal-footer">
      </div>
    </div>
  </div>
</div>

我的jsfiddle: - https://jsfiddle.net/L0pt229a/

1 个答案:

答案 0 :(得分:2)

标签的问题是由于在隐藏容器时绘制图表

当谷歌加载时,

而不是绘制图表 等到模态显示

关于模态,请听 - &gt; 'shown.bs.modal'
然后绘制图表,参见下面的工作片段......

&#13;
&#13;
google.charts.load('current', {
  packages:['corechart']
}).then(function () {
  $("#myModal").on('shown.bs.modal', function () {
    var data = google.visualization.arrayToDataTable([
      ['Task', 'Hours per Day'],
      ['Work',     11],
      ['Eat',      2],
      ['Commute',  2],
      ['Watch TV', 2],
      ['Sleep',    7]
    ]);

    var options = {
      title: 'My Daily Activities',
      is3D: true
    };

    var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
    chart.draw(data, options);
  });
});
&#13;
.button {
  left: 50%;
  margin: 0;
  position: absolute;
  top: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
}

.chart {
  align-content: center;
  display: flex;
  justify-content: center;
}

.modal {
  text-align: center;
}

@media screen and (min-width: 768px) {
  .modal:before {
    content: " ";
    display: inline-block;
    height: 100%;
    vertical-align: middle;
  }
}

.modal-dialog {
  display: inline-block;
  min-height: 230px;
  min-width: 430px;
  text-align: center;
  vertical-align: middle;
}

.modal-footer {
  color: #00b5e6;
  font-size: 25px;
  text-align: center;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://www.gstatic.com/charts/loader.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<!-- Chart -->
<div class="button">
  <button class="btn btn-primary" data-toggle="modal" data-target="#myModal" class="myModal">Chart</button>
</div>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
      </div>
      <div class="modal-body">
        <div class="chart" id="chart_div"></div>
      </div>
      <div class="modal-footer">
      </div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

注意:一个问题,图表将遵循其容器的大小
模态将遵循内容的大小
所以你必须给它们一个尺寸,
或图表将是豌豆的大小 在上面的例子中,模态给出了最小css大小