谷歌图表:谷歌没有定义

时间:2016-10-23 08:28:34

标签: firefox google-api google-visualization

我正在尝试在firefox上使用google图表,问题是google api未在firefox上成功加载ReferenceError: google is not defined

这是我的代码:

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script>
    function LoadGoogle(){
        if(typeof google != 'undefined' && google && google.load){
            google.charts.load('current', {'packages':['corechart']});

              // Set a callback to run when the Google Visualization API is loaded.
              google.charts.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.BarChart(document.getElementById('chart_div'));
            chart.draw(data, options);
          }
        }
        else
        {
            console.log("try");
            setTimeout(LoadGoogle, 30);
        }
    }

    LoadGoogle();
</script>

2 个答案:

答案 0 :(得分:0)

首先,删除对jsapi的引用,如release notes中所述......

  

通过jsapi加载程序保留的Google图表版本不再一致更新。从现在开始,请使用新的gstatic加载程序(loader.js)。

下一步,删除&#34;验证&#34;来自if的{​​{1}}声明,这不应该是必需的,并且由于删除LoadGoogle而需要更改

以下代码段适用于Firefox ESR,v:45.3.0

&#13;
&#13;
jsapi
&#13;
function LoadGoogle(){
  google.charts.load('current', {
    callback: drawChart,
    packages:['corechart']
  });

  function drawChart() {
    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]
    ]);

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

    var chart = new google.visualization.BarChart(document.getElementById('chart_div'));
    chart.draw(data, options);
  }
}

LoadGoogle();
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您必须复制URL https://www.gstatic.com/charts/loader.js并将URL放置在浏览器中,单击右键并另存为file.js将这两个文件保存在您的项目中,只需引用HTML中的文件