2016-07-01 60 views
2

這裏是一些代碼字面從https://developers.google.com/chart/interactive/docs/drawing_charts#chartwrapper
它應該顯示一個ColumnChart,但它不會產生任何輸出。相反,會發生JavaScript錯誤。
當加載文件獨立時,錯誤是loader.js:135 Uncaught TypeError: Cannot read property 'length' of undefinedloader.js:135
任何想法如何解決這個問題?無法使google.visualization.ChartWrapper示例工作

<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>

回答

1

在一個點,它會工作然而不加載任何packages

,最近的版本中,'corechart'包需要加載使用loader.js

時,請參閱以下,工作示例...

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(); 
 
}
<script src="https://www.gstatic.com/charts/loader.js"></script> 
 
<div id="vis_div" style="width: 600px; height: 400px;"></div>