当我运行下面的代码时,我得到了 TypeError: google.visualisation is undefined[了解更多]
数据从我的 .php 文件返回,当我执行 console.log 时我可以看到它。
我可能迷失在回调中,或者也许有人可以用其他方式解决?
我尝试在执行 ajax 之前加载视觉效果 here但同样的错误。我也查看了其他可能的答案,但我仍然无法让它发挥作用。
这是我的代码:
$(document).ready(function(){
console.log("hello world")
//alert("result")
$.ajax({
url:"data.php",
dataType : "JSON",
success : function(result) {
google.charts.load('current', { 'packages':['corechart'] });
google.charts.setOnLoadCallback(function() {
console.log(result);
drawChart(result);
});
}
});
function drawChart(result) {
var data = new google.visualisation.Datatable();
data.addColumn('string','Name');
data.addColumn('number','Quantity');
var dataArray=[];
$.each(result, function(i, obj) {
dataArray.push([ obj.name, parseInt(obj.quantity) ]);
});
data.addRows(dataArray);
var piechart_options = {
title : 'Pie Chart: How Much Products Sold By Last Night',
width : 400,
height : 300
}
var piechart = new google.visualisation.PieChart(document
.getElementById('piechart_div'));
piechart.draw(data, piechart_options)
var barchart_options = {
title : 'Bar Chart: How Much Products Sold By Last Night',
width : 400,
height : 300,
legend : 'none'
}
var barchart = new google.visualisation.BarChart(document
.getElementById('barchart_div'));
barchart.draw(data, barchart_options)
}
});
我从我的数据库查询中得到一个对象,所以我认为那部分是正确的,它是一个包含 6 个对象的数组:
0: Object { id: "1", name: "Product1", quantity: "2" }
1: Object { id: "2", name: "Product2", quantity: "3" }
2: Object { id: "3", name: "Product3", quantity: "4" }
3: Object { id: "4", name: "Product4", quantity: "2" }
4: Object { id: "5", name: "Product5", quantity: "6" }
5: Object { id: "6", name: "Product6", quantity: "11" }
值得我的php代码如下:
数据.php
<?php
require_once 'database.php';
$stmt = $conn->prepare('select * from product');
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_OBJ);
echo json_encode($results);
?>
数据库.php
<?php
$conn = new PDO('mysql:host=192.168.99.100;dbname=demo','root', 'root');
?>
备注:this版本相似但略有不同
最佳答案
你可以实际使用
google.charts.load
代替
$(文档).ready
它应该是 z 而不是 s
可视化
不是
可视化
像这样尝试......
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
$.ajax({
url:"data.php",
dataType : "JSON",
success : function(result) {
drawChart(result);
}
});
});
function drawChart(result) {
var data = new google.visualization.Datatable();
data.addColumn('string','Name');
data.addColumn('number','Quantity');
$.each(result, function(i, obj) {
data.addRow([ obj.name, parseInt(obj.quantity) ]);
});
var piechart_options = {
title : 'Pie Chart: How Much Products Sold By Last Night',
width : 400,
height : 300
}
var piechart = new google.visualization.PieChart(document
.getElementById('piechart_div'));
piechart.draw(data, piechart_options)
var barchart_options = {
title : 'Bar Chart: How Much Products Sold By Last Night',
width : 400,
height : 300,
legend : 'none'
}
var barchart = new google.visualization.BarChart(document
.getElementById('barchart_div'));
barchart.draw(data, barchart_options)
}
关于javascript - google.visualisation 未使用 ajax 定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49310414/