(function() { var height, map, svg, vis, width, zoom, levels; width = document.body.clientWidth; height = document.body.clientHeight; /* create the SVG */ svg = d3.select('body').append('svg').attr('width', width).attr('height', height); vis = svg.append('g'); map = vis.append('g').attr('transform', "translate(" + (width / 2) + "," + (height / 2) + ")scale(.13,.13)"); /* define a zoom behavior */ zoom = d3.behavior.zoom().scaleExtent([0, 5]).on('zoom', function() { /* whenever the user zooms, modify translation and scale of the zoom group accordingly */ return vis.attr('transform', "translate(" + (zoom.translate()) + ")scale(" + (zoom.scale()) + ")"); }); /* bind the zoom behavior to the main SVG */ svg.call(zoom); d3.json('weighted_ontology.json', function(data) { var defs, depth_color, hierarchy, leaves, nodes, scale, tree; tree = dbpedia_reader.tree(data.children[14]); hierarchy = d3.layout.hierarchy(); nodes = hierarchy(tree); /* this tree is unordered, we need a canonical ordering for it */ tree_utils.canonical_sort(tree); tree_utils.compute_leaf_descendants(tree); /* tree height */ tree_utils.compute_height(tree); /* obtain the sequence of leaves */ leaves = tree_utils.get_leaves(tree); /* VISUALIZATION */ /* compute the space-filling curve layout */ /*spiral = { tiling: 'hex', base: 3, angle: Math.PI / 3, axiom: '-Y', rules: { X: 'XF', Y: 'Y+XF+X+XF+XF+XF+XF' } };*/ scale = 25; //translation = sfc_layout.displace(leaves, spiral, scale, scale, -Math.PI/6); translation = sfc_layout.displace(leaves, sfc_layout.GOSPER, scale, scale, -Math.PI/6); //translation = sfc_layout.displace(leaves, sfc_layout.HILBERT, scale, scale, 0); /* compute also the position of internal nodes */ //sfc_layout.displace_tree(tree); /* translate cells to label font size */ /*cells2fontsize = function(x) { return Math.log(x) * scale; }*/ /*d3.scale.pow() .exponent(3) .domain([1, leaves.length]) .range([4, 100]);*/ /* define a color scale for leaf depth */ depth_color = d3.scale.linear() .domain([0, d3.max(leaves, function(d) {return d.depth;})]) .range(['#FFF7DB', '#F0A848']) .interpolate(d3.interpolateHcl); /* group leaves by depth */ levels = []; for (depth = 0, _ref3 = tree.height; 0 <= _ref3 ? depth <= _ref3 : depth >= _ref3; 0 <= _ref3 ? depth++ : depth--) { levels.push([]); } for (_l = 0, _len4 = nodes.length; _l < _len4; _l++) { node = nodes[_l]; levels[node.depth].push(node); } /* compute all the internal nodes regions */ jigsaw.treemap(tree, scale, jigsaw.HEX_CELL); //jigsaw.treemap(tree, scale, jigsaw.SQUARE_CELL); /* define the level zero region (the land) */ /*defs = svg.append('defs'); defs.append('path') .attr('id', 'land') .attr('d', jigsaw.get_svg_path(tree.region, scale/scale));*/ /* faux land glow (using filters takes too much resources) */ map.append('use').attr('class', 'land-glow-outer').attr('xlink:href', '#land'); map.append('use').attr('class', 'land-glow-inner').attr('xlink:href', '#land'); /* DRAW the CELLS */ cells = map.selectAll('.cell') .data(leaves) .enter().append('path') .attr('class', 'cell') .attr('d', jigsaw.hex_generate_svg_path(scale)) //.attr('d', jigsaw.square_generate_svg_path(scale)) .attr('transform', function(d) { return "translate(" + d.x + "," + d.y + ")"; }) .attr('fill', function(d) { return depth_color(d.depth); //return '#F0A848'; }) .attr('opacity', 0.5); cells.append("title") .text(function(d) { return d.name; }); /* DRAW BOUNDARIES */ /*depth2width = function(x) { //return (20 - 0.2) / Math.pow(2, x) + 0.2; return Math.pow(6 - x, 3)/3; };*/ depth2width = d3.scale.pow() .exponent(-2) .domain([1, 4]) .range([8, 1]); old_highlighted_depth = null; regions_g = map.append('g'); regions_levels = regions_g.selectAll('.level') .data(levels).enter() .append('g') .attr('class', 'level'); regions_levels.selectAll('.region').data(function(level) { return level.filter(function(d) { return true; }); }).enter().append('path').attr('class', 'region').attr('d', function(d) { if (d.height > 1) { return jigsaw.get_svg_path(d.region); } }).attr('stroke-width', function(d) { switch(d.depth) { case 1: return 3; case 2: return 2.2; case 3: return 1; case 4: return 0.5; case 5: return 0.5; } }).attr('stroke', 'white') .style("stroke-dasharray", function(d) { switch(d.depth) { case 2: return "3, 4"; case 3: return "3, 3"; case 4: return "1, 4"; } }); /* draw the level one region boundaries */ /* draw the land border (above cells) */ map.append('use').attr('class', 'land-fill').attr('xlink:href', '#land'); /* draw labels */ /*return map.selectAll('.label').data(nodes.filter(function(d) { return d.depth === 1; })).enter().append('text').attr('class', 'label').attr('font-size', function(d) { return cells2fontsize(d.leaf_descendants.length); }).attr('dy', '0.35em').attr('transform', function(d) { return "translate(" + d.x + "," + d.y + ")"; }).text(function(d) { return d.name.split('.').reverse()[0]; });*/ /* draw the leaf labels */ /*return map.selectAll('.label') .data(leaves).enter() .append('text') .attr('class', 'label') .attr('font-size', '2px') .attr('dy', '0.35em') .attr('transform', function(d) { return "translate(" + d.x + "," + d.y + ")"; }).text(function(d) { return d.key; });*/ }); }).call(this);