【问题标题】:My page does not load after adding google chart添加谷歌图表后我的页面没有加载
【发布时间】:2016-02-05 21:54:24
【问题描述】:

我想在 ajax 调用后在同一页面上显示数据表 + 谷歌图表。我从传递给谷歌图表的 ajax 调用中获得 numberOfStudents 和 numberOfFamiliesUsingApp。当我尝试下面的代码页时,只是没有出现。在开发人员工具中,我一直看到“缓冲区使用情况”,但没有显示任何内容。

<script src='assets/js/jquery.dataTables.min.js'></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
    var classId = "<s:property value="classId"/>" ;
    var schoolId = "<s:property value="schoolId"/>" ;
    var tableData = [];
    var numberOfStudents = 0;
    var numberOfFamiliesUsingApp = 0;

    $(document).ready(function() {
         $.getJSON( "json1/getJSONParentResult.json?schoolId="+schoolId+"&classId="+classId, function( data ) {
        tableData = data.data;
        numberOfStudents = data.numberOfStudents;
        numberOfFamiliesUsingApp = data.numberOfFamiliesUsingApp;

        //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);

       function drawChart() {

          // Create the data table.
         var data = new google.visualization.DataTable();
         data.addColumn('string', 'Total');
         data.addColumn('number', 'Usage');
         //replace the values with javascript variables which are set after ajax call  
         data.addRows([
            ['Total Students', numberOfStudents],
            ['Total Usage', numberOfFamiliesUsingApp]
         ]);

         // Set chart options
         var options = {'title':'School Rush Activities',
                     'width':500,
                     'height':400};

         // Instantiate and draw our chart, passing in some options.
         var chart = new       google.visualization.PieChart(document.getElementById('chart_div'));
         chart.draw(data, options);
       }            

       $('#example').dataTable( {
         "bPaginate": true,
         "bProcessing": true,
         "bServerSide": false,
         "iDisplayLength": 100,
         "aaSorting": [[1, 'asc']],
         "aaData": tableData
       } );
    });
} );
</script>
<div id="chart_div"></div>
<table id="example" class="display" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th>Last Login</th>
            <th>Student LastName</th>
            <th>Student FirstName</th>
            <th>Account Username</th>
        </tr>
    </thead>
</table>

如果我评论谷歌图表代码,那么数据表会正确显示。无法理解这里出了什么问题。请帮忙。

【问题讨论】:

  • 您能提供给我们您的 JSON 吗?
  • {"data":[[null,"Datey","Kabir","2220000000"],[null,"Datey","Zara","2220000000"],[null," Rao","Aditya","4444444444"]],"numberOfStudents":10,"numberOfFamiliesUsingApp":8}

标签: javascript ajax google-api google-visualization


【解决方案1】:

您的脚本有语法错误,因为您没有正确地转义字符串。这些行是造成这种情况的原因:

var classId = "<s:property value="classId"/>";
var schoolId = "<s:property value="schoolId"/>";

您可以使用反斜杠转义",或使用'。像这样:

var classId = "<s:property value=\"classId\"/>";
var schoolId = '<s:property value="schoolId" />';

(请注意代码的着色如何使这一点一目了然。如果您还没有,使用good text editor 将帮助您避免将来出现类似问题)

【讨论】:

  • 还是同样的问题。当我将谷歌图表放在单独的脚本标签中时,它唯一一次起作用。但是有时我确实得到了正确的结果,有时我没有得到结果。我必须刷新2-3次。
  • @user2869612 您在开发控制台中看到任何错误吗? (Ctrl+Shift+J)
  • 控制台上没有错误,除了“缓冲区使用”。如果我将谷歌图表移动到单独的标签中,图表会出现(可能在 2-3 次刷新后)
  • 我切换到了 Chart.js 而不是谷歌图表,现在它可以正确获取我的动态数据并显示正确的结果。我知道这不是解决方案,而是替代方案。暂时这对我有用。
  • @user2869612 等等,我认为问题在于 dataTables 无法加载...使用谷歌地图代码,这仍然发生吗?
猜你喜欢
  • 1970-01-01
  • 2014-06-14
  • 2013-05-09
  • 2016-03-31
  • 1970-01-01
  • 1970-01-01
  • 2014-11-01
  • 1970-01-01
  • 2019-07-27
相关资源
最近更新 更多