Skip to content

Instantly share code, notes, and snippets.

@githoov githoov/index.html
Last active Aug 24, 2018

Embed
What would you like to do?
hierarchy
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Tree Example</title>
<style>
.node {
cursor: pointer;
}
.node circle {
fill: #fff;
stroke: steelblue;
stroke-width: 3px;
}
.node text {
font: 12px sans-serif;
}
.link {
fill: none;
stroke: #ccc;
stroke-width: 2px;
}
</style>
</head>
<body>
<!-- load the d3.js library -->
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var treeData = [
{
"id":"-1",
"children":[
{
"id":"122124",
"children":[
{
"id":"122144"
},
{
"id":"122148"
},
{
"id":"122156"
},
{
"id":"122160"
},
{
"id":"122164",
"children":[
{
"id":"122220"
},
{
"id":"122224"
},
{
"id":"122248"
},
{
"id":"122256"
},
{
"id":"122260"
},
{
"id":"122264"
},
{
"id":"122268"
},
{
"id":"122272"
},
{
"id":"122280"
},
{
"id":"122288"
},
{
"id":"122292"
},
{
"id":"122300"
},
{
"id":"122304"
},
{
"id":"122308"
},
{
"id":"122312"
},
{
"id":"122316"
},
{
"id":"122320"
},
{
"id":"31260933"
},
{
"id":"31260937"
},
{
"id":"31260945"
},
{
"id":"31260949"
},
{
"id":"31260953",
"children":[
{
"id":"31261165"
},
{
"id":"31261169"
},
{
"id":"31261181"
},
{
"id":"31261217"
}
]
},
{
"id":"31260957"
},
{
"id":"31260961"
},
{
"id":"31260965"
},
{
"id":"31260969"
},
{
"id":"31260973"
},
{
"id":"31260993"
},
{
"id":"31260997"
},
{
"id":"31261001"
},
{
"id":"31261005"
},
{
"id":"31261009"
},
{
"id":"31261013"
},
{
"id":"31261017"
},
{
"id":"31261021",
"children":[
{
"id":"31261037"
},
{
"id":"31261045"
},
{
"id":"31261093"
},
{
"id":"31261097"
},
{
"id":"31261109"
},
{
"id":"31261113"
},
{
"id":"31261133"
},
{
"id":"31261149"
},
{
"id":"31261153"
},
{
"id":"31261189"
},
{
"id":"31261193"
},
{
"id":"31261201"
},
{
"id":"31261205"
},
{
"id":"31261225"
},
{
"id":"31261229"
},
{
"id":"31261233"
},
{
"id":"31261237"
},
{
"id":"31261241"
},
{
"id":"31261245"
},
{
"id":"31261249"
},
{
"id":"31261253"
},
{
"id":"31261257"
},
{
"id":"31261265"
},
{
"id":"31261269"
},
{
"id":"31261273"
},
{
"id":"31261277"
},
{
"id":"31261281"
},
{
"id":"31261285"
},
{
"id":"31261289"
},
{
"id":"31261297"
},
{
"id":"31261301"
},
{
"id":"31261305"
},
{
"id":"31261313"
},
{
"id":"31261317"
},
{
"id":"31261321"
},
{
"id":"31261329"
},
{
"id":"31261333"
},
{
"id":"31261341"
},
{
"id":"31261345"
},
{
"id":"31261349"
},
{
"id":"31261353"
},
{
"id":"31261357"
},
{
"id":"31261361"
},
{
"id":"31261365"
},
{
"id":"31261369"
},
{
"id":"31261373"
},
{
"id":"31261377"
},
{
"id":"31261381"
},
{
"id":"31261385"
},
{
"id":"31261393"
},
{
"id":"31261397"
},
{
"id":"31261401"
},
{
"id":"31261405"
},
{
"id":"31261409"
},
{
"id":"31261413"
},
{
"id":"31261417"
},
{
"id":"31261421"
},
{
"id":"31261425"
},
{
"id":"31261429"
},
{
"id":"31261433"
},
{
"id":"31261437"
},
{
"id":"31261441"
},
{
"id":"31261445"
},
{
"id":"31261449"
},
{
"id":"31261453"
},
{
"id":"31261457"
},
{
"id":"31261461"
},
{
"id":"31261465"
},
{
"id":"31261469"
},
{
"id":"31261473"
},
{
"id":"31261477"
},
{
"id":"31261481"
},
{
"id":"31261485"
},
{
"id":"31261489"
},
{
"id":"31261493"
},
{
"id":"31261497"
},
{
"id":"31261501"
},
{
"id":"31261505"
},
{
"id":"31261509"
},
{
"id":"31261513"
},
{
"id":"31261517"
},
{
"id":"31261521"
},
{
"id":"31261525"
},
{
"id":"31261529"
},
{
"id":"31261533"
},
{
"id":"31261541"
},
{
"id":"31261545"
},
{
"id":"31261549"
},
{
"id":"31261557"
},
{
"id":"31261561"
},
{
"id":"31261565"
},
{
"id":"31261569",
"children":[
{
"id":"31261685"
},
{
"id":"31261797"
},
{
"id":"31261801"
},
{
"id":"31261817"
}
]
},
{
"id":"31261573"
},
{
"id":"31261577"
},
{
"id":"31261581"
},
{
"id":"31261585"
},
{
"id":"31261589"
},
{
"id":"31261593"
},
{
"id":"31261597"
},
{
"id":"31261601"
},
{
"id":"31261605"
},
{
"id":"31261609"
},
{
"id":"31261613",
"children":[
{
"id":"31261617"
},
{
"id":"31261625"
},
{
"id":"31261629"
},
{
"id":"31261633"
},
{
"id":"31261637"
},
{
"id":"31261641"
},
{
"id":"31261645"
},
{
"id":"31261649"
},
{
"id":"31261657"
},
{
"id":"31261661"
},
{
"id":"31261665"
}
]
},
{
"id":"31261621"
},
{
"id":"31261653"
},
{
"id":"31261669",
"children":[
{
"id":"31261713"
},
{
"id":"31261717"
},
{
"id":"31261721"
},
{
"id":"31261733"
},
{
"id":"31261741"
},
{
"id":"31261745"
},
{
"id":"31261853"
}
]
},
{
"id":"31261673"
},
{
"id":"31261677",
"children":[
{
"id":"31261681"
},
{
"id":"31261689"
},
{
"id":"31261693"
},
{
"id":"31261705"
},
{
"id":"31261729"
},
{
"id":"31261737"
},
{
"id":"31261749"
},
{
"id":"31261753"
},
{
"id":"31261757"
},
{
"id":"31261785"
},
{
"id":"31261789"
},
{
"id":"31261805"
},
{
"id":"31261809"
},
{
"id":"31261821"
},
{
"id":"31261825"
},
{
"id":"31261829"
},
{
"id":"31261833"
},
{
"id":"31261837"
},
{
"id":"31261841"
},
{
"id":"31261845"
},
{
"id":"31261849"
}
]
},
{
"id":"31261697"
},
{
"id":"31261701"
},
{
"id":"31261709"
},
{
"id":"31261725"
},
{
"id":"31261761"
},
{
"id":"31261765"
},
{
"id":"31261769"
},
{
"id":"31261773"
},
{
"id":"31261777"
},
{
"id":"31261781"
},
{
"id":"31261793"
},
{
"id":"31261813"
}
]
},
{
"id":"31261025"
},
{
"id":"31261029"
},
{
"id":"31261033"
},
{
"id":"31261053"
},
{
"id":"31261057"
},
{
"id":"31261061"
},
{
"id":"31261073"
},
{
"id":"31261077"
},
{
"id":"31261085"
},
{
"id":"31261117"
},
{
"id":"31261121"
},
{
"id":"31261125"
},
{
"id":"31261161"
},
{
"id":"31261173"
},
{
"id":"31261177"
},
{
"id":"31261185"
},
{
"id":"31261197"
},
{
"id":"31261209"
},
{
"id":"31261213"
},
{
"id":"31261221"
},
{
"id":"31261309"
},
{
"id":"31261325"
},
{
"id":"31261337"
},
{
"id":"31261389"
},
{
"id":"31261553"
}
]
},
{
"id":"122168"
},
{
"id":"122172"
},
{
"id":"122176"
},
{
"id":"122180"
},
{
"id":"122184"
},
{
"id":"122192"
},
{
"id":"122196"
},
{
"id":"122200"
},
{
"id":"122204"
},
{
"id":"122212",
"children":[
{
"id":"31261157"
},
{
"id":"31261293"
}
]
},
{
"id":"122228"
},
{
"id":"122232"
},
{
"id":"122236"
},
{
"id":"122240"
},
{
"id":"122244"
},
{
"id":"122252"
},
{
"id":"122276"
},
{
"id":"122284"
},
{
"id":"122296"
},
{
"id":"122324"
},
{
"id":"122328"
},
{
"id":"122332"
},
{
"id":"122336"
},
{
"id":"122340"
},
{
"id":"122344"
},
{
"id":"122348"
},
{
"id":"122352"
},
{
"id":"122356"
},
{
"id":"122360"
},
{
"id":"122364"
},
{
"id":"31260929"
},
{
"id":"31260941"
},
{
"id":"31260977"
},
{
"id":"31260981"
},
{
"id":"31260985"
},
{
"id":"31260989"
},
{
"id":"31261041"
},
{
"id":"31261049"
},
{
"id":"31261065"
},
{
"id":"31261069"
},
{
"id":"31261081"
},
{
"id":"31261089"
},
{
"id":"31261101"
},
{
"id":"31261105"
},
{
"id":"31261129"
},
{
"id":"31261137"
},
{
"id":"31261141"
},
{
"id":"31261145"
},
{
"id":"31261261"
},
{
"id":"31261537"
}
]
}
]
}
];
// ************** Generate the tree diagram *****************
var margin = {top: 20, right: 240, bottom: 20, left: 240},
width = 960 - margin.right - margin.left,
height = 500 - margin.top - margin.bottom;
var i = 0,
duration = 750,
root;
var tree = d3.layout.tree()
.size([height, width]);
var diagonal = d3.svg.diagonal()
.projection(function(d) { return [d.y, d.x]; });
var svg = d3.select("body").append("svg")
.attr("width", width + margin.right + margin.left)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
root = treeData[0];
root.x0 = height / 2;
root.y0 = 0;
update(root);
d3.select(self.frameElement).style("height", "500px");
function update(source) {
// Compute the new tree layout.
var nodes = tree.nodes(root).reverse(),
links = tree.links(nodes);
// Normalize for fixed-depth.
nodes.forEach(function(d) { d.y = d.depth * 180; });
// Update the nodes…
var node = svg.selectAll("g.node")
.data(nodes, function(d) { return d.id || (d.id = ++i); });
// Enter any new nodes at the parent's previous position.
var nodeEnter = node.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; })
.on("click", click);
nodeEnter.append("circle")
.attr("r", 1e-6)
.style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });
nodeEnter.append("text")
.attr("x", function(d) { return d.children || d._children ? -13 : 13; })
.attr("dy", ".35em")
.attr("text-anchor", function(d) { return d.children || d._children ? "end" : "start"; })
.text(function(d) { return d.id; })
.style("fill-opacity", 1e-6);
// Transition nodes to their new position.
var nodeUpdate = node.transition()
.duration(duration)
.attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; });
nodeUpdate.select("circle")
.attr("r", 10)
.style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });
nodeUpdate.select("text")
.style("fill-opacity", 1);
// Transition exiting nodes to the parent's new position.
var nodeExit = node.exit().transition()
.duration(duration)
.attr("transform", function(d) { return "translate(" + source.y + "," + source.x + ")"; })
.remove();
nodeExit.select("circle")
.attr("r", 1e-6);
nodeExit.select("text")
.style("fill-opacity", 1e-6);
// Update the links…
var link = svg.selectAll("path.link")
.data(links, function(d) { return d.target.id; });
// Enter any new links at the parent's previous position.
link.enter().insert("path", "g")
.attr("class", "link")
.attr("d", function(d) {
var o = {x: source.x0, y: source.y0};
return diagonal({source: o, target: o});
});
// Transition links to their new position.
link.transition()
.duration(duration)
.attr("d", diagonal);
// Transition exiting nodes to the parent's new position.
link.exit().transition()
.duration(duration)
.attr("d", function(d) {
var o = {x: source.x, y: source.y};
return diagonal({source: o, target: o});
})
.remove();
// Stash the old positions for transition.
nodes.forEach(function(d) {
d.x0 = d.x;
d.y0 = d.y;
});
}
// Toggle children on click.
function click(d) {
if (d.children) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
}
update(d);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.