你能帮忙吗?下面是用于获取 MYSQL 数据并将其传递给谷歌图表的脚本
$result = DB_query($sql, '', '', False, False);
$table = array();
$table['cols'] = array(
array('label' => 'col1', 'type' => 'string'),
array('label' => 'col2', 'type' => 'int')
);
$rows = array();
while($r = mysqli_fetch_assoc($result)) {
$temp = array();
// the following line will be used to slice the Pie chart
$temp[] = array('v' => (string) $r['col1']);
// Values of each slice
$temp[] = array('v' => (int) $r['col2']);
$rows[] = array('c' => $temp);
}
下面是js部分
echo
'
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {"packages":["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable(',$jsonTable,');
var options = {
title: "My Weekly Plan",
is3D: "true",
width: 800,
height: 600
};
var chart = new google.visualization.PieChart(document.getElementById("chart_div"));
chart.draw(data, options);
}
</script>
<div id="chart_div"></div>
';
这里的问题是,即使我在 JSON 变量中获得了正确的值,但在将相同的值加载到谷歌图表时出现以下错误,因此图表没有显示。
"Uncaught Error: Invalid type, int, for column "col2".
at gvjs_R.gvjs_.Sda
能否请您帮助理解这个问题。
最佳答案
将 'int'
更改为 'number'
google 数据表的有效类型...
type - A string with the data type of the values of the column. The type can be one of the following: 'string', 'number', 'boolean', 'date', 'datetime', and 'timeofday'
关于php - 在 PHP 中将 mysql 数据加载到谷歌图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41681175/