我目前正在使用带有PHP的Google图表来填充数据,但是图表的交互性停止工作,我不知道为什么。
代码如下:
<?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
我打算更快地更新,但问题出在其他地方 - 谷歌图表在使用 php 填充图表数据时工作得很好。