block by mbostock 3cba6ac2fac09e5483bf6c1fade733be

Example Authors

Full Screen

Source: Stewart Noyce, Christophe Viau.

Updated Example →

index.html

<!DOCTYPE html>
<meta charset="utf-8">
<style>

text {
  font: 10px sans-serif;
  text-anchor: middle;
}

.node circle {
  fill: none;
  pointer-events: all;
}

.node:not(.node--leaf) circle {
  stroke: #aaa;
}

.node--root circle {
  fill: #fff;
}

.node--leaf circle {
  fill: #ddd;
}

.node--hover circle {
  stroke: #000 !important;
  stroke-width: 1.5px;
}

</style>
<svg width="960" height="960"><g transform="translate(1,1)"></g></svg>
<script src="//d3js.org/d3.v4.min.js"></script>
<script>

var svg = d3.select("svg"),
    width = +svg.attr("width"),
    height = +svg.attr("height");

var format = d3.format(",d");

var pack = d3.pack()
    .size([width - 2, height - 2])
    .padding(3);

d3.json("authors.json", function(error, data) {
  if (error) throw error;

  var root = d3.hierarchy(data)
      .each(function(d) { if (/^other[0-9]+$/.test(d.data.name)) d.data.name = null; })
      .sum(function(d) { return d.size; })
      .sort(function(a, b) { return b.value - a.value; });

  pack(root);

  var node = svg.select("g")
    .selectAll("g")
    .data(root.descendants())
    .enter().append("g")
      .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
      .attr("class", function(d) { return "node" + (!d.children ? " node--leaf" : d.depth ? "" : " node--root"); })
      .each(function(d) { d.node = this; })
      .on("mouseover", hovered(true))
      .on("mouseout", hovered(false));

  node.append("circle")
      .attr("id", function(d, i) { return "node-" + i; })
      .attr("r", function(d) { return d.r; });

  var leaf = node.select(function(d) { return !d.children ? this : null; })
      .classed("node--leaf", true)
    .select(function(d) { return d.data.name ? this : null; });

  leaf.append("clipPath")
      .attr("id", function(d, i) { return "clip-" + i; })
    .append("use")
      .attr("xlink:href", function(d, i) { return "#node-" + i + ""; });

  leaf.append("text")
      .style("font-size", function(d) { return Math.sqrt(d.r) * 2 + "px"; })
      .attr("clip-path", function(d, i) { return "url(#clip-" + i + ")"; })
    .selectAll("tspan")
      .data(function(d) { return d.data.name.split(/\s+/g); })
    .enter().append("tspan")
      .attr("x", 0)
      .attr("y", function(d, i, nodes) { return 1.3 + (i - nodes.length / 2 - 0.5) + "em"; })
      .text(function(d) { return d; });

  node.append("title")
      .text(function(d) { return (d.data.name || "N/A") + "\n" + (d.children ? "" : d.parent.data.name + "\n") + format(d.value); });
});

function hovered(hover) {
  return function(d) {
    d3.selectAll(d.ancestors()
        .map(function(d) { return d.node; }))
      .classed("node--hover", hover);
  };
}

</script>