Javascript 只使用一个脚本标记加载google图表库,

Javascript 只使用一个脚本标记加载google图表库,,javascript,google-visualization,Javascript,Google Visualization,我想加载google图表库,并仅从一个脚本标记使用它。 下面是我的代码示例。当我用单独的脚本加载GoogleAPI时 一切正常。但如果是,我只想使用一个脚本 可能 <html> <head> <title>one script tag test</title> </head> <body> <!--Div that will hold the pie chart--> <

我想加载google图表库,并仅从一个脚本标记使用它。 下面是我的代码示例。当我用单独的脚本加载GoogleAPI时 一切正常。但如果是,我只想使用一个脚本 可能

<html>
  <head>
    <title>one script tag test</title>
  </head>

  <body>
    <!--Div that will hold the pie chart-->
    <div id="chart_div"></div>
    <script type="text/javascript">
      // Here I'm creating a script object and
      // load jsapi
      var scr = document.createElement("script");
      scr.setAttribute("src","https://www.google.com/jsapi");
      document.head.appendChild(scr);

      // here i wait until 
      // google object appears. 
      (function waitForGoogleLoad() {
            if(typeof google == 'undefined') {
                setTimeout(waitForGoogleLoad, 0);
            } else {
                processChart();
            }
      })();

      function processChart() {
          // 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);
          }
      }
    </script>
  </body>
</html>

单脚本标记测试
//这里我创建了一个脚本对象
//加载jsapi
var scr=document.createElement(“脚本”);
scr.setAttribute(“src”https://www.google.com/jsapi");
文件.标题.附件(scr);
//我在这里等到
//谷歌对象出现。
(函数waitForGoogleLoad(){
如果(谷歌的类型=='undefined'){
setTimeout(waitForGoogleLoad,0);
}否则{
processChart();
}
})();
函数processChart(){
//加载可视化API和piechart包。
load('visualization','1.0',{'packages':['corechart']});
//将回调设置为在加载Google Visualization API时运行。
setOnLoadCallback(drawChart);
//创建并填充数据表的回调,
//实例化饼图,传入数据并
//画它。
函数绘图图(){
//创建数据表。
var data=new google.visualization.DataTable();
data.addColumn('string','Topping');
data.addColumn('number','Slices');
data.addRows([
[‘蘑菇’,3],
[‘洋葱’,1],
[Olives',1],
[‘西葫芦’,1],
[意大利香肠,2]
]);
//设置图表选项
var options={'title':'我昨晚吃了多少比萨饼',
“宽度”:400,
‘高度’:300};
//实例化并绘制图表,传入一些选项。
var chart=new google.visualization.PieChart(document.getElementById('chart_div');
图表绘制(数据、选项);
}
}
如果您有任何想法和建议,我将不胜感激。
致以最良好的祝愿

处理此问题的最简单方法是在脚本标记中使用autoloader语法。这允许您避免等待脚本加载并从另一个函数内部调用google loader时出现任何问题:

var scr = document.createElement("script");
scr.setAttribute("src",'https://www.google.com/jsapi?autoload={"modules":[{"name":"visualization","version":"1","packages":["corechart"],"callback":"drawChart"}]}');
document.head.appendChild(scr);
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);
}
请参见此处的工作示例: