无法使google.visualization.ChartWrapper示例工作

时间:2016-07-01 22:17:40

标签: javascript google-visualization

以下是https://developers.google.com/chart/interactive/docs/drawing_charts#chartwrapper字面上的一些代码 它应该显示ColumnChart,但它不会产生任何输出。相反,会发生JavaScript错误 独立加载文件时,loader.js:135 Uncaught TypeError: Cannot read property 'length' of undefined中的错误为loader.js:135 知道如何解决这个问题吗?



<html>
  <head>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">
      google.charts.load('current');   // Don't need to specify chart libraries!
      google.charts.setOnLoadCallback(drawVisualization);

      function drawVisualization() {
        var wrapper = new google.visualization.ChartWrapper({
          chartType: 'ColumnChart',
          dataTable: [['', 'Germany', 'USA', 'Brazil', 'Canada', 'France', 'RU'],
                      ['', 700, 300, 400, 500, 600, 800]],
          options: {'title': 'Countries'},
          containerId: 'vis_div'
        });
        wrapper.draw();
      }
    </script>
  </head>
  <body style="font-family: Arial;border: 0 none;">
    <div id="vis_div" style="width: 600px; height: 400px;"></div>
  </body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

在某一点上,它可以在不加载任何packages

的情况下工作

但是,对于最近的版本,使用'corechart'

时需要加载loader.js

见以下,工作示例......

&#13;
&#13;
google.charts.load('current', {packages: ['corechart']});
google.charts.setOnLoadCallback(drawVisualization);

function drawVisualization() {
  var wrapper = new google.visualization.ChartWrapper({
    chartType: 'ColumnChart',
    dataTable: [['', 'Germany', 'USA', 'Brazil', 'Canada', 'France', 'RU'],
                ['', 700, 300, 400, 500, 600, 800]],
    options: {'title': 'Countries'},
    containerId: 'vis_div'
  });
  wrapper.draw();
}
&#13;
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="vis_div" style="width: 600px; height: 400px;"></div>
&#13;
&#13;
&#13;