最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin3PV0评论

The following code works fine as a independant page as itself. But if incorporated into my PHP web app, it throws error. How to debug if the error happens in the google chart JS? Any suggestions will be helpful

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" ".dtd">
<html xmlns="">
  <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=""></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>

Error in Chrome: It displays "Cannot read property 'length' of undefined", With a red background in the container

Error in Firefox: Throws error in Firebug console as: b[c] is undefined .0/97e6275dc5c50efe5944ddda289e59d2/format+en,default,corechart.I.js Line 785

The following code works fine as a independant page as itself. But if incorporated into my PHP web app, it throws error. How to debug if the error happens in the google chart JS? Any suggestions will be helpful

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3/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./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>

Error in Chrome: It displays "Cannot read property 'length' of undefined", With a red background in the container

Error in Firefox: Throws error in Firebug console as: b[c] is undefined http://www.google./uds/api/visualization/1.0/97e6275dc5c50efe5944ddda289e59d2/format+en,default,corechart.I.js Line 785

Share Improve this question edited Jan 27, 2017 at 16:45 Nakilon 35.1k16 gold badges111 silver badges148 bronze badges asked Oct 13, 2011 at 19:11 ravi.panchravi.panch 6374 gold badges9 silver badges19 bronze badges 2
  • The code you provided works fine - what do you mean by incorporate into PHP? – Dennis Commented Oct 13, 2011 at 22:52
  • I meant in my .ctp file. Sorry for the lack of clarity. I did figured it out that, due to inclusion of prototype-1.6.0.2.js, it is not working. :) – ravi.panch Commented Oct 14, 2011 at 16:00
Add a ment  | 

3 Answers 3

Reset to default 9

This answer will apply if you are using the Prototype JS framework. (I don't see it here, but it might have been excluded).

There is a ticket open for this issue on the Google Visualization API here.

One potential issue is that there might be a conflicting version of prototype or some other library being included.

Alternately, the reduce function may need to be redefined:

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

[Note: I had this issue in the past, and this led to the solution. I am experiencing this issue a second time, but the original solution is not working for this new issue. Your mileage may vary.]

In my web app, I had included 'prototype-1.6.0.2.js', which created this error ! If I exclude 'prototype-1.6.0.2.js', everything runs great !

I had a similar issue where Google chart tools and Prototype library conflicted. I upgraded Prototype from version 1.6.1 to 1.7.1 and the problem was gone.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far