我目前正在使用带有 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