JSFiddle - React, Tailwind, and code Playground
HTML
<!DOCTYPE html>
<html>
<head>
<title>Force-Directed Layout</title>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js"></script>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.geom.js"></script>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.layout.js"></script>
</head>
<body>
</body>
</html>
CSS
circle {
stroke-width: 1.5px;
}
line {
stroke: #999;
}
JavaScript
var w = 960,
h = 500,
r = 6,
fill = d3.scale.category20();
var force = d3.layout.force().gravity(.01).charge(-120).linkDistance(30).size([w, h]);
var svg = d3.select("body").append("svg:svg").attr("width", w).attr("height", h);
d3.json("http://bl.ocks.org/d/1129492/readme.json", function(json) {
var link = svg.selectAll("line").data(json.links).enter().append("svg:line");
var node = svg.selectAll("circle").data(json.nodes).enter().append("svg:circle").attr("r", r - .75).style("fill", function(d) {
return fill(d.group);
}).style("stroke", function(d) {
return d3.rgb(fill(d.group)).darker();
}).call(force.drag).on("mouseover", fade(.1)).on("mouseout", fade(1));;
force.nodes(json.nodes).links(json.links).on("tick", tick).start();
function tick() {
node.attr("cx", function(d) {
return d.x = Math.max(r, Math.min(w - r, d.x));
}).attr("cy", function(d) {
return d.y = Math.max(r, Math.min(h - r, 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;
});
}
function fade(opacity) {
return function(d, i) {
//fade all elements
svg.selectAll("circle, line").style("opacity", opacity);
var associated_links = svg.selectAll("line").filter(function(d) {
return d.source.index == i || d.target.index == i;
}).each(function(dLink, iLink) {
//unfade links and nodes connected to the current node
d3.select(this).style("opacity", 1);
//THE FOLLOWING CAUSES: Uncaught TypeError: Cannot call method 'setProperty' of undefined
d3.select(dLink.source).style("opacity", 1);
...