Radial Dendrogram

by b_g_v

HTML

<p>Max two words distance without intermediate nodes</p>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script type="application/json" id="data">
{
  "nodes": [
    {
      "size": 1, 
      "atom": "ABC News"
    }, 
    {
      "size": 1, 
      "atom": "African American"
    }, 
    {
      "size": 1, 
      "atom": "Alex Jones"
    }, 
    {
      "size": 2, 
      "atom": "Alexander Petrov"
    }, 
    {
      "size": 5, 
      "atom": "Alexandria Ocasio-Cortez"
    }, 
    {
      "size": 1, 
      "atom": "Amy Klobuchar"
    }, 
    {
      "size": 1, 
      "atom": "Andrew Gillum"
    }, 
    {
      "size": 1, 
      "atom": "Aretha Franklin"
    }, 
    {
      "size": 2, 
      "atom": "Arizona Governor"
    }, 
    {
      "size": 18, 
      "atom": "Attorney General Jeff Sessions"
    }, 
    {
      "size": 1, 
      "atom": "Bank of England"
    }, 
    {
      "size": 2, 
      "atom": "Barack Obama"
    }, 
    {
      "size": 1, 
      "atom": "Ben Sasse"
    }, 
    {
      "size": 1, 
      "atom": "Bernie Sanders"
    }, 
    {
      "size": 10, 
      "atom": "Bob Woodward"
    }, 
    {
      "size": 10, 
      "atom": "Boston City Councilor Ayanna Pressley"
    }, 
    {
      "size": 1, 
      "atom": "British Prime Minister"
    }, 
    {
      "size": 2, 
      "atom": "Bush White House"
    }, 
    {
      "size": 1, 
      "atom": "CBS News"
    }, 
    {
      "size": 3, 
      "atom": "Capitol Hill"
    }, 
    {
      "size": 1, 
      "atom": "Capitol Police"
    }, 
    {
      "size": 1, 
      "atom": "Carl Bernstein"
    }, 
    {
      "size": 1, 
      "atom": "Category 1"
    }, 
    {
      "size": 5, 
      "atom": "Chairman Chuck Grassley"
    }, 
    {
      "size": 1, 
      "atom": "Chancellor Angela Merkel"
    }, 
    {
      "size": 1, 
      "atom": "Charlie Baker"
    }, 
    {
      "size": 1, 
      "atom": "Charlie Rowley"
    }, 
    {
      "size": 1, 
      "atom": "Chemical Weapons"
    }, 
    {
  ...

CSS

.link line {
  stroke: #696969;
}

.link line.separator {
  stroke: #fff;
  stroke-width: 2px;
}

.node circle {
  stroke: gray;
  stroke-width: 1.5px;
}

.node text {
  font: 10px sans-serif;
  pointer-events: none;
}

JavaScript

var data = document.getElementById('data').innerHTML,
    graph = JSON.parse(data);

var width = 1900,
    height = 1000;

var color = d3.scale.category20();

var radius = d3.scale.sqrt()
    .range([0, 6]);

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

var force = d3.layout.force()
    .size([width, height])
    .charge(-400)
    .linkDistance(function(d) { return radius(d.source.size) + radius(d.target.size) + 20; });

  force
      .nodes(graph.nodes)
      .links(graph.links)
      .on("tick", tick)
      .start();

  var link = svg.selectAll(".link")
      .data(graph.links)
    .enter().append("g")
      .attr("class", "link");

  link.append("line")
      .style("stroke-width", function(d) { return (d.bond * 2 - 1) * 2 + "px"; });

  link.filter(function(d) { return d.bond > 1; }).append("line")
      .attr("class", "separator");

  var node = svg.selectAll(".node")
      .data(graph.nodes)
    .enter().append("g")
      .attr("class", "node")
      .call(force.drag);

  node.append("circle")
      .attr("r", function(d) { return radius(d.size); })
      .style("fill", function(d) { return color(d.atom); });

  node.append("text")
      .attr("dy", ".35em")
      .attr("text-anchor", "middle")
      .text(function(d) { return d.atom; });

  function tick() {
    link.selectAll("line")
        .attr("x1", function(d) { return d.source.x; })
        .attr("y1", function(d) { return d.source.y; })
        .attr("x2", function(d) { return d.target.x; })
        .attr("y2", function(d) { return d.target.y; });

    node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
  }