javascript - 谷歌图表错误 :Cannot read property 'length' of undefined; Debugging error in Google Charts

标签 javascript google-visualization

以下代码作为一个独立的页面本身运行良好。但是如果合并到我的 PHP Web 应用程序中,它会抛出错误。 google chart 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 中的错误:它显示“无法读取未定义的属性‘长度’”,容器中的背景为红色

Firefox 中的错误: 在 Firebug 控制台中抛出错误: b[c] 未定义 http://www.google.com/uds/api/visualization/1.0/97e6275dc5c50efe5944ddda289e59d2/format+en,default,corechart.I.js 785行

最佳答案

如果您使用的是 Prototype JS 框架,则此答案将适用。 (我在这里没有看到它,但它可能已被排除)。

Google Visualization API 上有针对此问题的工单 here .

一个潜在的问题是原型(prototype)或其他库的版本可能存在冲突。

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

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

[注意:我过去遇到过这个问题,这导致了解决方案。我第二次遇到这个问题,但原来的解决方案不适用于这个新问题。您的里程可能会有所不同。]

关于javascript - 谷歌图表错误 :Cannot read property 'length' of undefined; Debugging error in Google Charts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7759231/

相关文章:

javascript - 想要检查用户是否已经存在 MongoDB

javascript - 仅在离线时使用应用程序缓存

java - 具有异步数据 (RPC) 的 GWT Google 可视化

javascript - 将 JSON 加载到 Google 面积图中

JavaScript foreach 循环遍历十六进制值数组,使用 setTimeout 循环遍历背景颜色

javascript - 在 angularjs 中搜索和过滤多个模型

javascript - Extjs 4 中的组件选择器

javascript - 谷歌图表,如何在甘特图上添加自定义点?

javascript - 当数据表为全局时,Google 图表无法工作

google-visualization - 带有分组数据过滤器的 Google 仪表板 - 如何绘制分组数据图表