Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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 --
var api_key = 'your api key';
function get_data() {
"callbackParameter": 'callback',
"url": " &zone=book,article,collection,picture,map,music&facet=format&n=0&encoding=json&key=" + api_key,
"timeout": 20000,
"success": function(results) {
function process_results(results) {
var data = {'name': 'Trove', 'children': []};
$.each(, function(index, zone) {
var this_zone = {'name':, 'children': []};
$.each(zone.facets.facet.term, function(index, term) {
this_child = process_term(term);
//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);
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};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.