views:

63

answers:

1

Hi! I'd like to use google drawVisualization API. Example:

 var data = new google.visualization.DataTable();
  data.addColumn('string');
  data.addColumn('number');
  data.addRows([
    ['a', 14],
    ['b', 47],
    ['c', 80],
    ['d', 55],
    ['e', 16],
    ['f', 90],
    ['g', 29],
    ['h', 23],
    ['i', 58],
    ['j', 48]
  ]);

My version gets elements by an other google api, and join them, and after place the variable between ([ and ]), to be like in the example.

var outputGraph = [];

for (var i = 0, entry; entry = entries[i]; ++i) {

var asd = [
entry.getValueOf('ga:pageTitle'),
entry.getValueOf('ga:pageviews')
].join("',");
outputGraph.push(" ['" + asd + "]");
//get the 2 elements and join them to be like ['asd', 2], 

}
// this is fine, the outputgraph is like ['asd', 2], ['asd', 2], ['asd', 2] as seen in the example

var outputGraphFine = ("(["+outputGraph+"])");
// i suggest this is which fails the script.

var data = new google.visualization.DataTable();
 data.addColumn('string', 'Task');
 data.addColumn('number', 'Hours per Day');
 data.addRows = outputGraphFine;

But it doesn't work. Why?

+1  A: 

Two suggestions.

1 - According to the documentation, addRows() gets invoked like this:

data.addRows([
  ['Work', 11],
  ['Eat', 2],
  ['Commute', 2],
  ['Watch TV', 2],
  ['Sleep', {v:7, f:'7.000'}]
]);

so data.addRows = outputGraphFine; is erroneous to begin with.

2 - outputGraphFine is a string, and addRows() expects an object. To convert a string stored in a variable into an object you will need to tell Javascript to interpret it in expression parsing mode (in otherwords, to evaluate it as code):

data.addRows(eval(outputGraphFine));

Hope that helps.

karim79
` data.addRows( [ [ 'a', 148 ] , ['b', 47],['c', 80],['d', 55],['e', 16],['f', 90],['g', 29],['h', 23],['i', 58],['j', 48]]);` is good also.`eval()` does not work. :(
neduddki