Populating Google Chart with Data Array

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Live24x7
Forum Contributor
Posts: 194
Joined: Sat Nov 19, 2011 9:32 am

Populating Google Chart with Data Array

Post by Live24x7 »

Need help with populating data to google charts

I have taken this example from google chart documentation, so it naturally works correctly.
var data = google.visualization.arrayToDataTable([
['Age', 'Weight'],
[ 8, 12],
[ 4, 5.5],

]);
if i replace this with:
var data = google.visualization.arrayToDataTable([
['Age', 'Weight'],
[eval(arr1[0]),eval(arr2[0])],
[eval(arr1[1]),eval(arr2[1])],
[eval(arr1[2]),eval(arr2[2])],

]);
it still works- meaning my array values for arr1 and arr2 are being recognized.

However if i replace this with
var data = google.visualization.arrayToDataTable([
['Age', 'Weight'],
for(i=0;i<arr1.length;i++){
[eval(arr1),eval(arr2)],
}

]);


I get syntax error pointing at 'for' in the javascript error console.

How do i populate the chart dynamically with a for loop(or otherwise). Can you please point the error out here ? Thanks a lot !
Live24x7
Forum Contributor
Posts: 194
Joined: Sat Nov 19, 2011 9:32 am

Re: Populating Google Chart with Data Array

Post by Live24x7 »

Turns out that you need to add colums and rows as per google visualization API.

Code: Select all


data.addColumn('number', 'X');
data.addColumn('number', 'Y (scatter)');
data.addColumn('number', 'Y (line)');
for (var i = 0; i < arry.length; ++i) {
    data.addRow(arrx[i], arry[i], arrz[i]);
}

Also worth noting that since i added 3 colums here, i need to populate all the corresponding fields in each row - which i have done here using arrx, arry, arrz.
amarinder
Forum Newbie
Posts: 1
Joined: Wed Nov 12, 2014 1:26 am

Re: Populating Google Chart with Data Array

Post by amarinder »

hi i am also looking for annswer to above question......i am not much familiar with javascript...so please explain me in little detail,,,,how i can run loop for above issue
Post Reply