Variable number of points in visualization.arraytodatatable

1,337 views
Skip to first unread message

devel...@nctcog.org

unread,
Jun 18, 2012, 12:00:28 PM6/18/12
to Google Visualization API
Is it possible to have a variable array of points in a line or bar
chart?
Your code example shows

var data = new google.visualization.arrayToDataTable ([

['heading1','heading2'],

[data point, datapoint],
[data point, datapoint]

]);

All of the points are hard coded, but I don't know how many points I'm
going to have. It could be any where from 0-96.

I have successfully passed in arrays so it reads as

['heading1', 'heading2'],
[arr1[0],arr2[0]],
[arr1[1],arr2[1]]
]);
and so forth but I'm having to build ridiculously large case
statements to handle the arrays so i don't get errors.

I tried building in a for next loop for the array elements, but I'm
getting syntax errors on that as well.

Here is my code with the loop that is giving syntax errors:

var data = new google.visualization.arrayToDataTable([
['Time','Count'],
for (var n=0;n<96;n++)
{
[arr2[n], parseInt(arr[n]) ],
}
[arr2(mycount),parseInt(arr[mycount])]

]);

asgallant

unread,
Jun 18, 2012, 1:15:00 PM6/18/12
to google-visua...@googlegroups.com
Here's one way of doing it (assumes you want everything in arr2):

var dataArray [
    ['Time','Count']
];

// interate over all of arr2
for (var n=0arr2.lengthn++
    dataArray.push([arr2[n]parseInt(arr[n])])
}
dataArray.push([arr2(mycount),parseInt(arr[mycount])]);

var data new google.visualization.arrayToDataTable(dataArray) 

devel...@nctcog.org

unread,
Jun 18, 2012, 3:13:58 PM6/18/12
to google-visua...@googlegroups.com
That worked with two slight modifications.  First, there isn't any need to put the final element outside the loop as I was initially doing so the line
 
dataArray.push([arr2(mycount),parseInt(arr[mycount])]);

can go away.  Also removed the comma after the array element.  Works great. Final code looks like
 
 
  var dataArray = [['Date','Count']];
 
 for (var n =0; n < arr2.length; n++)
 {
  dataArray.push ([arr2[n], parseInt(arr[n])]) }

asgallant

unread,
Jun 18, 2012, 3:17:35 PM6/18/12
to google-visua...@googlegroups.com
Oops, sorry, that comma was supposed to be a semi-colon  >;o)

devel...@nctcog.org

unread,
Jun 18, 2012, 3:20:12 PM6/18/12
to google-visua...@googlegroups.com

It actually is working without even the semicolon, though that might be bad coding syntax. 

asgallant

unread,
Jun 18, 2012, 3:28:43 PM6/18/12
to google-visua...@googlegroups.com
Leaving it out is bad syntax, but javascript has some limited tolerance of bad syntax, so it works in that case (but you should not count on it working).
Reply all
Reply to author
Forward
0 new messages