Last active
January 22, 2016 20:03
-
-
Save yan2014/29423f366e6081120aee to your computer and use it in GitHub Desktop.
Directional Force layout
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 | value | |
---|---|---|---|
step 1.1 | step 2.1 | 1 | |
step 2.1 | step 2.2 | 1 | |
step 2.2 | step 2.2 | 1 | |
step 2.2 | step 3.1 | 1 | |
step 3.1 | step 2.2 | 1 | |
step 2.2 | step 3.1 | 1 | |
step 3.1 | step 4.1 | 1 | |
step 4.1 | step 4.2 | 1 | |
step 4.2 | step 4.2 | 1 | |
step 4.2 | step 4.3 | 1 | |
step 4.3 | step 4.4 | 1 | |
step 4.4 | step 1.1 | 1 | |
step 1.1 | step 4.2 | 1 | |
step 4.2 | step 4.3 | 1 | |
step 4.3 | step 4.1 | 1 | |
step 4.1 | step 4.4 | 1 | |
step 4.4 | step 4.5 | 1 | |
step 4.5 | step 4.6 | 1 | |
step 4.6 | step 5.1 | 1 | |
step 5.1 | step 5.2 | 1 | |
step 5.2 | step 5.3 | 1 | |
step 5.3 | step 5.4 | 1 | |
step 5.4 | step 1.1 | 1 | |
step 1.1 | step 1.1 | 1 | |
step 1.1 | step 2.2 | 1 | |
step 2.2 | step 2.1 | 1 | |
step 2.1 | step 2.2 | 1 | |
step 2.2 | step 2.2 | 1 | |
step 2.2 | step 3.1 | 1 | |
step 3.1 | step 4.1 | 1 | |
step 4.1 | step 4.2 | 1 | |
step 4.2 | step 4.1 | 1 | |
step 4.1 | step 4.2 | 1 | |
step 4.2 | step 4.3 | 1 | |
step 4.3 | step 4.5 | 1 | |
step 4.5 | step 4.6 | 1 | |
step 4.6 | step 5.3 | 1 | |
step 5.3 | step 5.2 | 1 | |
step 5.2 | step 5.1 | 1 |
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
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<script src="http://d3js.org/d3.v3.js"></script> | |
<style> | |
path.link { | |
fill: none; | |
stroke: #666; | |
stroke-width: 1.5px; | |
} | |
circle { | |
stroke: #fff; | |
stroke-width: 1.5px; | |
} | |
text { | |
fill: #000; | |
font: 10px sans-serif; | |
pointer-events: none; | |
} | |
</style> | |
<body> | |
<script> | |
// get the data | |
d3.csv("44394_1.csv", function(error, links) { | |
var nodes = {}; | |
// Compute the distinct nodes from the links. | |
links.forEach(function(link) { | |
link.source = nodes[link.source] || | |
(nodes[link.source] = {name: link.source}); | |
link.target = nodes[link.target] || | |
(nodes[link.target] = {name: link.target}); | |
link.value = +link.value; | |
}); | |
var width = 960, | |
height = 500; | |
var color = d3.scale.ordinal().range(["#F16745", "#FFC65D", "#FFC65D","#4CC3D9","#7BC8A4","#7BC8A4","#7BC8A4","#7BC8A4","#7BC8A4","#7BC8A4","#93648D","#93648D","#93648D","#93648D"]); | |
color.domain(["step 1.1", "step 2.1", "step 2.2","step 3.1","step 4.1","step 4.2","step 4.3","step 4.4","step 4.5","step 4.6","step 5.1","step 5.2","step 5.3","step 5.4"]); | |
var nodeSize=d3.scale.ordinal().range([6, 4, 12,6,8,10,6,4,4,4,4,4,4,2]); | |
nodeSize.domain(["step 1.1", "step 2.1", "step 2.2","step 3.1","step 4.1","step 4.2","step 4.3","step 4.4","step 4.5","step 4.6","step 5.1","step 5.2","step 5.3","step 5.4"]); | |
var force = d3.layout.force() | |
.nodes(d3.values(nodes)) | |
.links(links) | |
.size([width, height]) | |
.linkDistance(60) | |
.charge(-300) | |
.on("tick", tick) | |
.start(); | |
var svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height); | |
// build the arrow. | |
svg.append("svg:defs").selectAll("marker") | |
.data(["end"]) // Different link/path types can be defined here | |
.enter().append("svg:marker") // This section adds in the arrows | |
.attr("id", String) | |
.attr("viewBox", "0 -5 10 10") | |
.attr("refX", 15) | |
.attr("refY", -1.5) | |
.attr("markerWidth", 6) | |
.attr("markerHeight", 6) | |
.attr("orient", "auto") | |
.append("svg:path") | |
.attr("d", "M0,-5L10,0L0,5"); | |
// add the links and the arrows | |
var path = svg.append("svg:g").selectAll("path") | |
.data(force.links()) | |
.enter().append("svg:path") | |
// .attr("class", function(d) { return "link " + d.type; }) | |
.attr("class", "link") | |
.attr("marker-end", "url(#end)"); | |
// define the nodes | |
var node = svg.selectAll(".node") | |
.data(force.nodes()) | |
.enter().append("g") | |
.attr("class", "node") | |
.call(force.drag); | |
// add the nodes | |
node.append("circle") | |
.style("fill", function(d) { return color(d.name); }) | |
.attr("r", function(d) { return nodeSize(d.name);}); | |
// add the text | |
node.append("text") | |
.attr("x", 12) | |
.attr("dy", ".35em") | |
.text(function(d) { return d.name; }); | |
// add the curvy lines | |
function tick() { | |
path.attr("d", function(d) { | |
var dx = d.target.x - d.source.x, | |
dy = d.target.y - d.source.y, | |
dr = Math.sqrt(dx * dx + dy * dy); | |
return "M" + | |
d.source.x + "," + | |
d.source.y + "A" + | |
dr + "," + dr + " 0 0,1 " + | |
d.target.x + "," + | |
d.target.y; | |
}); | |
node | |
.attr("transform", function(d) { | |
return "translate(" + d.x + "," + d.y + ")"; }); | |
} | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment