Page 1 of 1

Populating Google Chart with Data Array

Posted: Mon Oct 29, 2012 3:08 am
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 !

Re: Populating Google Chart with Data Array

Posted: Tue Oct 30, 2012 1:04 am
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.

Re: Populating Google Chart with Data Array

Posted: Wed Nov 12, 2014 2:59 am
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