简体   繁体   English

来自 MySQL 的谷歌图表

[英]Google charts from MySQL

I have tried for several hours to get MySQL data in a Google charts but I can't wrap my head around how to make a working page from the examples I've come across on the internet.我已经尝试了几个小时在 Google 图表中获取 MySQL 数据,但我无法从互联网上遇到的示例中了解如何制作工作页面。

To start fresh I took a example from Google charts and manually filled it with data.为了重新开始,我从谷歌图表中拿了一个例子,并用数据手动填充它。 This gives me the graph I want to have.这给了我想要的图表。 the Google charts graph is generated by a simple HTML PAGE (JUST THE VARIABLE PART:谷歌图表是由一个简单的 HTML 页面生成的(只是可变部分:

....
      function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['date',          'Baxico'    ,   'Degriek'   ,    'Foldman'  ,   'Madmeijs'  ,   'Marcello'  ,   'Meijster'  ,   'Pokermom'],
          ['110415180035',  38,             1,              16,             10,             6,              4,              25        ],
          ['110415190222',  38,             16,             6,              4,              1,              25,             10        ],
          ['110415200747',   6,             38,             25,             10,             1,              16,             4         ],
          ['110415211933',  10,             38,             6,              25,             4,              16,             1         ],
          ['110415222033',  16,             1,              10,             6,              38,             25,             4         ],
          ['110415232833',  38,             4,              1,              25,             10,             6,              16        ]
        ]);

        

I made the same data output in MySQL:我在 MySQL 中做了相同的数据输出:

select tournamentid
,(select points  from pokermax_scores as t2 where playerid = 'Baxico' and t1.tournamentid = t2.tournamentid) as Baxico
,(select points  from pokermax_scores as t2 where playerid = 'Degriek' and t1.tournamentid = t2.tournamentid) as Degriek
,(select points  from pokermax_scores as t2 where playerid = 'Foldman' and t1.tournamentid = t2.tournamentid) as Foldman
,(select points  from pokermax_scores as t2 where playerid = 'Madmeijs' and t1.tournamentid = t2.tournamentid) as Madmeijs
,(select points  from pokermax_scores as t2 where playerid = 'Marcello' and t1.tournamentid = t2.tournamentid) as Marcello
,(select points  from pokermax_scores as t2 where playerid = 'Meijster' and t1.tournamentid = t2.tournamentid) as Meijster
,(select points  from pokermax_scores as t2 where playerid = 'Pokermom' and t1.tournamentid = t2.tournamentid) as Pokermom
from pokermax_scores as t1
group by tournamentid

which results in same data: http://i60.tinypic.com/6nqp76.png结果相同的数据: http : //i60.tinypic.com/6nqp76.png

But I can't get the data loaded as shown in this example: http://datamakessense.com/google-charts-api-from-your-sql-database-to-a-live-chart-with-no-coding-skills/但我无法如本示例所示加载数据: http : //datamakessense.com/google-charts-api-from-your-sql-database-to-a-live-chart-with-no-coding -技能/

I can make the database connection, and paste in the SQL, but I'm unclear how to set the script so it takes the data from the SQL.我可以建立数据库连接并粘贴 SQL,但我不清楚如何设置脚本以便从 SQL 获取数据。

Hey buddy i had the same problem, what you are trying to do is to take the tournamentid and draw for every data column a line, bar, column or whatever this problem called "pivot table" try to search over the internet, there is two different solutions for this :嘿,伙计,我遇到了同样的问题,您要做的是获取锦标赛 ID 并为每个数据列绘制一条线、条、列或任何称为“数据透视表”的问题尝试在互联网上搜索,有两个对此的不同解决方案:

  1. You can solve this in google charts api using javascript.您可以使用 javascript 在 google charts api 中解决此问题。
  2. You can solve this by nested loops(for loops) + using two selects.您可以通过嵌套循环(for 循环)+ 使用两个选择来解决此问题。

Check the code below: using the second solution.检查下面的代码:使用第二种解决方案。

<?php
    /* Connect  to database */
    $mysqli = new mysqli("localhost","root","123","charts");
    if(mysqli_connect_errno()){
      trigger_error('Connection failed: '.$mysqli->error);
    }

    /* Build the query */
    $query = "SELECT a.item_code,a.total,a.date FROM chart_values a, (SELECT DISTINCT item_code FROM chart_values GROUP BY item_code,date) b WHERE a.item_code = b.item_code";

    /* Loop through the results and build a JSON array for the data table */
    $result = $mysqli->query($query);

    $table = array();
    while ($row = $result->fetch_array(MYSQLI_ASSOC)) {

                if (!isset($table[$row['item_code']])) {
                    $table[$row['item_code']] = array(
                          'total' => array(),
                          'date' => array()
              );
           }

            $table[$row['item_code']]['total'][] = $row['total'];
            $table[$row['item_code']]['date'][] = $row['date'];
    }       
    echo var_dump($table);
    $datas = json_encode($table);

$datas = json_decode($datas,true);
// echo '<pre>';
// print_r($datas);

        $googleData = array('Date');
        foreach($datas as $key => $data){
            $googleData[] = $key;
        }

        for($i=0;$i<count($datas);$i++){
            foreach($datas as $key => $data){
                if(!in_array($data['date'][$i], $googleData)){
                    $googleData[] = $data['date'][$i];
                }
                $googleData[] = $data['total'][$i];
            }
        }

        $googleData = json_encode(array_chunk($googleData,count($datas)+1), JSON_NUMERIC_CHECK);
        // print_r($googleData);
?>



<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<body>
    <div style="width:90%; height:500px;" id="columnchart_material" style="width: 1000px; height: 500px;"></div>
</body>


<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);

function drawChart() {

    var data = new google.visualization.arrayToDataTable(<?=$googleData?>);

    var options = {
        chart: {
            title: 'Company Performance',
            subtitle: 'Sales, Expenses, and Profit: 2014-2017',
        }
    };

    var chart = new google.visualization.ColumnChart(document.getElementById('columnchart_material'));

    chart.draw(data, options);
}
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM