forked from hsuh's block: Force layout constrained
Last active
February 18, 2016 14:51
-
-
Save bjtucker/cdfecd9d13bb54edd1c2 to your computer and use it in GitHub Desktop.
Force layout constrained
This file contains hidden or 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
var width= 960; var height = 500; | |
var color = d3.scale.category20(); | |
var cola = cola.d3adaptor() | |
.linkDistance(200) | |
.avoidOverlaps(true) | |
.size([width,height]) | |
var svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height); | |
d3.json("graph_data.json", function(error, graph) { | |
cola.nodes(graph.nodes) | |
.links(graph.links) | |
.constraints(graph.constraints) | |
.start(); | |
graph.nodes.forEach(function(d) { | |
d.width = d.width||60; | |
d.height = d.height||30; | |
}); | |
var link = svg.selectAll(".link") | |
.data(graph.links) | |
.enter().append("line") | |
.attr("class", "link"); | |
var node = svg.selectAll(".node") | |
.data(graph.nodes) | |
.enter().append("rect") | |
.attr("class", "node") | |
.attr("width", function (d) { return d.width; }) | |
.attr("height", function (d) { return d.height; }) | |
.attr("rx", 5).attr("ry", 5) | |
.style("fill", function (d) { return color(1); }) | |
.call(cola.drag); | |
var label = svg.selectAll(".label") | |
.data(graph.nodes) | |
.enter().append("text") | |
.attr("class", "label") | |
.text(function (d) { return d.name; }) | |
.call(cola.drag); | |
node.append("title") | |
.text(function (d) { return d.name; }); | |
cola.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("x", function (d) { return d.x - d.width / 2; }) | |
.attr("y", function (d) { return d.y - d.height / 2; }); | |
label.attr("x", function (d) { return d.x; }) | |
.attr("y", function (d) { | |
var h = this.getBBox().height; | |
return d.y + h/4; | |
}); | |
}); | |
}); |
This file contains hidden or 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
{ | |
"nodes":[ | |
{"name":"A"}, | |
{"name":"B"}, | |
{"name":"C"}, | |
{"name":"D"}, | |
{"name":"E"}, | |
{"name":"F"}, | |
{"name":"G"}, | |
{"name":"H"} | |
], | |
"links":[ | |
{"source":0,"target":1}, | |
{"source":0,"target":5}, | |
{"source":0,"target":6}, | |
{"source":0,"target":7}, | |
{"source":1,"target":2}, | |
{"source":2,"target":0}, | |
{"source":2,"target":3} | |
], | |
"constraints":[ | |
{"type":"alignment", | |
"axis":"x", | |
"offsets":[ | |
{"node":"1", "offset":"0"}, | |
{"node":"2", "offset":"0"}, | |
{"node":"3", "offset":"0"} | |
]}, | |
{"type":"alignment", | |
"axis":"y", | |
"offsets":[ | |
{"node":"0", "offset":"0"}, | |
{"node":"1", "offset":"0"}, | |
{"node":"4", "offset":"0"} | |
]} | |
] | |
} |
This file contains hidden or 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"> | |
<html> | |
<head> | |
<link rel="stylesheet" href="style.css"> | |
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script> | |
<script src="http://marvl.infotech.monash.edu/webcola/cola.v2.min.js"></script> | |
</head> | |
<body> | |
<h1> Force layout constrained </h1> | |
<script src="d3cola.js"></script> | |
</body> | |
</html> |
This file contains hidden or 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
.node { | |
stroke: #fff; | |
stroke-width: 1.5px; | |
cursor: move; | |
} | |
.link { | |
stroke: #999; | |
stroke-width: 3px; | |
stroke-opacity: 1; | |
} | |
.label { | |
fill: white; | |
font-family: Verdana; | |
font-size: 25px; | |
text-anchor: middle; | |
cursor: move; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment