【问题标题】:draw google charts with JSON用 JSON 绘制谷歌图表
【发布时间】:2011-08-15 17:31:03
【问题描述】:

如果它是一个单独的 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);
}

【问题讨论】:

  • 您能否尽可能多地发布您的代码,以便我们有效地帮助您?

标签: javascript jquery ajax google-visualization


【解决方案1】:

new google.visualization.DataTable(json) 有效。

查看dataTable.toJSON() 的输出以了解要使用的正确结构。

所以,如果您的服务器上有一个 getjson.php 脚本可以返回格式正确的 json,您可以这样做:

$.getJSON('/getjson.php', function(json) {
    var dataTable = new google.visualization.DataTable(json);
});

【讨论】:

  • 谢谢,我还没有 php 脚本,但我可以尝试调用一个有效的 json 文件
猜你喜欢
  • 1970-01-01
  • 2018-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-26
  • 2010-10-14
相关资源
最近更新 更多