简体   繁体   English

Google Chart API实现显示空白页

[英]Google Chart API implementation displaying blank page

Attempting to implement a form to insert simple data into a database, then have a google chart display based on the database. 尝试实现将简单数据插入数据库的表单,然后基于该数据库显示google图表。 Page is currently blank. 页面当前为空白。 (I'll worry about the form when the chart displays properly). (当图表正确显示时,我将担心表单)。

I've checked around google for some answers, but the code is basically straight from their tutorial on google charts with a few modifications. 我已经在Google周围检查了一些答案,但是代码基本上是直接从他们在Google图表上的教程中进行了一些修改。

<?php
  require_once("apicreds.php");
  $db = new PDO($connectionString, $username , $password);

    if(!$db){echo "Connection Error";}
    if($db){echo "Connected!";}

  $query = "SELECT `country`, `sum(visits)` FROM `trips` GROUP BY 
`country`";
  $result = $db->query($query);
?>
<html>
  <head>

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



  function drawChart() {
    var data = new google.visualization.arrayToDataTable([
    ['country', 'visits'],
        <?php 
        while($row=$result->fetch_assoc())
        {
            echo "['".$row['country']."',".$row['sum(visits)']."],";
        }
        ?>
    ]);

    var options = {'title':'Visits To Countries',
                    'is3D':true};

    var chart = new 
google.visualization.PieChart(document.getElementById('chart_div'));
        chart.draw(data, options);
  }
    </script>
  </head>

  <body>
      <div id="chart_div"></div>
  </body>
</html>

Some of these tags might be messed up, so the full code is here. 其中一些标记可能会弄乱,因此完整代码在这里。

https://pastebin.com/31n5KVG2 https://pastebin.com/31n5KVG2

copy of the current table: 当前表的副本:

country visits 国家访问
Germany 1 德国1
Canada 2 加拿大2
UK 1 英国1
UK 1 英国1

Ideally, it should output in a 3d pie chart from the table, and update as more data is inserted. 理想情况下,它应从表中以3d饼图形式输出,并在插入更多数据时进行更新。 That part, I should be able to handle later, but I need help finding out why it won't display at the moment. 那部分,我以后应该可以处理,但是我需要帮助找出为什么现在不会显示。

need to name the sum column using the as keyword, 需要使用as关键字命名sum列,
the result will not have a valid key for --> sum(visits) 结果将没有-> sum(visits)的有效密钥

$query = "SELECT country, sum(visits) as total_visits FROM trips GROUP BY country";

then when loading the data, access the named column... 然后在加载数据时,访问命名列...

echo "['".$row['country']."',".$row['total_visits']."],";

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

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