我已经尝试了很长时间尝试将此阵列连接到谷歌图表并且没有运气,我真的很感激帮助找到我做错了什么。我有一个jsfiddle,你可以看到数组是好的,如果手动复制并粘贴到图表它是有效的,所以它只是一个代码问题,没有通过。
google.load("visualization", "1", {
packages: ["corechart"]
});
google.setOnLoadCallback(drawChart);
function drawChart() {
var dataset = $.ajax({
url: 'http://data.sparkfun.com/output/AJ2p4r8Owvt1MyV8q9MV.json?page=1',
type: 'get',
dataType: 'jsonp',
crossDomain: true,
success: function (jsonObj) {
var arr = ["[['Time', 'Humidity', 'Temp']"];
$.each(jsonObj, function (i, tObj) {
arr.push("['" + tObj.stationtime + "', " + tObj.humidity + ', ' + tObj.temp + ']');
});
arr.push("]")
// This for debugging
document.getElementById("demo").innerHTML = arr;
}
});
var data = google.visualization.arrayToDataTable([
dataset
]);
var options = {
title: 'Company Performance'
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
答案 0 :(得分:1)
有几件事:
查看以下内容:
$.ajax({
url: 'http://data.sparkfun.com/output/AJ2p4r8Owvt1MyV8q9MV.json?page=1',
type: 'get',
dataType: 'jsonp',
crossDomain: true,
success: function (jsonObj) {
var arr = [['Time', 'Humidity', 'Temp']];
$.each(jsonObj, function (i, tObj) {
arr.push([new Date(tObj.stationtime), parseFloat(tObj.humidity), parseFloat(tObj.temp)]);
});
document.getElementById("demo").innerHTML = arr;
var data = google.visualization.arrayToDataTable(arr);
var options = {
title: 'Company Performance'
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options); }
});