php - 如何将 MySQL 表中的数据放入 Google Chart API?

标签 php mysql sql google-visualization

我正在努力弄清楚如何根据我拥有的数据制作图表,这是一个示例。

带有虚拟数据的图表应如下所示:

function drawChart() {
    var data = google.visualization.arrayToDataTable([
    ['Month', 'Margarita Murphy', 'Lora Gonzales', 'Mario Moran', 'Wefico Local Faire', 'Zegko Collection', 'Saxux Program for Youth', 'Test New location venue'],
    ['4/12', 9, 74, 10, 8, 93, 33, 90], 
    ['5/12', 10, 168, 0, 10, 198, 108, 154], 
    ['6/12', 9, 174, 12, 12, 165, 96, 261], 
    ['7/12', 12, 288, 8, 36, 180, 264, 140], 
    ['8/12', 40, 275, 15, 30, 275, 395, 170], 
    ['9/12', 54, 534, 30, 48, 240, 246, 552], 
    ['10/12', 28, 518, 63, 28, 182, 672, 98], 
    ['11/12', 56, 520, 8, 64, 424, 568, 704], 
    ['12/12', 45, 675, 9, 63, 864, 567, 756], 
    ['1/13', 90, 570, 40, 70, 350, 510, 150], 
    ['2/13', 55, 946, 110, 55, 253, 429, 88], 
    ['3/13', 96, 684, 12, 96, 528, 1140, 468], 
    ['4/13', 52, 832, 104, 130, 1261, 1235, 663], 
    ['5/13', 28, 756, 70, 70, 1050, 910, 728], 
    ['6/13', 105, 930, 15, 60, 1440, 660, 690], 
    ['7/13', 144, 1600, 96, 64, 1312, 1488, 1120], 
]);

正如您所看到的,它有一个项目列表,以及过去每月的浏览次数。

我遇到的问题是,当我从 MySQL 获取数据时,我想循环浏览 View ,这些 View 将在列中垂直向下,而不是交叉。我该如何让它像这样水平移动:

['4/12', item1.views, item2.view]
['5/12', item1.views, item2.view]

我对此感到非常困惑......

示例数据

-------------------
|date|views|ref_id|
|----|-----|------|
|4/12|123  |2     |
|5/12|526  |7     |
|6/12|2    |1     |
|7/12|46   |3     |
-------------------

编辑:

首先将日期设置为变量,然后循环所有内容并将数据添加到正确的数据中?

$month4_12 = "['4/12', ";
$month5_12 = "['5/12', ";

foreach($views_data as $data){
    ${"month_$data->date"} .= $data->views . ', ';
}

$month4_12 .= "],";
$month5_12 .= "],";

编辑2:

所以这就是我现在所拥有的,但是它有一些问题,如果 View 表不包含记录,它就不算数,因为它只计算在数据库中找到的内容...显然不是不起作用,因为与标题相比,它没有正确的列数。

// Get views for chart
$views_data = $this->content_model->get_chart_view_data();

// First make the months
$month = 1;
while($month <= 16){
    $month_text = date('d/m/y');
    $month_text = strtotime($month_text . ' -'.$month.' months');
    $month_text_display = date('n/y', $month_text);
    $month_text_variable = str_replace('/', '_', $month_text_display);
    ${"month_$month_text_variable"} = "['".$month_text_display."', ";

    // Now add the data
    foreach($views_data as $row){
        ${"month_$month_text_variable"} .= $row->views . ', ';
    }
    ${"month_$month_text_variable"} = rtrim(${"month_$month_text_variable"}, ", ");

    // Finish the lines
    ${"month_$month_text_variable"} .= "],\n";

    $month++;
}

// Now join the lot!
$month = 1;
$chart_data = '';
while($month <= 16){
    $month_text = date('d/m/y');
    $month_text = strtotime($month_text . ' -'.$month.' months');
    $month_text_display = date('n/y', $month_text);
    $month_text_variable = str_replace('/', '_', $month_text_display);
    $chart_data .= ${"month_$month_text_variable"};
    $month++;
}

$data['chart_data'] = rtrim($chart_data, ",\n");

echo $data['chart_data'];

这给出了输出:

function drawChart() {
        var data = google.visualization.arrayToDataTable([
        ['Month', 'Margarita Murphy', 'Lora Gonzales', 'Mario Moran', 'Wefico Local Faire', 'Zegko Collection', 'Saxux Program for Youth', 'Test New location venue'],
        ['7/13', 2, 1, 1],
        ['6/13', 2, 1, 1],
        ['5/13', 2, 1, 1],
        ['4/13', 2, 1, 1],
        ['3/13', 2, 1, 1],
        ['2/13', 2, 1, 1],
        ['1/13', 2, 1, 1],
        ['12/12', 2, 1, 1],
        ['11/12', 2, 1, 1],
        ['10/12', 2, 1, 1],
        ['9/12', 2, 1, 1],
        ['8/12', 2, 1, 1],
        ['7/12', 2, 1, 1],
        ['6/12', 2, 1, 1],
        ['5/12', 2, 1, 1],
        ['4/12', 2, 1, 1]
]);

编辑3

这是浏览量数据在数据库中的存储方式,可以看到没有浏览量的一天根本就没有记录

enter image description here

最佳答案

您需要在 SQL 查询中转换数据。由于 MySQL 本身不支持枢轴,因此您必须进行一些作弊。最终输出中的每个旋转列都将采用以下形式:

IF(ref_id = <this column's reference id>, views, 0) AS <column name>

然后按日期列进行分组,如下所示:

SELECT
    data,
    IF(ref_id = 327, views, 0) AS column_327,
    IF(ref_id = 329, views, 0) AS column_329,
    // etc...
FROM <table name>
WHERE <conditions>
GROUP BY date

然后您可以迭代输出来构建 DataTable 对象。

如果您事先不知道所有 ref_id 值(或者有很多),那么您可以查询以获取 ref_id 列表并以编程方式构建查询:

SELECT DISTINCT ref_id FROM <table name>

关于php - 如何将 MySQL 表中的数据放入 Google Chart API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18122344/

相关文章:

php - 如何做到这一点..向后?

php - 自定义搜索需要根据选择的名字显示名字搜索结果

MySQL,如何在 SELECT 查询中引用子查询字段

mysql - 如何编写基于列值组合行的自联接?

php - 如果记录存在则更新或插入 php/mysql

php - WordPress 创建新的帖子状态?

MySQL 错误 1045 (28000) : Access denied for user 'bill' @'localhost' (using password: YES)

java - Centos mysql 连接器 java - JDBC 驱动程序

mysql - SQL Server 自动递增下一个 INSERT 查询

mysql - 根据汇总结果选择