Cannot read property '0' of undefined

897 views
Skip to first unread message

Bill Bohling

unread,
Mar 29, 2016, 4:03:35 PM3/29/16
to Google Visualization API
I've just started using the Visualization API, trying to build a table.  I've got code putting together a JSON object per https://developers.google.com/chart/interactive/docs/reference#methods, and my data looks like this:

{"cols":[{"id":"day","label":"Day","type":"date"},{"id":"Left","label":"Left","type":"string"},{"id":"Center","label":"Center","type":"string"},{"id":"Right","label":"Right","type":"string"}],"rows":[[{"v":"2016-03-20T04:00:00"},{"v":"unscheduled"},{"v":"unscheduled"},{"v":"6"}],[{"v":"2016-03-20T04:00:00"},{"v":"11"},{"v":"unscheduled"},{"v":"unscheduled"}],[{"v":"2016-03-20T04:00:00"},{"v":"unscheduled"},{"v":"15"},{"v":"unscheduled"}],[{"v":"2016-03-21T10:08:07"},{"v":"12"},{"v":"unscheduled"},{"v":"unscheduled"}],[{"v":"2016-03-27T14:00:00"},{"v":"unscheduled"},{"v":"unscheduled"},{"v":"2"}]]}

The code to use it is

var data = new google.visualization.DataTable(dataTbl);
var table = new google.visualization.Table(document.getElementById('google-chart'));
table.draw(data, {frozenColumns: 1, alternatingRowStyle: true, width: '100%', height: '600'});

When I trying loading up my page, I get Cannot read property '0' of undefined.  I've run my data through a JSON parser and it looks good there.

Sergey Grabkovsky

unread,
Mar 29, 2016, 4:05:29 PM3/29/16
to Google Visualization API
Hi Bill,

I believe that your issue stems from using dates incorrectly. Please follow the instructions on this page to properly format your dates.

--
You received this message because you are subscribed to the Google Groups "Google Visualization API" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-visualizati...@googlegroups.com.
To post to this group, send email to google-visua...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-visualization-api.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-visualization-api/c6893fca-1085-49a5-bf40-232bdb52cdcf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--

unnamed.gif

Sergey Grabkovsky

Software Engineer

Google, Inc

gra...@google.com


Bill Bohling

unread,
Mar 29, 2016, 5:02:23 PM3/29/16
to Google Visualization API
Hi Sergey,

I've redone my times per the datesandtimes page using 'Date(milliseconds)':

var milliseconds = Date.parse(schedules[i]['startTime']);
row[0] = {
'v': 'Date(milliseconds)'
}

but the date is coming out as the string
'Date(milliseconds)' 
  and I'm still getting the error.  Here's some prettified JSON that might make things easier:

{
  "cols": [
    {
      "id": "day",
      "label": "Day",
      "type": "date"
    },
    {
      "id": "Left",
      "label": "Left",
      "type": "string"
    },
    {
      "id": "Center",
      "label": "Center",
      "type": "string"
    },
    {
      "id": "Right",
      "label": "Right",
      "type": "string"
    }
  ],
  "rows": [
    [
      {
        "v": "Date(milliseconds)"
      },
      {
        "v": "unscheduled"
      },
      {
        "v": "unscheduled"
      },
      {
        "v": "6"
      }
    ],
...]}

I have also tried converting the date using new Date(milliseconds) and passing that as the value of 'v' for my date column, and no luck there, either.

Thanks for the help!

Sergey Grabkovsky

unread,
Mar 29, 2016, 7:07:41 PM3/29/16
to Google Visualization API
Bill,

Just to be clear, you're substituting "milliseconds" with the actual milliseconds, correct? Like "Date(13829382)"?

If so, could you please post a complete code sample that reproduces your issue?
--
You received this message because you are subscribed to the Google Groups "Google Visualization API" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-visualizati...@googlegroups.com.
To post to this group, send email to google-visua...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-visualization-api.

For more options, visit https://groups.google.com/d/optout.

Daniel LaLiberte

unread,
Mar 29, 2016, 7:09:34 PM3/29/16
to Google Visualization API
Hi Bill,

You would have to construct the string with concatenation.  The variable does not get evaluated inside the string.  So something like this:

var milliseconds = Date.parse(schedules[i]['startTime']);
row[0] = {
    'v': 'Date(' + milliseconds + ')'
}

However, the string notation for dates requires that you specify the year, month, day, hour, minute, seconds, and the milliseconds.

Note that months start at 0.


--
You received this message because you are subscribed to the Google Groups "Google Visualization API" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-visualizati...@googlegroups.com.
To post to this group, send email to google-visua...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-visualization-api.



--

Bill Bohling

unread,
Mar 30, 2016, 7:15:13 AM3/30/16
to Google Visualization API
I was wondering if it might be a matter of needing to concatenate that.  I also see something else, in my row format.  I've got
"rows": [
    [
      {
        "v": "Date(milliseconds)"
      },
      {
        "v": "unscheduled"
      },
      {
        "v": "unscheduled"
      },
      {
        "v": "6"
      }
    ]


it should be:
rows: [{c:[{v: 'a'},
             {v: 1.0, f: 'One'},
             {v: new Date(2008, 1, 28, 0, 31, 26), f: '2/28/08 12:31 AM'}
        ]},


On Tuesday, March 29, 2016 at 4:03:35 PM UTC-4, Bill Bohling wrote:

Bill Bohling

unread,
Mar 30, 2016, 7:27:30 AM3/30/16
to Google Visualization API
My error was in the JSON for my rows.  Got each one of those as an array keyed by 'c:' and now I'm getting an error about time.  So off to read up on those.


On Tuesday, March 29, 2016 at 4:03:35 PM UTC-4, Bill Bohling wrote:
Reply all
Reply to author
Forward
0 new messages