Skip to content

Instantly share code, notes, and snippets.

@ajmistu
Last active June 28, 2017 15:39
Show Gist options
  • Save ajmistu/d5903d4ace218bbc19ec0104a99cb8cd to your computer and use it in GitHub Desktop.
Save ajmistu/d5903d4ace218bbc19ec0104a99cb8cd to your computer and use it in GitHub Desktop.
Collapsible Force Layout (Sample 4 diagram w/ json file)
<!DOCTYPE html>
<meta charset="utf-8">
<title>Force-Directed Graph</title>
<style>
.node {
cursor: pointer;
stroke: #3182bd;
stroke-width: 1.5px;
}
.link {
fill: none;
stroke: #9ecae1;
stroke-width: 2.5px;
}
</style>
<body>
<p align="center"><b>Sample 4</b></p>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var width = 960,
height = 600,
root;
var force = d3.layout.force()
.size([width, height])
.on("tick", tick);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var link = svg.selectAll(".link"),
node = svg.selectAll(".node");
d3.json("Sample4.json", function(error, json) {
if (error) throw error;
root = json;
update();
});
function update() {
var nodes = flatten(root),
links = d3.layout.tree().links(nodes);
// Restart the force layout.
force
.nodes(nodes)
.links(links)
.start();
// Update the links…
link = link.data(links, function(d) { return d.target.id; });
// Exit any old links.
link.exit().remove();
// Enter any new links.
link.enter().insert("line", ".node")
.attr("class", "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; });
// Update the nodes…
node = node.data(nodes, function(d) { return d.id; }).style("fill", color);
// Exit any old nodes.
node.exit().remove();
// Enter any new nodes.
node.enter().append("circle")
.attr("class", "node")
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; })
.attr("r", function(d) { return Math.sqrt(d.size)*2 || 4.5; })
.style("fill", color)
.on("click", click)
.call(force.drag);
}
function tick() {
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; });
}
// Color leaf nodes orange, and packages white or blue.
function color(d) {
return d._children ? "#3182bd" : d.children ? "#c6dbef" : "#fd8d3c";
}
// Toggle children on click.
function click(d) {
if (!d3.event.defaultPrevented) {
if (d.children) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
}
update();
}
}
// Returns a list of all nodes under the root.
function flatten(root) {
var nodes = [], i = 0;
function recurse(node) {
if (node.children) node.children.forEach(recurse);
if (!node.id) node.id = ++i;
nodes.push(node);
}
recurse(root);
return nodes;
}
</script>
{
"name": "1",
"children": [
{
"name": "deer",
"size": 30
},
{
"name": "elk",
"size": 30
},
{
"name": "blackbuck",
"size": 30
},
{
"name": "2",
"children": [
{
"name": "pony",
"size": 30
},
{
"name": "mule",
"size": 30
},
{
"name": "male horse",
"size": 30
},{
"name": "3",
"children": [
{
"name": "warehorse",
"size": 30
},
{
"name": "4",
"children": [
{
"name": "goat",
"size": 30
},
{
"name": "5",
"children": [
{
"name": "sheep",
"size": 30
},
{
"name": "6",
"children": [
{
"name": "bison",
"size": 30
},
{
"name": "7",
"children": [
{
"name": "zebra",
"size": 30
}
]}
]}
]}
]
}
]
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment