var width = 960, height = 500, radius = Math.min(width, height) / 2, colors = {'Person': '#E14E5F', 'Organisation': '#A87621', 'Place': '#43943E', 'Work': '#AC5CC4', 'Species': '#2E99A0', 'Event': '#2986EC'}, colorOpacity = 0.7; var svg = d3.select("body").append("svg") .attr('viewBox', -width/2 + " " + (-height/2) + " " + width + " " + height) var zoomable_layer = svg.append('g'); zoom = d3.behavior.zoom() .scaleExtent([1,12]) .on('zoom', function() { zoomable_layer .attr('transform', "translate(" + zoom.translate() + ")scale(" + zoom.scale() + ")"); }); svg.call(zoom); var partition = d3.layout.partition() .size([2 * Math.PI, radius * radius]) .value(function(d) { return d.size; }); var arc = d3.svg.arc() .startAngle(function(d) { return d.x; }) .endAngle(function(d) { return d.x + d.dx; }) .innerRadius(function(d) { return Math.sqrt(d.y); }) .outerRadius(function(d) { return Math.sqrt(d.y + d.dy); }); var text = zoomable_layer.append("text") .attr("x", 0) .attr("y", 0) .attr("dy", "0.35em"); var getColor = function(d) { var flag; if (d.name === "owl#Thing") { return '#7E7F7E'; } else if (d.name in colors) { return colors[d.name]; } else { flag = '#7E7F7E'; while (d.parent.name !== "owl#Thing") { if (d.parent.name in colors) { flag = colors[d.parent.name]; break; } else { d = d.parent; } } return flag; } }; var setSize = function(d) { if (!d.hasOwnProperty("children")) { d.size = 1; return 1; } else { var sum = 1; d.children.forEach(function(c) { sum += setSize(c); }); d.size = sum; return sum; } return d; }; thousand_sep_format = d3.format(','); drawLegend(); redraw = function(file_name) { d3.json(file_name, function(error, root) { setSize(root); root = {"name": root.name, "size": root.size, "children": [root]}; var path = zoomable_layer.datum(root).selectAll("path") .data(partition.nodes, function(d) {return (d.depth==0) ? 'root' : d.name;}); path.exit() .transition().duration(1000) .style("opacity", 0) .remove(); path .attr("display", function(d) { return d.depth ? null : "none"; }) // hide inner ring .style("fill", function(d) { return getColor(d); }) .transition().delay(1000).duration(3000) .attr("d", arc); path .append('title') .text(function(d) {return thousand_sep_format(d.size) + "\n" + d.name + "\n" + (100 * d.size / root.size).toPrecision(3);}); enter_path = path .enter().append("path") .attr("d", arc) .attr("display", function(d) { return d.depth ? null : "none"; }) // hide inner ring .style("stroke", "#fff") .style("opacity", 0) .style("fill", function(d) { return getColor(d); }) .on("mouseover", mouseover) .on("mouseout", mouseout) .transition().delay(4000).duration(1000) .style("opacity", colorOpacity); function mouseover(d) { var percentage = (100 * d.size / root.size).toPrecision(3); text.html("" + thousand_sep_format(d.size) + "" + d.name + "" + percentage + "%"); d3.selectAll("path") .filter(function(d1) { return d === d1; }) .style("opacity", 0.5); } function mouseout(d) { text.html(""); d3.selectAll("path") .filter(function(d1) { return d === d1; }) .style("opacity", colorOpacity); } }); }; d3.select(self.frameElement).style("height", height + "px"); d3.select('select') .on('change', function() { redraw(this.value+'.json'); }); redraw("ontology2015.json"); function drawLegend() { var li = { w: 85, h: 30, s: 3, r: 3 }; var legend = svg.append("g") .attr("transform", function(d, i) { return "translate(" + (width/2 - li.w*1.3) + "," + (height/2 - li.h*Object.keys(colors).length*1.2) + ")"; }); var g = legend.selectAll("g") .data(d3.entries(colors)) .enter().append("svg:g") .attr("transform", function(d, i) { return "translate(0," + (i * (li.h + li.s)) + ")"; }); g.append("svg:rect") .attr("rx", li.r) .attr("ry", li.r) .attr("width", li.w) .attr("height", li.h) .style("opacity", colorOpacity) .style("fill", function(d) { return d.value; }); g.append("svg:text") .attr("x", li.w / 2) .attr("y", li.h / 2) .attr("dy", "0.35em") .attr("text-anchor", "middle") .classed("legendItem", true) .text(function(d) { return d.key; }); }