This is a simple d3.js tree diagram, arranged so that the tree is vertical, as used as an example in the book D3 Tips and Tricks.
It is derived from the Mike Bostock Collapsible tree example but it is a VERY cut down version without the ability to update (collapse).
forked from d3noob's block: Simple vertical d3.js tree diagram
forked from anonymous's block: Simple vertical d3.js tree diagram
forked from anonymous's block: Simple vertical d3.js tree diagram
forked from anonymous's block: Simple vertical d3.js tree diagram
forked from anonymous's block: Simple vertical d3.js tree diagram
xxxxxxxxxx
<html lang="en">
<head>
<meta charset="utf-8">
<title>Collapsible Tree Example</title>
<style>
.node circle {
fill: #fff;
stroke: red;
stroke-width: 2px;
}
.node text { font: 12px sans-serif; }
.link {
fill: none;
stroke: #ccc;
stroke-width: 2px;
}
</style>
</head>
<body>
<!-- load the d3.js library -->
<script src="https://d3js.org/d3.v3.min.js"></script>
<script>
var treeData = [
{
"name": "United States of America",
"parent": "null",
"children": [
{
"name": "States",
"parent": "United States of America",
"children": [
{
"name": "Counties",
"parent": "States"
},
{
"name": "Places",
"parent": "States"
}
]
},
{
"name": "Zip Codes (ZCTA)",
"parent": "United States of America"
},
{
"name": "114th CD",
"parent": "United States of America"
}
]
}
];
// ************** Generate the tree diagram *****************
var margin = {top: 40, right: 20, bottom: 20, left: 20},
width = 500 - margin.right - margin.left,
height = 600 - margin.top - margin.bottom;
var i = 0;
var tree = d3.layout.tree()
.size([height, width]);
var diagonal = d3.svg.diagonal()
.projection(function(d) { return [d.x, d.y]; });
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];
update(root);
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 * 100; });
// Declare the nodes…
var node = svg.selectAll("g.node")
.data(nodes, function(d) { return d.id || (d.id = ++i); });
// Enter the nodes.
var nodeEnter = node.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")"; });
nodeEnter.append("circle")
.attr("r", 5)
.style("fill", "#fff");
nodeEnter.append("text")
.attr("y", function(d) {
return d.children || d._children ? -20 : 20; })
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.text(function(d) { return d.name; })
.style("fill-opacity", 1);
// Declare the links…
var link = svg.selectAll("path.link")
.data(links, function(d) { return d.target.id; });
// Enter the links.
link.enter().insert("path", "g")
.attr("class", "link")
.attr("d", diagonal);
}
</script>
</body>
</html>
Modified http://d3js.org/d3.v3.min.js to a secure url
https://d3js.org/d3.v3.min.js