This is the code for Chapter 6, Figure 8 from D3.js in Action showing how to create a force-directed network using d3.layout.force() as well as SVG markers for arrowheads.
Last active
March 18, 2016 03:31
-
-
Save emeeks/040c4eb87d36de3c87d3 to your computer and use it in GitHub Desktop.
Ch. 6, Fig. 8 - D3.js in Action
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source | target | weight | |
---|---|---|---|
sam | pris | 1 | |
roy | pris | 5 | |
roy | sam | 1 | |
tully | pris | 5 | |
tully | kim | 3 | |
tully | pat | 1 | |
tully | mo | 3 | |
kim | pat | 2 | |
kim | mo | 1 | |
mo | tully | 7 | |
mo | pat | 1 | |
mo | pris | 1 | |
pat | tully | 1 | |
pat | kim | 2 | |
pat | mo | 5 | |
lee | al | 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>D3 in Action Chapter 6 - Example 3</title> | |
<meta charset="utf-8" /> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script> | |
<script src="https://d3js.org/queue.v1.min.js" type="text/JavaScript"></script> | |
</head> | |
<style> | |
svg { | |
height: 500px; | |
width: 500px; | |
border: 1px solid gray; | |
} | |
</style> | |
<body> | |
<div id="viz"> | |
<svg> | |
</svg> | |
</div> | |
</body> | |
<footer> | |
<script> | |
var marker = d3.select("svg").append('defs') | |
.append('marker') | |
.attr("id", "Triangle") | |
.attr("refX", 12) | |
.attr("refY", 6) | |
.attr("markerUnits", 'userSpaceOnUse') | |
.attr("markerWidth", 12) | |
.attr("markerHeight", 18) | |
.attr("orient", 'auto') | |
.append('path') | |
.attr("d", 'M 0 0 12 6 0 12 3 6'); | |
queue() | |
.defer(d3.csv, "nodelist.csv") | |
.defer(d3.csv, "edgelist.csv") | |
.await(function(error, file1, file2) { createForceLayout(file1, file2); }); | |
function createForceLayout(nodes,edges) { | |
var nodeHash = {}; | |
for (x in nodes) { | |
nodeHash[nodes[x].id] = nodes[x]; | |
} | |
for (x in edges) { | |
edges[x].weight = parseInt(edges[x].weight); | |
edges[x].source = nodeHash[edges[x].source]; | |
edges[x].target = nodeHash[edges[x].target]; | |
} | |
// chargeScale = d3.scale.linear().domain(d3.extent(nodes, function(d) {return d.followers})).range([-500,-2000]) | |
// nodeSize = d3.scale.linear().domain(d3.extent(nodes, function(d) {return d.followers})).range([5,20]) | |
var weightScale = d3.scale.linear().domain(d3.extent(edges, function(d) {return d.weight})).range([.1,1]) | |
force = d3.layout.force() | |
// .charge(-1000) | |
.charge(function (d) {return d.weight * -500}) | |
.gravity(.3) | |
// .linkDistance(50) | |
// .linkStrength(function (d) {return weightScale(d.weight)}) | |
.size([500,500]).nodes(nodes) | |
.links(edges).on("tick", forceTick); | |
d3.select("svg").selectAll("line.link").data(edges, function (d) {return d.source.id + "-" + d.target.id}).enter() | |
.append("line") | |
.attr("class", "link") | |
.style("stroke", "black") | |
.style("opacity", .5) | |
.style("stroke-width", function(d) {return d.weight}); | |
var nodeEnter = d3.select("svg").selectAll("g.node").data(nodes, function (d) {return d.id}).enter() | |
.append("g") | |
.attr("class", "node") | |
.call(force.drag()) | |
.on("click", fixNode); | |
function fixNode(d) { | |
d3.select(this).select("circle").style("stroke-width", 4); | |
d.fixed = true; | |
} | |
nodeEnter.append("circle") | |
.attr("r", 5) | |
.style("fill", "lightgray") | |
.style("stroke", "black") | |
.style("stroke-width", "1px"); | |
nodeEnter.append("text") | |
.style("text-anchor", "middle") | |
.attr("y", 15) | |
.text(function(d) {return d.id}) | |
d3.selectAll("line").attr("marker-end", "url(#Triangle)"); | |
force.start(); | |
function forceTick() { | |
d3.selectAll("line.link") | |
.attr("x1", function (d) {return d.source.x}) | |
.attr("x2", function (d) {return d.target.x}) | |
.attr("y1", function (d) {return d.source.y}) | |
.attr("y2", function (d) {return d.target.y}); | |
d3.selectAll("g.node") | |
.attr("transform", function (d) {return "translate("+d.x+","+d.y+")"}) | |
} | |
} | |
</script> | |
</footer> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
id | followers | following | |
---|---|---|---|
sam | 17 | 500 | |
roy | 83 | 80 | |
pris | 904 | 15 | |
tully | 7 | 5 | |
kim | 11 | 50 | |
mo | 80 | 85 | |
pat | 150 | 300 | |
lee | 38 | 7 | |
al | 12 | 12 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment