问题描述
我搜索了很多,找到了一个使用 MySQL 表数据作为数据源生成 Google 图表的好例子.我搜索了几天,发现可以使用
之前从StackOverflow那里得到了很多帮助,所以这次我会返回一些.
我有两个例子;一个使用 Ajax,另一个不使用.今天,我只会展示非 Ajax 的例子.
用法:
要求:
'string'),数组('标签' => '百分比','类型' => '数字'));$rows = array();while($r = mysql_fetch_assoc($sth)) {$temp = 数组();//以下行将用于切片饼图$temp[] = array('v' => (string) $r['Weekly_task']);//每个切片的值$temp[] = array('v' => (int) $r['percentage']);$rows[] = array('c' => $temp);}$table['rows'] = $rows;$jsonTable = json_encode($table);//echo $jsonTable;?>
query('SELECT * FROM googlechart');/*---------------------------示例数据:表格(googlechart)--------------------------每周_任务百分比睡眠 30看电影 10工作 40练习 20*/$rows = array();$table = 数组();$table['cols'] = 数组(//图表的标签,这些标签代表列标题./*请注意,一列采用字符串"格式,另一列采用数字"格式因为饼图只需要数字"来计算百分比和字符串将用于切片标题*/array('label' => '每周任务', 'type' => 'string'),数组('标签' => '百分比','类型' => '数字'));/* 从 $result 中提取信息 */foreach($result as $r) {$temp = 数组();//以下行将用于切片饼图$temp[] = array('v' => (string) $r['weekly_task']);//每个切片的值$temp[] = array('v' => (int) $r['percentage']);$rows[] = array('c' => $temp);}$table['rows'] = $rows;//将数据转换为JSON格式$jsonTable = json_encode($table);//echo $jsonTable;} catch(PDOException $e) {回声'错误:'.$e->getMessage();}?>
'每周任务', 'type' => 'string'),数组('标签' => '百分比','类型' => '数字'));/* 从 $result 中提取信息 */foreach($result as $r) {$temp = 数组();//以下行将用于切片饼图$temp[] = array('v' => (string) $r['weekly_task']);//每个切片的值$temp[] = array('v' => (int) $r['percentage']);$rows[] = array('c' => $temp);}$table['rows'] = $rows;//将数据转换为JSON格式$jsonTable = json_encode($table);//echo $jsonTable;?>
有些人可能会在本地或服务器上遇到此错误:
语法错误 var data = new google.visualization.DataTable(<?=$jsonTable?>);
这意味着他们的环境不支持短标签,解决方案是使用它:
一切正常!
I have searched a lot to find a good example for generating a Google Chart using MySQL table data as the data source. I searched for a couple of days and realised that there are few examples available for generating a Google Chart (pie, bar, column, table) using a combination of
I have previously received a lot of help from StackOverflow, so this time I will return some.
I have two examples; one uses Ajax and the other does not. Today, I will only show the non-Ajax example.
Usage:
Requirements:
<?
<?
<?
解决方案
Some might encounter this error either locally or on the server:
syntax error var data = new google.visualization.DataTable(<?=$jsonTable?>);
This means that their environment does not support short tags the solution is to use this instead:
<?
And everything should work fine!
这篇关于