0

我目前正在使用带有 PHP 的 Google Charts 来填充数据,但图表的交互性停止工作,我不知道为什么。

代码如下:

<?php
// Connect to DB and execute while loop to get values
...
...

echo '<div id="chart_div"></div>';

?>

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">

  // Load the Visualization API and the piechart package.
  google.load('visualization', '1.0', {'packages':['corechart']});

  // Set a callback to run when the Google Visualization API is loaded.
  google.setOnLoadCallback(drawChart);

  // Callback that creates and populates a data table,
  // instantiates the pie chart, passes in the data and
  // draws it.
  function drawChart() {

    // Create the data table.
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Review Score');
    data.addColumn('number', 'Number of Reviews');
    data.addRows([
      ['1 Star Reviews', <?php echo $one; ?>],
      ['2 Star Reviews', <?php echo $two; ?>],
      ['3 Star Reviews', <?php echo $three; ?>],
      ['4 Star Reviews', <?php echo $four; ?>],
      ['5 Star Reviews', <?php echo $five; ?>]
    ]);

    // Set chart options
    var options = {'title':'Breakdown of Review Scores',
                   'is3D':true,
                   'width':600,
                   'height':400};

    // Instantiate and draw our chart, passing in some options.
    var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
    chart.draw(data, options);
  }

即使我chart_div在 javascript 之后移动到它也不起作用。

万一有人问 js 不在head标签中,那是因为我还显示了一个填充了 sql 查询结果的表,并根据这些结果计算总计以在图表中使用。

我已经找到了这个页面,但不明白为什么会改变任何东西: https ://developers.google.com/chart/interactive/docs/php_example

4

1 回答 1

0

我的意思是尽快更新,但问题出在其他地方 - 当使用 php 填充图表数据时,Google Charts 工作得非常好。

于 2012-07-23T16:20:05.227 回答