正如标题所说,我在尝试加载一个简单的柱形图时遇到了这个错误:"未捕获的TypeError:无法读取属性' DataTable'未定义"
这只发生在谷歌浏览器中,而Firefox显示它没有问题。
我试图找到解决方案,但我无法弄清楚问题。顺便说一下google.load
,这是完整的jQuery脚本:
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
current = 0;
var jsondata1 =$.ajax({url: "index.php?action=stats&data=post",dataType:"json",async: false}).responseText;
var jsondata2 =$.ajax({url: "index.php?action=stats&data=comm",dataType:"json",async: false}).responseText;
var jsondata3 =$.ajax({url: "index.php?action=stats&data=like",dataType:"json",async: false}).responseText;
var jsondata4 =$.ajax({url: "index.php?action=stats&data=disl",dataType:"json",async: false}).responseText;
var data = [];
data[0] = new google.visualization.DataTable(jsondata1);
data[1] = new google.visualization.DataTable(jsondata2);
data[2] = new google.visualization.DataTable(jsondata3);
data[3] = new google.visualization.DataTable(jsondata4);
var title = ["post","comments","likes","dislikes"];
var options = {
width:600,
height:500,
vAxis: {title: "Number"},
hAxis: {title: "Users"},
legend: {position: 'none'},
animation:{
duration: 1000,
easing: 'out',
},
};
var chart = new google.visualization.ColumnChart(document.getElementById('graphimg'));
function drawChart() {
google.visualization.events.addListener(chart, 'ready',function() {});
options['title'] = 'Max number of '+title[current];
chart.draw(data[current], options);
}
function next(){
current++;
if(current === 4)
current = 0;
drawChart();
}
function prev(){
current--;
if(current < 0)
current = 3;
drawChart();
}
数据是从ajax调用获得的,请求是由类似的php函数处理的,这里有一个(不要介意不推荐的MySQL调用):
function getMaxLikes(){
$conn = dbConnect();
$sql = "SELECT username, count( user ) AS numlikes
FROM users, likes
WHERE user = username and value=1
GROUP BY username
ORDER BY numlikes DESC
LIMIT 5";
$res = mysql_query($sql) or die("Error:".mysql_error());
$json = array();
$json['cols'] = array(
array('label' => 'User', 'type' => 'string'),
array('label' => 'Likes', 'type' => 'number'),
array('type' => 'string', 'role' => 'style')
);
$rows = array();
$i = 0;
while($r = mysql_fetch_assoc($res)) {
$temp = array();
$temp[] = array('v' => (string) $r['username']);
$temp[] = array('v' => (int) $r['numlikes']);
$temp[] = array('v' => (string) $this->color[$i]);
$rows[] = array('c' => $temp);
$i++;
}
$json['rows'] = $rows;
return json_encode($json);
}
json格式似乎对我而言,但我可能会弄错。 什么可能导致问题?提前谢谢!