Google图表无法加载

时间:2015-01-20 19:23:03

标签: javascript jquery ajax charts google-visualization

我在尝试将Google图表添加到项目时遇到问题。页面不会加载或加载空白页面。尝试加载后,它会正确显示页面几秒钟,然后页面变为空白并卡住。 这是我的代码:

<script type="text/javascript">

  // Load the Visualization API and the piechart package.
  google.load('visualization', '1.0', {'packages':['corechart','table']});

  // 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([
    ['TEST 1', 3],
    ['TEST 2', 1],
    ['TEST 3', 1], 
    ['TEST 4', 1],
    ['TEST 5', 2]
  ]);

  // Set chart options
  var options = {'title':'ACTIONS',
                 '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>

这是我在应用视图中添加的div

<div id="chart_div" style="width:400; height:300"></div>

1 个答案:

答案 0 :(得分:0)

你在加载JQuery和JQueryUI吗?

<script type="text/javascript">
  google.load("search", "1");
  google.load("jquery", "1.4.2");
  google.load("jqueryui", "1.7.2");
</script>

以下是一些示例代码(工作)

<html>
  <head>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {

        var data = google.visualization.arrayToDataTable([
          ['Task', 'Hours per Day'],
          ['Work',     11],
          ['Eat',      2],
          ['Commute',  2],
          ['Watch TV', 2],
          ['Sleep',    7]
        ]);

        var options = {
          title: 'My Daily Activities'
        };

        var chart = new google.visualization.PieChart(document.getElementById('piechart'));

        chart.draw(data, options);
      }
    </script>
  </head>
  <body>
    <div id="piechart" style="width: 900px; height: 500px;"></div>
  </body>
</html>

您是如何尝试加载数据的?你能给我们更多的信息吗?