谷歌可视化API:使用交叉表格式的MySQL结果 [英] Google Visualization API: Format mysql result with cross tabulation

查看:101
本文介绍了谷歌可视化API:使用交叉表格式的MySQL结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的谷歌Apps脚本我试图想象使用谷歌可视化API数据。
我已经能够从混合PHP和HTML给予很好的例子显示表。但数据库似乎不具有用于柱形图正确的格式。

表具有colums'天''用户'任务'小时'和数据然后被一个while循环被推入行下方

  $表1 =阵列();
$表1 ['COLS'] =阵列(//标签为图表,这些重新present列标题。
阵列('标签'=>'天','型'=>'串'),
阵列('标签'=>'用户','型'=>'串'),
阵列('标签'=>'任务','型'=>'串'),
阵列('标签'=>'小时','型'=>'号')
);$行=阵列();
而($ R = mysql_fetch_assoc($结果)){
$ TEMP =阵列();
//每一列都需要有通过$临时数组插入数据
 $温度[] =阵列('V'=> $ R ['天']);
 $温度[] =阵列('V'=> $ R ['用户']);
 $温度[] =阵列('V'=> $ R ['任务']);
 $温度[] =阵列('V'=> $ R ['小时']);//临时阵列插入$行
$行[] =阵列('C'=> $温度);
}
//填充该表与数据行
 表1 $ ['行'] = $行;

给我的表是这样的:

 用户每天工作小时
周一USER1洗3
周一USER2清洁2
周一用户3铁4
周二USER1清洗4
周二user2的铁1
周二洗用户3 3

我其实需要的是一个表是这样的:

 天的user1用户2用户3清洁洗铁
星期一3 2 4 2 4 3
周二4 1 3 4 1 3

我知道用户提前列表中。

编辑:问题是'(服务器端)对照表作为我的此链接
我想弄明白,并仍然会高兴之淡然标志的答案这样的每一个帮助和意志。
谢谢!


解决方案

要得到你所需要的格式,最简单的方法是在转动的MySQL查询您的数据。 MySQL不支持转点,但你可以捏造事实是这样的:

  SELECT
    天,
    SUM(IF(USER ='用户1',小时,0))为用户1,
    SUM(IF(USER ='用户2',小时,0))为用户2
    SUM(IF(USER ='用户3',小时,0))为用户3,
    SUM(IF(任务='干净',小时,0))一样干净,
    SUM(IF(任务='铁',小时,0))如铁,
    SUM(IF(任务='清洗',小时,0)),为洗涤
FROM mytable的
GROUP BY日

然后与为每个用户和任务的列构建数据表

  $表1 =阵列(
    COLS =>阵列(
        阵列('标签'=>'天','型'=>'串'),
        阵列('标签'=>'用户1','型'=>'串'),
        阵列('标签'=>用户2,类型= GT;'串'),
        阵列('标签'=>'用户3','型'= GT;'号'),
        阵列('标签'=>'干净','型'=>'号'),
        阵列('标签'=>'铁','型'=>'号'),
        阵列('标签'=>'洗','型'=>'号')
    )
    行=>阵列()
);
而($ R = mysql_fetch_assoc($结果)){
    表1 $ ['行'] [] =阵列('C'=>阵列(
        阵列('V'=> $ R ['天'])
        阵列('V'=> $ R ['USER1'])
        阵列('V'=> $ R ['user2的'])
        阵列('V'=> $ R ['用户3'])
        阵列('V'=> $ R ['干净'])
        阵列('V'=> $ R ['铁'])
        阵列('V'=> $ R ['洗'])
    ));
}

i'm new to google apps script and i'm trying to visualize data using the Google Visualization API. I have been able to display a table from the good examples given on mixing php and html. But the database does not seem to have the right format for the column charts.

The table has the colums 'day' 'user' 'task' 'hours' and the data is then being pushed into the rows underneath by a while loop:

$table1 = array();
$table1['cols'] = array(

// Labels for your chart, these represent the column titles.
array('label' => 'day', 'type' => 'string'),
array('label' => 'user', 'type' => 'string'),
array('label' => 'task', 'type' => 'string'),
array('label' => 'hours', 'type' => 'number')
);

$rows = array();
while($r = mysql_fetch_assoc($result)) {
$temp = array();
// each column needs to have data inserted via the $temp array
 $temp[] = array('v' => $r['day']);
 $temp[] = array('v' => $r['user']);
 $temp[] = array('v' => $r['task']);
 $temp[] = array('v' => $r['hours']);

// insert the temp array into $rows
$rows[] = array('c' => $temp);
}
// populate the table with rows of data
 $table1['rows'] = $rows;

giving me a table like this:

day     user    task    hours
monday  user1   wash    3
monday  user2   clean   2
monday  user3   iron    4
tuesday user1   clean   4
tuesday user2   iron    1
tuesday user3   wash    3

What i actually need is a table like this:

day     user1  user2  user3  clean  iron  wash
monday  3      2      4      2      4     3
tuesday 4      1      3      4      1     3

I know the list of users in advance.

EDIT: The issue is '(server side) cross tabulation' as i'm seeing in this link. I'm trying to figure it out and would still be glad for every help and will of couse mark answers as such. Thanks!

解决方案

To get the format you need, the easiest way is to pivot your data in the MySQL query. MySQL doesn't support pivots, but you can fake it like this:

SELECT
    day,
    SUM(IF(user = 'user1', hours, 0)) as user1,
    SUM(IF(user = 'user2', hours, 0)) as user2,
    SUM(IF(user = 'user3', hours, 0)) as user3,
    SUM(IF(task = 'clean', hours, 0)) as clean,
    SUM(IF(task = 'iron', hours, 0)) as iron,
    SUM(IF(task = 'wash', hours, 0)) as wash
FROM myTable
GROUP BY day

Then build your DataTable with a column for each user and task:

$table1 = array(
    'cols' => array(
        array('label' => 'day', 'type' => 'string'),
        array('label' => 'user1', 'type' => 'string'),
        array('label' => 'user2', 'type' => 'string'),
        array('label' => 'user3', 'type' => 'number'),
        array('label' => 'clean', 'type' => 'number'),
        array('label' => 'iron', 'type' => 'number'),
        array('label' => 'wash', 'type' => 'number')
    ),
    'rows' => array()
);
while($r = mysql_fetch_assoc($result)) {
    $table1['rows'][] = array('c' => array(
        array('v' => $r['day']),
        array('v' => $r['user1']),
        array('v' => $r['user2']),
        array('v' => $r['user3']),
        array('v' => $r['clean']),
        array('v' => $r['iron']),
        array('v' => $r['wash'])
    ));
}

这篇关于谷歌可视化API:使用交叉表格式的MySQL结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆