mysql - 谷歌可视化 API : Format mysql result with cross tabulation

标签 mysql arrays google-visualization

我是 google apps 脚本的新手,我正在尝试使用 Google Visualization API 来可视化数据。 我已经能够显示关于混合 php 和 html 的好例子的表格。但数据库似乎没有正确的柱形图格式。

该表具有“天”“用户”“任务”“小时”列,然后数据通过 while 循环推送到下面的行中:

$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;

给我一​​个这样的表格:

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

我真正需要的是这样的表格:

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

我提前知道用户列表。

编辑:问题是“(服务器端)交叉制表”,正如我在this link中看到的那样。 我正在努力解决这个问题,并且仍然会很高兴获得每一个帮助,并且当然会标记答案。 谢谢!

最佳答案

要获得所需的格式,最简单的方法是在 MySQL 查询中转换数据。 MySQL 不支持数据透视表,但你可以像这样伪造它:

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

然后为每个用户和任务构建包含一列的数据表:

$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'])
    ));
}

关于mysql - 谷歌可视化 API : Format mysql result with cross tabulation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24743627/

相关文章:

javascript - google visualization api饼图百分比错误位置

mysql - Spring 启动 MySQL : LAZY Loading issue - No operations allowed after statement closed

mysql - 为特定的人选择最多 "popular"关注者。某人拥有的追随者越多,他们的 "popular"就越多

c - 如何用 C 中表示为数组的数字进行数学运算?

ios - 如何重新加载和重置 UIPickerView 中的行数 - Swift

javascript - 谷歌图表 : Object has no method 'getTime'

php - 使用 php 和 ajax 自动填写表单

mysql2 : error on reading inital communication packets

C++ 指针数组

html - 无法增加 Google Chart 高度