用JSON和PHP显示谷歌图表
Display google chart with JSON and PHP
我试图显示来自数据库的JSON对象查询,我收到的JSON对象似乎是正确的:
{
"cols": [
{
"id": "A",
"label": "Date",
"type": "string"
},
{
"id": "B",
"label": "User",
"type": "string"
},
{
"id": "C",
"label": "Cement Brand",
"type": "string"
},
{
"id": "D",
"label": "Volume",
"type": "string"
},
{
"id": "E",
"label": "Type",
"type": "string"
}
],
"rows": [
{
"c": [
{
"v": "08-06-2013"
},
{
"v": "razee.hj@gmail.com"
},
{
"v": "Muthana"
},
{
"v": "27"
},
{
"v": "Local Plant"
}
]
}
]
}
但我查询数据的php文件似乎有问题,但我找不到错误。这是dataTableViewDaily.php文件
<?php
$selectQuery = 'SELECT * FROM competitive_daily_volume';
$table = array();
$table['cols'] = array(
array("id"=>"A","label"=>"Date","type"=>"string"),
array("id"=>"B","label"=>"User","type"=>"string"),
array("id"=>"C","label"=>"Cement Brand","type"=>"string"),
array("id"=>"D","label"=>"Volume","type"=>"string"),
array("id"=>"E","label"=>"Type","type"=>"string")
);
$rows = array();
$result = mysql_query($selectQuery);
while($row = mysql_fetch_assoc($result)){
$temp = array();
$temp[] = array("v"=> $row['Date']);
$temp[] = array("v"=> $row['UserId']);
$temp[] = array("v"=> $row['CementBrand']);
$temp[] = array("v"=> $row['VolumeBGLP']);
$temp[] = array("v"=> $row['Type']);
$rows[] = array("c"=> $temp);
}
$table['rows'] = $rows;
$jsonObj = json_encode($table);
header('Cache-Control: no-cache, must-revalidate');
header('Content-type: application/json');
echo $jsonObj;
?>
这是javascript文件:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
google.load('visualization', '1', {packages: ['table']});
</script>
<script type="text/javascript">
function drawVisualization() {
// var jsonData = null;
var jsonData = $.ajax({
url: "php/DailyVolume/dataTableViewDaily.php", // make this url point to the data file
dataType: "json",
async: false
}).responseText;
var data = new google.visualization.DataTable(jsonData);
// Create and draw the visualization.
visualization = new google.visualization.Table(document.getElementById('table'));
visualization.draw(data, null);
}
google.setOnLoadCallback(drawVisualization);
</script>
</head>
<body style="font-family: Arial;border: 0 none;">
<div id="table"></div>
</body>
</html>
我已经在这方面工作了很长一段时间,我找不到我缺少的东西,如果有任何意见,我将不胜感激。
问题是:
var jsonData = $.ajax({
url: "php/DailyVolume/dataTableViewDaily.php", // make this url point to the data file
dataType: "json",
async: false
}).responseText;
更改为:
var jsonData;
$.ajax({
url: "php/DailyVolume/dataTableViewDaily.php", // make this url point to the data file
dataType: "json",
async: false,
success:function(data){
//get data in your callback
jsonData = data;
}
});
或者使用推荐的方法使用promise
并避免async: false
:
$.ajax({
url: "php/DailyVolume/dataTableViewDaily.php", // make this url point to the data file
dataType: "json",
}).done(function(jsonData){
var data = new google.visualization.DataTable(jsonData);
// Create and draw the visualization.
visualization = new google.visualization.Table(document.getElementById('table'));
visualization.draw(data, null);
});
需要更改此
var jsonData = $.ajax({
url: "php/DailyVolume/dataTableViewDaily.php", // make this url point to the data file
dataType: "json",
async: false
}).responseText;
var data = new google.visualization.DataTable(jsonData);
到这个
$.ajax({
url: "php/DailyVolume/dataTableViewDaily.php", // make this url point to the data file
dataType: "json",
async: false,
success : function(response){
var data = new google.visualization.DataTable(response);
// other code
}
});
相关文章:
- 不显示带有本地json文件数据的谷歌地图脚本
- 谷歌地图不是以HTML显示,而是在JS Fiddle上工作
- 谷歌地图标记不会显示
- 无法在JS中显示谷歌地图
- 需要帮助谷歌地图方向面板在FancyBox中显示
- 使用谷歌应用程序脚本从工作表中获取值并将其显示在文本框中
- 谷歌地图JS API+JSON-多个标记没有显示
- 如何在谷歌字体加载时显示加载图像
- 谷歌水印未显示在自定义搜索框中
- 谷歌地图v3标记没有显示ie8和9
- 标记不会显示在谷歌地图上
- 谷歌地图停止显示点v3
- 谷歌图表显示所有标签
- PHP谷歌地图-位置红色气球没有显示
- SIMPLE Javascript代码,用于显示谷歌电子表格中单个字段的数据
- 谷歌地图地点API地图未显示
- 当我使用高度百分比时,谷歌地图javascript api不会显示
- 谷歌图表在运行应用程序时只能显示一次
- 使用JSON在谷歌地图上显示标记
- 使用谷歌地图API和phonegap显示当前位置+方向