我正在尝试使用 .json 文件中的值使用谷歌图表绘制折线图。我已经尝试过了,但没有成功。
这是我的 .json 文件
[
{
"Year":2005,
"Sales":25000,
},
{
"Year":2006,
"Sales":25085,
},
{
"Year":2007,
"Sales":186230,
},
{
"Year":2008,
"Sales":35036,
},
{
"Year":2009,
"Sales":15900,
},
{
"Year":2010,
"Sales":35700,
}
]
代码
google.load("visualization", "1", { packages: ["corechart"] });
google.setOnLoadCallback(drawChart);
function loadData(fileName) {
// getting json from a remote file
// by returning the jqXHR object we can use the .done() function on it
// so the callback gets executed as soon as the request returns successfully
return $.getJSON( fileName + ".json");
}
function drawChart() {
var myFile = "Data";
var obj= loadData(myFile);
var data = google.visualization.arrayToDataTable(obj);
var options = {
title: 'Chart Demo'
};
var chart = new google.visualization.LineChart(
document.getElementById('chart_div'));
chart.draw(data, options);
}
错误
Error: Not an array
http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js
Line 2
如何解决这个问题?
最佳答案
尝试从文件中获取 json 数据。
var data = $.ajax({
url: "Data",
dataType: "json",
async: false,
}).responseText;
关于jquery - 谷歌图表 : Data from . json 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14952388/