如果它是单独的JSON文件,如何检索和使用Google图表数据集?我尝试了jQuery getJSON,但无法正常工作。Google Viz应该使用JSON绘制条形图有本地的Google 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); }
new google.visualization.DataTable(json) 作品。
new google.visualization.DataTable(json)
查找的输出dataTable.toJSON()以使用正确的结构。
dataTable.toJSON()
因此,如果服务器上有一个getjson.php脚本返回正确格式的json,则可以执行以下操作:
$.getJSON('/getjson.php', function(json) { var dataTable = new google.visualization.DataTable(json); });