I've noticed a peculiar problem crop up in a project I'm working on, where 
I need to use the same data in multiple DataTables (it's a situation that 
is unsuited to using DataViews, at least as long as the API doesn't have a 
row equivalent of calculated columns); in particular, I need one version 
for drawing Table visualizations and one version for drawing charts.  I 
find that when I apply formatting to one table, it shows up (unwelcome) in 
the other one.

I'm building the tables like this:

function createReport(dataObj) {
    var tableData = new google.visualization.DataTable(dataObj);
    var chartData = new google.visualization.DataTable(dataObj);
    // do stuff
}

Is dataObj being referenced in the DataTable?  It would seem so.  I created 
a bunch of test cases, first using a javascript object (as in my 
application): http://jsfiddle.net/asgallant/yMh3e/, then with a JSON 
string: http://jsfiddle.net/asgallant/yMh3e/2/.  Behavior works as expected 
when using a JSON string, and when calling the DataTable#clone method, but 
not when passing a javascript object.

After typing all this out, I thought of a test case to answer my own 
question: create the DataTables, modify the JSON object, and then draw; if 
the modification makes it into the DataTables, they are referencing the 
original object.  Test and confirm theory: 
http://jsfiddle.net/asgallant/yMh3e/4/.  Oddly enough, deleting the 
original object does not clear the data from the DataTable: 
http://jsfiddle.net/asgallant/yMh3e/5/.  Likely all of this comes down to 
javascript's bizarre rules for object assignment.

So, I've answered my own question here.  The lesson is, if you need to 
create multiple tables from the same object (or reuse an object to create 
more tables), you need to make sure to use the clone method, or else you 
will mess with all of the DataTables.  This could be cleaned up in the 
DataTable constructor by deep cloning the javascript object, but I'm not 
sure if the end result is worth the effort if it's more than a minute or 
two to fix.

</end rambling>

-- 
You received this message because you are subscribed to the Google Groups 
"Google Visualization API" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-visualization-api.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to