简体   繁体   English

Google图表上的多个实例

[英]Multiple instances on Google Charts

I am trying to get 2 instances of google charts but only the last one is appearing. 我正在尝试获取2个Google图表实例,但仅显示最后一个实例。

Here is the code: 这是代码:

// Load the Visualization API and the piechart package.
    google.load('visualization', '1', {'packages':['corechart']});

    // Set a callback to run when the Google Visualization API is loaded.
    google.setOnLoadCallback(drawChart);

    function drawChart() {
      var jsonData = $.ajax({
          url: "charts/getData.php",
          dataType:"json",
          async: false
          }).responseText;

      // Create our data table out of JSON data loaded from server.
      var data = new google.visualization.DataTable(jsonData);

      // Instantiate and draw our chart, passing in some options.
      var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
      chart.draw(data, {width: 400, height: 240});

    }


    //Chart 2

    google.load('visualization', '1', {'packages':['corechart']});

    // Set a callback to run when the Google Visualization API is loaded.
    google.setOnLoadCallback(drawChart);

    function drawChart() {
      var jsonData = $.ajax({
          url: "charts/getData.php",
          dataType:"json",
          async: false
          }).responseText;

      // Create our data table out of JSON data loaded from server.
      var data2 = new google.visualization.DataTable(jsonData);

      // Instantiate and draw our chart, passing in some options.
      var chart2 = new google.visualization.PieChart(document.getElementById('chart_div2'));
      chart2.draw(data2, {width: 400, height: 240});

    }

What I'm I doing wrong here? 我在这里做错了什么?

You need to call google.load('visualization', '1', {'packages':['corechart']}); 您需要调用google.load('visualization','1',{'packages':['corechart']}); only once. 只有一次。 I would suggest having an initialize function that would regroup all your drawchart() functions. 我建议您使用一个初始化函数来重新组合所有drawchart()函数。 See below. 见下文。 Apart from that your code looks fine and should work. 除此之外,您的代码看起来还不错,并且应该可以正常工作。

// Load the Visualization API and the piechart package.
google.load('visualization', '1', {'packages':['corechart']});

// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(initialize);

function initialize() { 
  drawChart();
  drawChart2();
}


function drawChart() {
  var jsonData = $.ajax({
      url: "charts/getData.php",
      dataType:"json",
      async: false
      }).responseText;

  // Create our data table out of JSON data loaded from server.
  var data = new google.visualization.DataTable(jsonData);

  // Instantiate and draw our chart, passing in some options.
  var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
  chart.draw(data, {width: 400, height: 240});

}



function drawChart2() {
  var jsonData = $.ajax({
      url: "charts/getData.php",
      dataType:"json",
      async: false
      }).responseText;

  // Create our data table out of JSON data loaded from server.
  var data2 = new google.visualization.DataTable(jsonData);

  // Instantiate and draw our chart, passing in some options.
  var chart2 = new google.visualization.PieChart(document.getElementById('chart_div2'));
  chart2.draw(data2, {width: 400, height: 240});

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM