php - 在 PHP 中将 mysql 数据加载到谷歌图表

标签 php mysql google-visualization

你能帮忙吗?下面是用于获取 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'

参见 DataTable addColumn

关于php - 在 PHP 中将 mysql 数据加载到谷歌图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41681175/

相关文章:

php - $this 关键字是什么意思?

PHP 剥离内容以固定行数

php - 在查询前调用 session_write_close 是做什么的?

mysql - 如何在 MySQL 的更新语法中使用 order by

json - 如何避免使用 Perl JSON 模块在数字周围加上引号?

php - 从两个表内连接中删除*

触发器内的 select 语句中的 MySQL 错误

MySQL 没有结果,因为外键是 NULL

javascript - 当数据类型为数字时,如何对谷歌图X轴进行排序

javascript - Barchart/Columnchart - 同一系列中相同颜色的条形图