我的表显示没有数据。 console.log(dt)是正确的。如果我打印console.log(dt.toJSON());它显示列定义但没有行。有什么问题?
google.load("visualization", "1.1", { packages: ["table"] });
google.setOnLoadCallback(drawChart);
function drawChart() {
var dt = new google.visualization.DataTable();
dt.addColumn('number', 'Time');
dt.addColumn('number', 'MAX');
dt.addColumn('number', 'AVE');
dt.addColumn('number', 'MIN');
$.getJSON("4711.json", function(data) {
dt.addRows(data.measuring.length);
$.each(data.measuring, function(idx, row) {
dt.setCell(idx, 0, row["time"]);
dt.setCell(idx, 1, row["MAX"]);
dt.setCell(idx, 2, row["AVE"]);
dt.setCell(idx, 3, row["MIN"]);
});
});
console.log(dt); // correct output !!
var table = new google.visualization.Table(document.getElementById('table_div'));
table.draw(dt, { showRowNumber: true, width: '100%', height: '100%' });
}
如果我将async设置为false,则可以正常工作。
function drawChart() {
var jsonData = $.ajax({
url: "4711.json",
dataType: "json",
async: false
}).responseText;
var data = JSON.parse(jsonData);
$.each(data.measuring, function (idx, r) {
...
});
});
}
答案 0 :(得分:0)
jQuery.getJSON()
默认为异步,因此您需要将draw
函数调用放入success
回调:
$.getJSON("https://gist.githubusercontent.com/vgrem/d7733bd200b6064873fe/raw/10794476e201f9b68bc9d3caa9fdaa43178f1ad4/4711.json", function(data) {
dt.addRows(data.measuring.length);
$.each(data.measuring, function(idx, row) {
dt.setCell(idx, 0, row["time"]);
dt.setCell(idx, 1, row["MAX"]);
dt.setCell(idx, 2, row["AVE"]);
dt.setCell(idx, 3, row["MIN"]);
});
var table = new google.visualization.Table(document.getElementById('table_div'));
table.draw(dt, { showRowNumber: true, width: '100%', height: '100%' });
});
示例强>
google.load("visualization", "1.1", { packages: ["table"] });
google.setOnLoadCallback(drawChart);
function drawChart() {
var dt = new google.visualization.DataTable();
dt.addColumn('number', 'Time');
dt.addColumn('number', 'MAX');
dt.addColumn('number', 'AVE');
dt.addColumn('number', 'MIN');
$.getJSON("https://gist.githubusercontent.com/vgrem/d7733bd200b6064873fe/raw/10794476e201f9b68bc9d3caa9fdaa43178f1ad4/4711.json", function(data) {
dt.addRows(data.measuring.length);
$.each(data.measuring, function(idx, row) {
dt.setCell(idx, 0, row["time"]);
dt.setCell(idx, 1, row["MAX"]);
dt.setCell(idx, 2, row["AVE"]);
dt.setCell(idx, 3, row["MIN"]);
});
var table = new google.visualization.Table(document.getElementById('table_div'));
table.draw(dt, { showRowNumber: true, width: '100%', height: '100%' });
});
}

<script src="http://www.google.com/jsapi"></script>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="chart.js"></script>
<style>
#chart_div {
width: 100% !important;
height: 400px;
}
</style>
<div id="table_div"></div>
&#13;
或使用Promise界面:
$.getJSON("https://gist.githubusercontent.com/vgrem/d7733bd200b6064873fe/raw/10794476e201f9b68bc9d3caa9fdaa43178f1ad4/4711.json")
.done(function(data){
dt.addRows(data.measuring.length);
$.each(data.measuring, function(idx, row) {
dt.setCell(idx, 0, row["time"]);
dt.setCell(idx, 1, row["MAX"]);
dt.setCell(idx, 2, row["AVE"]);
dt.setCell(idx, 3, row["MIN"]);
});
var table = new google.visualization.Table(document.getElementById('table_div'));
table.draw(dt, { showRowNumber: true, width: '100%', height: '100%' });
});