如果它是一个单独的JSON文件,我如何检索和使用谷歌图表的数据集?我尝试过jQuery getJSON但是无法使用它.Google Viz应该使用JSON来绘制条形图
是否有原生谷歌API方式?或者我可以找到使用jQuery的方法以及如何使用?
谢谢
// Load the Visualization API and the piechart package.
google.load('visualization', '1.0', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.setonLoadCallback(drawChart);
// Callback that creates and populates a data table,
// instantiates the pie chart, passes in the data and
// draws it.
function drawChart() {
// Create the data table.
var data = new google.visualization.DataTable();
data.addColumn('string', 'Products');
data.addColumn('number', 'Automated');
data.addRows([
['Product 1', 85],
['Product 2', 75],
['Product 3', 90],
['Product 4', 40],
['Product 5', 40]
]);
// Set chart options
var pie_options = {'title':'How Much Automated our Products are?',
'width':520,'height':300
};
var bar_options ={'width': 620, 'height': 300,
'title': 'Products',
'hAxis': {'title': '% Automated', 'titleTextStyle': {'color': 'red', 'fontSize': 16}}
}
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.PieChart(document.getElementById('piechart_div'));
chart.draw(data, pie_options);
var chart = new google.visualization.ColumnChart(document.getElementById('barchart_div'));
chart.draw(data, bar_options);
}
解决方法:
新的google.visualization.DataTable(json)有效.
查看dataTable.toJSON()
的输出以获取正确的结构.
因此,如果您的服务器上有一个getjson.PHP脚本,它返回正确格式化的json,那么您可以这样做:
$.getJSON('/getjson.PHP', function(json) {
var dataTable = new google.visualization.DataTable(json);
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。