无法使谷歌图表响应

时间:2017-10-05 06:58:07

标签: html charts responsive-design google-visualization bootstrap-modal

我试图让我的谷歌图表响应。谷歌图表位于bootstrap模式。

javascript代码:

 var jsonData = $.ajax({
        url: "testChart.php",
        dataType: "json",
        async: false
    }).responseText;


    var data = new google.visualization.DataTable(jsonData);

    var options = {
        chart: {
            title: 'Values',
        },
        width: 900,
        height: 600
    };

    //var chart = new google.charts.Line(document.getElementById('chart_div'));
    var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
    //chart.draw(data, google.charts.Line.convertOptions(options));
    chart.draw(data, options);

    $('#chartModal').modal("show");

HTML代码:

<div id="chartModal" class="modal fade">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <h4 class="modal-title">Chart</h4>
        </div>
        <div class="modal-body" id="chart_details">
            <div id="chart_div"></div>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
    </div>
</div>

电话模式中的图表图像&#34;现在:

chart in bootstrap modal resized

*我已尝试以百分比形式在javascript中创建宽度和高度值,但无效。

1 个答案:

答案 0 :(得分:0)

图表将遵循其容器的大小
但只有当容器可见时

因此,需要等到模态显示,
在绘制图表之前

听这个事件 - &gt; 'shown.bs.modal'

请参阅以下工作代码段...

&#13;
&#13;
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://www.gstatic.com/charts/loader.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <script>
    google.charts.load('current', {
      packages: ['corechart']
    }).then(function () {
      $("#myModal").on('shown.bs.modal', function () {
        var data = google.visualization.arrayToDataTable([
          ['x', 'y'],
          [0, 0],
          [1, 1],
          [2, 2],
          [3, 3]
        ]);

        var options = {
          chartArea: {
            // these options leave room for the title, axis-labels, etc...
            top: 16,
            left: 24,
            right: 16,
            bottom: 36,

            height: '100%',
            width: '100%'
          },
          height: '100%',
          width: '100%'
        };

        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        chart.draw(data, options);
      });
    });
  </script>
</head>
<body>

<div class="container">
  <h2>Modal Example</h2>
  <!-- Trigger the modal with a button -->
  <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>

  <!-- Modal -->
  <div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog modal-lg">

      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Modal Header</h4>
        </div>
        <div class="modal-body test">
          <div id="chart_div"></div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>

    </div>
  </div>

</div>

</body>
</html>
&#13;
&#13;
&#13;