谷歌图表错误:无法读取属性'长度'未定义的;谷歌图表中的调试错误

Google charts error:Cannot read property 'length' of undefined; Debugging error in Google Charts

本文关键字:错误 谷歌 调试 长度 未定义 属性 读取      更新时间:2023-09-26

下面的代码作为一个独立的页面运行良好。但如果合并到我的PHP web应用程序中,它会抛出错误。如何调试,如果错误发生在谷歌图表JS?任何建议都会有帮助的。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>
      Google Visualization API Sample
    </title>
  </head>
  <body style="font-family: Arial;border: 0 none;">
   <div id="myGraphLabel" onclick="drawColumnChart();">
        My Column Chart
    </div>
    <div id="myColumnChart" style="width: 600px; height: 400px;"></div>
  </body>
  <script type="text/javascript" src="http://www.google.com/jsapi"></script>
  <script type="text/javascript">
      google.load('visualization', '1', {packages: ['corechart']});
      function drawColumnChart() {
        // Create and populate the data table.
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Task');
        data.addColumn('number', 'Hours per Day');
        data.addRows(2);
        data.setValue(0, 0, 'Work');
        data.setValue(0, 1, 11);
        data.setValue(1, 0, 'Eat');
        data.setValue(1, 1, 2);
        // Create and draw the visualization.
        new google.visualization.PieChart(document.getElementById('myColumnChart')).
            draw(data, {title:"So, how was your day?"});
      }   
    </script>
</html>

Chrome中的错误:它显示"无法读取属性'length' of undefined",在容器中带有红色背景

Error in Firebug console:抛出错误:B [c]是未定义的http://www.google.com/uds/api/visualization/1.0/97e6275dc5c50efe5944ddda289e59d2/format+en,default,corechart.I.js第785行

这个答案适用于使用Prototype JS框架的情况。(我在这里没有看到,但它可能被排除在外)。

在Google可视化API上有一个关于这个问题的票证。

一个潜在的问题是可能有一个冲突的原型版本或其他一些库被包含。

或者,reduce函数可能需要重新定义:

<script type="text/javascript">
  Array.prototype.reduce = undefined;
</script>

[注:我过去有过这个问题,这导致了解决方案。我第二次遇到这个问题,但原来的解决方案不适用于这个新问题。

在我的web应用程序中,我包含了'prototype-1.6.0.2.js',这导致了这个错误!如果我排除了'prototype-1.6.0.2.js',一切都运行得很好!

我有一个类似的问题,谷歌图表工具和原型库冲突。我将Prototype从1.6.1版本升级到1.7.1,问题就解决了。