Merging 2 datatables with identical columns

659 views
Skip to first unread message

Elco Jacobs

unread,
Sep 28, 2011, 4:29:08 PM9/28/11
to Google Visualization API
I am generating datatables in a python script on a DD-WRT router. The
script will generate datatables of up to 5000 rows. The memory on the
router is limited, so the datatables grow too large and the router
crashes.
To prevent this from happening, I want to create a new datatable each
day and write these to separate JSON response files with
datatable.ToJSonResponse().

When a page is loaded in a web browser, these JSON response files have
to be loaded and combined into one chart in javascript.

I can iterate over the JSON response files and read them into
datatables, but I cannot find a way to append one datatable to
another.

The addRows() function is almost what I need, but it cannot accept an
entire datatable as input.

Can anybody help me on how to merge the two datatables into one
datatable so I can create a combined chart?

Riccardo Govoni ☢

unread,
Sep 29, 2011, 4:18:30 AM9/29/11
to google-visua...@googlegroups.com
Could you write them out using the ToJSon instead of ToJSonResponse ? This would serialize a plain json object for each datatable. In the browser, you could load these json objects, easily merge them together (join their 'rows' array together, assuming the columns' definitions are the same) and create a single datatable out of the joined json payload.

-- R.


--
You received this message because you are subscribed to the Google Groups "Google Visualization API" group.
To post to this group, send email to google-visua...@googlegroups.com.
To unsubscribe from this group, send email to google-visualizati...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-visualization-api?hl=en.


Elco Jacobs

unread,
Sep 29, 2011, 9:30:29 AM9/29/11
to Google Visualization API
I used the JSON response because the JSON generated by the python
library is reported as invalid when imported through ajax.
I cannot get the annotated time line data to load correctly. Maybe it
has something to do with {v:new Date(2011,8,29,12,53,42)} in the
table.

If I get them to load correctly, how would I join the rows arrays?

This is what my php generates:

$.get("data/testbiertje2/testbiertje2-2011-09-29.json", function(json)
{
eval("var jsonstring = " + json)
beerData = new google.visualization.DataTable(jsonstring);
});

On Sep 29, 10:18 am, Riccardo Govoni ☢ <battleho...@gmail.com> wrote:
> Could you write them out using the
> ToJSon<http://code.google.com/apis/chart/interactive/docs/dev/gviz_api_lib.h...>
> instead
> of ToJSonResponse ? This would serialize a plain json object for each
> datatable. In the browser, you could load these json objects, easily merge
> them together (join their 'rows' array together, assuming the columns'
> definitions are the same) and create a single datatable out of the joined
> json payload.
>
> -- R.
>

Elco Jacobs

unread,
Sep 29, 2011, 9:33:06 AM9/29/11
to Google Visualization API
I am sorry, that was copied from an old source. I have the AJAX load
working now:

var jsonData = $.ajax({
url: "data/testbiertje2/testbiertje2-2011-09-29.json",
dataType:"json",
async: false
}).responseText;
evalledJsonData = eval("("+jsonData+")");

beerData = new
google.visualization.DataTable(evalledJsonData);

beerChart = new
google.visualization.AnnotatedTimeLine(document.getElementById('visualization'));
beerChart.draw(beerData, {
'displayAnnotations': true,
'scaleType': 'maximized',
'displayZoomButtons': false,
'allValuesSuffix': "\u00B0 C",
'numberFormats': "##.0",
'displayAnnotationsFilter' : true});
}


How would I join multiple of these json files?

asgallant

unread,
Sep 29, 2011, 10:02:18 AM9/29/11
to google-visua...@googlegroups.com
If your tables have the same columns, you could use a full join to knit them together, though that would result in any rows duplicated across tables being merged rather than added separately (probably not an issue if you have a date-time column).

Elco Jacobs

unread,
Sep 29, 2011, 10:17:17 AM9/29/11
to Google Visualization API
Thanks for all your input, I got it working:

$handle = opendir($currentBeerDir);
$first = true;
while (false !== ($file = readdir($handle))){ // iterate over
all json files in directory
$extension = strtolower(substr(strrchr($file, '.'), 1));
if($extension == 'json' ){
for ($i = 1; $i <= 10; $i++) { //try 10 times, file might be in
use
$jsonFile = $currentBeerDir . '/' . $file;
if(is_readable($jsonFile)){
?>
var jsonData = $.ajax({
url: <?php echo '"' . $jsonFile . '"' ?>,
dataType:"json",
async: false
}).responseText;
var evalledJsonData = eval("("+jsonData+")");

<?php
if($first){
?>
combinedJson = evalledJsonData;
<?php
$first = false;
}
else{
?>
combinedJson.rows =
combinedJson.rows.concat(evalledJsonData.rows);
<?php
$first = false;
}
break;
}
else{
time_nanosleep(0,1000000);
}
}
// break;
}
}
?>
beerData = new google.visualization.DataTable(combinedJson);
Reply all
Reply to author
Forward
0 new messages