Skip to content

Instantly share code, notes, and snippets.

@csenkbeil
Forked from mbostock/.block
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save csenkbeil/8d67bd3d21b778f0d2d8 to your computer and use it in GitHub Desktop.

Select an option

Save csenkbeil/8d67bd3d21b778f0d2d8 to your computer and use it in GitHub Desktop.
A force-directed Graph of Relationships

This simple force-directed graph show the relationships to Elizabeth Bennet. A physical simulation of charged particles and springs places related characters in closer proximity, while unrelated characters are farther apart. Layout algorithm inspired by Tim Dwyer and Thomas Jakobsen.

{
"nodes": [
{ "name": "Elizabeth Bennet", "group": 1 },
{ "name": "Jane Bennet", "group": 2 },
{ "name": "Mary Bennet", "group": 2 },
{ "name": "Kitty Bennet", "group": 2 },
{ "name": "Mr Bennet", "group": 3 },
{ "name": "Mrs Bennet", "group": 3 },
{ "name": "Lydia Bennet", "group": 2 },
{ "name": "Mrs Philips", "group": 4 },
{ "name": "Edward Gardiner", "group": 4 },
{ "name": "Mrs M. Gardiner", "group": 4 }
],
"links": [
{ "source": 0, "target": 1, "value": 1 },
{ "source": 0, "target": 2, "value": 1 },
{ "source": 0, "target": 3, "value": 1 },
{ "source": 0, "target": 6, "value": 1 },
{ "source": 4, "target": 5, "value": 2 },
{ "source": 4, "target": 0, "value": 3 },
{ "source": 5, "target": 0, "value": 3 },
{ "source": 5, "target": 7, "value": 4 },
{ "source": 5, "target": 8, "value": 4 },
{ "source": 5, "target": 9, "value": 4 },
{ "source": 8, "target": 9, "value": 2 }
]
}
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.node {
stroke: #fff;
stroke-width: 1.5px;
}
.link {
stroke-opacity: .6;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var width = 960,
height = 500;
var colorNode = d3.scale.category20(),
colorLink = d3.scale.category10();
var force = d3.layout.force()
.charge(-120)
.linkDistance(35)
.size([width, height]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
d3.json("family.json", function(error, graph) {
force
.nodes(graph.nodes)
.links(graph.links)
.start();
var link = svg.selectAll(".link")
.data(graph.links)
.enter().append("line")
.attr("class", "link")
.style("stroke-width", 2)
.style("stroke", function(d) { return colorLink(d.value); });
var node = svg.selectAll(".node")
.data(graph.nodes)
.enter().append("circle")
.attr("class", "node")
.attr("r", 7)
.style("fill", function(d) { return colorNode(d.group); })
.call(force.drag);
node.append("title")
.text(function(d) { return d.name; });
force.on("tick", function() {
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; });
node.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment