how to pass data to "drawVisualization" function

2,697 views
Skip to first unread message

it-works!

unread,
Mar 1, 2012, 10:02:32 AM3/1/12
to Google Visualization API
Hi,

I'm trying to change the content of some variables passing it through
the function but I get errors no matter what I pass.. for example:

<script type="text/javascript">

//getting my data outside of the draw function...

var mydata = google.visualization.arrayToDataTable([["Name", "Gender",
"Age", "Donuts eaten"],["David", "Male", 34, 20],["Lisa", "Female",
23, 7]]);

//I would like to pass it to the function...

function drawVisualization(getData) {
// Prepare the data

var data = getData;
data;

//replacing the standard:
/*
var data = google.visualization.arrayToDataTable([
['Name', 'Gender', 'Age', 'Donuts eaten'],
['Michael' , 'Male', 12, 5],
['Elisa', 'Female', 20, 7],
['Robert', 'Male', 7, 3],
['John', 'Male', 54, 2],
['Jessica', 'Female', 22, 6],
['Aaron', 'Male', 3, 1],
['Margareth', 'Female', 42, 8],
['Miranda', 'Female', 33, 6]
]);


//by calling:
google.setOnLoadCallback(drawVisualization(mydata)); <-- seems like
I can't pass any variables here?

thanks for any help...

asgallant

unread,
Mar 1, 2012, 11:16:40 AM3/1/12
to google-visua...@googlegroups.com
The google#setOnLoadCallback method accepts a function as a parameter.  When you pass "drawVisualization(myData)" to it, it actually executes the drawVisualization function and passes the return value to #setOnLoadCallback.  What you would need is something like this:

google.setOnLoadCallback(init);

function init () {
    drawVisualization(mydata);
}

Also note that google.visualization might not exist at the time you call it unless you call it inside the google load callback function, like this:

function init () {
    var mydata = google.visualization.arrayToDataTable([
        ["Name", "Gender", "Age", "Donuts eaten"],
        ["David", "Male", 34, 20],
        ["Lisa", "Female", 23, 7]
    ]);  

    drawVisualization(mydata);
}
Reply all
Reply to author
Forward
0 new messages