JSFiddle - React, Tailwind, and code Playground

by Adelaide Chen

HTML

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<!--https://stackoverflow.com/questions/15369146/constraining-d3-force-layout-graphs-based-on-node-degree-->
<div id="graph"></div>

CSS

.link {
  stroke: #000;
  stroke-width: 1.5px;
}

.node {
  fill: #666;
  stroke: #fff;
  stroke-width: 1.5px;
}

.node.a { fill: #1f77b4; }
.node.b { fill: #ff7f0e; }
.node.c { fill: #2ca02c; }

.node.modified { fill: 'red' ! important; }

JavaScript

var a = {id: "a", active:true}, 
    b = {id: "b", active:false}, 
    c = {id: "c", active:true};

var width = 500,
    height = 500;

var nodes = [a, b, c],
    links = [{source: a, target: b}, 
             {source: a, target: c}, 
             {source: b, target: c}];

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

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

var node = svg.selectAll(".node")
    .data(force.nodes().filter(function(d) { return d.active; }));

var link = svg.selectAll(".link")
      .data(force.links().filter(function(d) { 
          var show =  d.source.active && d.target.active;
          if (show)
              console.log("kept", d);
          else
              console.log("excluded", d);
          return show;
      }) );

     
link.enter().insert("line", ".node").attr("class", "link");
link.exit().remove();

node.enter().append("circle").attr("class", function(d) { return "node " + d.id; }).attr("r", 8)
node.exit().remove();

force.start();

function tick() {
    node.attr("cx", function(d) { return d.x; })
      .attr("cy", function(d) { return d.y; })

    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; });
}