var width = 960, height = 600, radius = Math.min(width, height) / 2, colorOpacity = 0.7; var svg = d3.select("body").append("svg") .attr("width", width) .attr("height", height) .append("g") .attr("transform", "translate(" + width / 2 + "," + height / 1.95 + ") scale(1.00)") .call(d3.behavior.zoom().scaleExtent([1, 12]).on("zoom", zoom)) .append("g"); function zoom() { svg.attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")"); } 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 = svg.append("text") .attr("x", 0) .attr("y", 0) .attr("dy", "0.35em"); thousand_sep_format = d3.format(','); d3.json("http://wafi.iit.cnr.it/webvis/tmp/foursquareCategories.json", function(error, root) { getSize(root); root = {"name": root.name, "size": root.size, "children": [root]}; function getSize(d) { if (!d.hasOwnProperty("children")) { d.size = 1; return 1; } else { var sum = 0; d.children.forEach(function(c) { sum += getSize(c); }); d.size = sum; return d.size; } return d; } var path = svg.datum(root).selectAll("path") .data(partition.nodes) .enter().append("path") .attr("display", function(d) { return d.depth ? null : "none"; }) // hide inner ring .attr("d", arc) .style("stroke", "#fff") .style("fill", function(d) { return "#43943E"; }) .style("opacity", colorOpacity) .on("mouseover", mouseover) .on("mouseout", mouseout); function mouseover(d) { console.log(d.size); 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");