【问题标题】:google charts: google is not defined谷歌图表:谷歌未定义
【发布时间】:2016-10-23 08:28:34
【问题描述】:

我正在尝试在 firefox 上使用谷歌图表,问题是 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>

【问题讨论】:

    标签: firefox google-api google-visualization


    【解决方案1】:

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

    通过jsapi 加载程序仍然可用的 Google Charts 版本不再持续更新。请从现在开始使用新的 gstatic 加载程序 (loader.js)。

    下一步,从LoadGoogle 中删除“验证”if 语句,这不应该需要并且由于删除jsapi 而需要更改

    以下 sn-p 在 Firefox ESR 中适用于我,v: 45.3.0

    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();
    <script src="https://www.gstatic.com/charts/loader.js"></script>
    <div id="chart_div"></div>

    【讨论】:

    • thanx @whiteHat -
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-13
      • 2020-09-20
      • 2023-03-06
      • 2016-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多