【问题标题】:Google charts are not displaying with new release新版本未显示 Google 图表
【发布时间】:2013-11-28 13:55:55
【问题描述】:

这是我在新版 Google Api 之前用于柱形图的代码,它运行良好。现在我的图表没有显示。我已经检查了新版本的更改,但我发现注意到相关。第一个问题出现在数组中的数据类型,浮点数据类型是不合法的,如果我用其他数据类型替换它,那么图表仍然不显示。如果我将浮点数更改为字符串,则会出现错误,即不会在 X 轴上使用字符串数据类型。我在 X 轴上绘制了时间,在 Y 轴上绘制了距离。如果有人知道如何根据新版本的 Google Api 更改代码,请分享您的代码或一些信息。

  $host=""; // Host name
  $username=""; // Mysql username
  $password=""; // Mysql password
  $db_name="mysql"; // Database name
  $tbl_name="gprs"; // Table name

 // Connect to server and select database.
 $con=mysql_connect("$host", "$username")or die("cannot connect");
 mysql_select_db("$db_name")or die("cannot select DB");

 $data = mysql_query("select SUM(Distance) as Distance, CAST( Time AS date) as Time from gprs where DeviceId=25 and Time between '2013-08-08' and '2013-09-31' group by CAST(Time AS date) order by CAST( Time AS date)")
 or die(mysql_error()); 

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

    array('label' => 'TIME', 'type' => 'string'),
array('label' => 'Distance', 'type' => 'float') 
 );
$rows = array();
while ($nt = mysql_fetch_assoc($data))

{

$temp = array();
$temp[] = array('v' => (string)$nt['TIME']);
$temp[] = array('v' =>(float) $nt['Distance']);
    $rows[]['c'] = $temp;
 }
    $table['rows'] = $rows;
    $jsonTable = json_encode($table);
 ?>

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
 <title>
 Google Visualization API Sample
 </title>
 <script type="text/javascript" src="//www.google.com/jsapi"></script>
 <script type="text/javascript">
 google.load('visualization', '1', {packages: ['corechart']});
 </script>
 <script type="text/javascript">
 function drawVisualization() {
 // Create and populate the data table.
 var data = new google.visualization.DataTable(<?=$jsonTable?>);

 // Create and draw the visualization.
 new google.visualization.ColumnChart(document.getElementById('visualization')).
    draw(data);
  }

  google.setOnLoadCallback(drawVisualization);  
 </script>
 </head>
 <body style="font-family: Arial;border: 0 none;">
 <div id="visualization" style="width: 600px; height: 300px;"></div>
 </body>
 </html>

【问题讨论】:

    标签: php mysql google-visualization


    【解决方案1】:

    我已经解决了上述问题。用这个替换上面的数组 数组('标签' => '时间','类型' => '字符串'), array('label' => 'fuel', 'type' => 'number')

    这个 $temp[] = array('v' => $nt['TIME']); $temp[] = array('v' => $nt['fuel']);

    不要使用 Packages: columnchart 现在它在新的 Api 版本中被 corechart 取代

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-24
      • 1970-01-01
      • 2014-03-29
      • 2023-03-13
      • 1970-01-01
      • 2019-11-28
      • 1970-01-01
      相关资源
      最近更新 更多