A visualization of files in d3-hierarchy, based on Radial Tidy Tree.
Use git to clone a repository, then du to create a tsv with the directory contents.
git clone https://github.com/d3/d3-hierarchy.git
(echo -n 'size\tfile\n'; du -a d3-hierarchy/*) > d3-hierarchy.tsv
An updated version of the burrow
function from d3 src tree in plain JavaScript. This function takes an array of keys to generate a hierarchy.
Compared to d3.nest
: burrow
allows for branches of arbitrary depth.
Compared to d3.stratify
: parent nodes do not need to be specified as separate rows in the tabular data. Parents are created automatically as they are encountered.
forked from syntagmatic's block: d3-hierarchy tree
xxxxxxxxxx
<meta charset="utf-8">
<style>
.node rect {
fill: #fafafa;
stroke: #ddd;
width: 30px;
height: 2em;
x: -15px;
y: -1em;
}
.node text {
font: 10px sans-serif;
}
.node--internal circle {
fill: #555;
}
.node text {
text-shadow: 0 1px 0 #fff, 0 -1px 0 #fff, 1px 0 0 #fff, -1px 0 0 #fff;
}
.link {
fill: none;
stroke: #555;
stroke-opacity: 0.2;
/* stroke-width: 1.5px; */
}
</style>
<body>
<script src="//d3js.org/d3.v4.0.0-alpha.29.js"></script>
<script>
var width = 960,
height = 500,
radius = 250,
curve = 0.5;
var burrow = function(table) {
// create nested object
var obj = {};
table.forEach(function(row) {
// start at root
var layer = obj;
// create children as nested objects
row.taxonomy.forEach(function(key) {
layer[key] = key in layer ? layer[key] : {};
layer = layer[key];
});
});
console.log(obj);
// recursively create children array
var descend = function(obj, depth) {
var arr = [];
var depth = depth || 0;
for (var k in obj) {
var child = {
name: k,
depth: depth,
children: descend(obj[k], depth+1),
};
arr.push(child);
}
return arr;
};
// use descend to create nested children arrys
return {
name: "root",
children: descend(obj, 1),
depth: 0
}
};
var tree = d3.tree()
.size([width-40,height-40])
.separation(function(a, b) { return (a.parent == b.parent ? 1 : 2)/2; });
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + 20 + "," + 20 + ")");
d3.tsv("d3-hierarchy.tsv", function(error, data) {
if (error) throw error;
// splitting keys into an array is required before passing data into burrow
data.forEach(function(row) {
row.taxonomy = row.file.split("/");
});
var root = d3.hierarchy(burrow(data));
console.log(root);
tree(root);
var link = svg.selectAll(".link")
.data(root.descendants().slice(1))
.enter().append("path")
.attr("class", "link")
.attr("stroke-width", function(d){return d.value})
.attr("d", function(d) {
return "M" + [d.x, d.y]
+ "C" + [d.x, (d.y + d.parent.y) * curve]
+ " " + [d.parent.x, (d.y + d.parent.y) * (1-curve)]
+ " " + [d.parent.x, d.parent.y];
});
var node = svg.selectAll(".node")
.data(root.descendants())
.enter().append("g")
.attr("class", function(d) { return "node" + (d.children ? " node--internal" : " node--leaf"); })
.attr("transform", function(d) { return "translate(" + d.x + "," + (d.y) + ")" });
node.append("rect");
node.append("text")
.text(function(d) { return d.data.name + d.data; });
});
</script>
https://d3js.org/d3.v4.0.0-alpha.29.js