1

我正在尝试在我的 Drupal 7 模块(不是 google graph 模块)中包含 Google graph api。但是当我尝试调用该服务时,我得到:

  • “尝试在已清除的范围内运行 compile-and-go 脚本” - Firefox 中的错误。
  • “无法发送响应:每个 chrome.extension.onRequest 侦听器每个文档不能多次发送响应”-Chrome 中的错误。

代码如下:

(function ($) {

  Drupal.behaviors.exampleModule = {
    attach: function (context, settings) {
        // 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', 'Topping');
          data.addColumn('number', 'Slices');
          data.addRows([
            ['Mushrooms', 3],
            ['Onions', 1],
            ['Olives', 1],
            ['Zucchini', 1],
            ['Pepperoni', 2]
          ]);

          // Set chart options
          var options = {'title':'How Much Pizza I Ate Last Night',
                         'width':400,
                         'height':300};

          // Instantiate and draw our chart, passing in some options.
          var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
          chart.draw(data, options);
        }
        alert("test");
    }
  };

})(jQuery);

我该怎么做才能让它发挥作用?-谢谢

4

1 回答 1

0

我修好了它:

google.load('visualization', '1.0', {'packages':['corechart']});应该首先调用该行,就在(function ($) {. 所以解决方案是:

(function ($) {
    // Load the Visualization API and the piechart package.
    google.load('visualization', '1.0', {'packages':['corechart']});
  Drupal.behaviors.exampleModule = {
    attach: function (context, settings) {

        // 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', 'Topping');
          data.addColumn('number', 'Slices');
          data.addRows([
            ['Mushrooms', 3],
            ['Onions', 1],
            ['Olives', 1],
            ['Zucchini', 1],
            ['Pepperoni', 2]
          ]);

          // Set chart options
          var options = {'title':'How Much Pizza I Ate Last Night',
                         'width':400,
                         'height':300};

          // Instantiate and draw our chart, passing in some options.
          var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
          chart.draw(data, options);
        }
        alert("test");
    }
  };

})(jQuery);
于 2012-04-12T08:13:14.743 回答