Skip to content

Instantly share code, notes, and snippets.

@wragge
Created December 8, 2013 11:19
Show Gist options
  • Save wragge/7856103 to your computer and use it in GitHub Desktop.
Save wragge/7856103 to your computer and use it in GitHub Desktop.
Get format facets from the Trove API and construct a JSON object structured like the sample data used in most of the d3.js examples. It should be possible to plug this in to many of the d3 demos, though I've only tried it so far with the treeview and sunburst. As used in Trove Zone Explorer -- http://dhistory.org/trove/zone-explorer/
$(function(){
var api_key = 'your api key';
function get_data() {
$.jsonp({
"callbackParameter": 'callback',
"url": "http://api.trove.nla.gov.au/result?q= &zone=book,article,collection,picture,map,music&facet=format&n=0&encoding=json&key=" + api_key,
"timeout": 20000,
"success": function(results) {
process_results(results);
}
});
}
function process_results(results) {
var data = {'name': 'Trove', 'children': []};
$.each(results.response.zone, function(index, zone) {
var this_zone = {'name': zone.name, 'children': []};
$.each(zone.facets.facet.term, function(index, term) {
this_child = process_term(term);
this_zone.children.push(this_child.term);
});
data.children.push(this_zone);
});
//display_sunburst(data); Send the data off to one of the d3 demos
}
function process_term(term) {
var term_count = parseInt(term.count, 10);
var this_term = {'name': term.display};
var count = 0;
if (typeof term.term !== 'undefined') {
this_term.children = [];
$.each(term.term, function(index, child) {
var this_child = process_term(child);
this_term.children.push(this_child.term);
count = count + this_child.term.size;
});
if (term_count > count) {
other_count = term_count - count;
this_term.children.push({'name': 'Other', 'size': other_count});
}
} else {
this_term.size = term_count;
}
return {'term': this_term, 'count': count};
}
get_data();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment