JSFiddle - React, Tailwind, and code Playground

by bizamajig

HTML

<script src="http://d3js.org/d3.v3.min.js"></script>

CSS

.node {
  cursor: pointer;
  stroke: #3182bd;
  stroke-width: 1.5px;
}

.link {
  fill: none;
  stroke: #9ecae1;
  stroke-width: 1.5px;
}

JavaScript

var json = {
    "nodes": 
        {
        name:'RootNode',
        children:[
        {
            "switch_id": 1,
            "name": "switch1",
            "children": [
                {
                    "name": "port-1"
                },
                {
                    "name": "port-2"
                }
            ]
        },
        {
            "switch_id": 2,
            "name": "switch2"
        },
        {
            "switch_id": 3,
            "name": "switch3"
        }
        ]
        }
    ,
    "links":[
       {
          "source": 1,
          "target": 2, 
          "value": 5
       }
     ]
};



var width = 960,
    height = 500,
    root;

var force = d3.layout.force()
    .size([width, height])
    .on("tick", tick);

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

var link = svg.selectAll(".link"),
    node = svg.selectAll(".node");

/*d3.json("readme.json", function(json) {
  root = json;
  update();
});*/

// Now get at the nodes you want displayed
root = json.nodes;
update();

function update() {
  var nodes = flatten(root),
      links = d3.layout.tree().links(nodes);

  // Restart the force layout.
  force
      .nodes(nodes)
      .links(links)
      .start();

  // Update the links…
  link = link.data(links, function(d) { return d.target.id; });

  // Exit any old links.
  link.exit().remove();

  // Enter any new links.
  link.enter().insert("line", ".node")
      .attr("class", "link")
      .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; });

  // Update the nodes…
  node = node.data(nodes, function(d) { return d.id; }).style("fill", color);

  // Exit any old nodes.
  node.exit().remove();

  // Enter any new nodes.
  node.enter().append("circle")
      .attr("class", "node")
      .attr("cx",...