This is a tree diagram with images used for nodes written with d3.js v4. It is based on the styled tree version here
This is designed to be used as part of documenting an update to the book D3 Tips and Tricks to version 4 of d3.js.
license: gpl-3.0 |
This is a tree diagram with images used for nodes written with d3.js v4. It is based on the styled tree version here
This is designed to be used as part of documenting an update to the book D3 Tips and Tricks to version 4 of d3.js.
https://user-images.githubusercontent.com/3151700/69676149-14e75600-1098-11ea-8c27-5e90c7d871c6.png |
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> /* set the CSS */ | |
.node circle { | |
fill: #fff; | |
stroke: steelblue; | |
stroke-width: 3px; | |
} | |
.node text { font: 12px sans-serif; } | |
.node--internal text { | |
text-shadow: 0 1px 0 #fff, 0 -1px 0 #fff, 1px 0 0 #fff, -1px 0 0 #fff; | |
} | |
.link { | |
fill: none; | |
stroke: #ccc; | |
stroke-width: 2px; | |
} | |
</style> | |
<body> | |
<!-- load the d3.js library --> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<script> | |
var treeData = | |
{ | |
"name": "Top Level", | |
"value": 10, | |
"type": "black", | |
"level": "red", | |
"icon": "https://user-images.githubusercontent.com/3151700/69676618-06e60500-1099-11ea-8c5c-dcb70dae1cb8.png", | |
"children": [ | |
{ | |
"name": "Level 2: A", | |
"value": 5, | |
"type": "grey", | |
"level": "red", | |
"icon": "https://user-images.githubusercontent.com/3151700/69676149-14e75600-1098-11ea-8c27-5e90c7d871c6.png", | |
"children": [ | |
{ | |
"name": "Son of A", | |
"value": 5, | |
"type": "steelblue", | |
"icon": "https://user-images.githubusercontent.com/3151700/69676632-0baab900-1099-11ea-9304-4c18a1097a0c.png", | |
"level": "orange" | |
}, | |
{ | |
"name": "Daughter of A", | |
"value": 18, | |
"type": "steelblue", | |
"icon": "https://user-images.githubusercontent.com/3151700/69676652-17967b00-1099-11ea-8bba-b0a74909e8ab.png", | |
"level": "red" | |
} | |
] | |
}, | |
{ | |
"name": "Level 2: B", | |
"value": 10, | |
"type": "grey", | |
"icon": "https://user-images.githubusercontent.com/3151700/69676637-0fd6d680-1099-11ea-85ab-2a9e68229328.png", | |
"level": "green" | |
} | |
] | |
}; | |
// set the dimensions and margins of the diagram | |
var margin = {top: 20, right: 90, bottom: 30, left: 90}, | |
width = 660 - margin.left - margin.right, | |
height = 500 - margin.top - margin.bottom; | |
// declares a tree layout and assigns the size | |
var treemap = d3.tree() | |
.size([height, width]); | |
// assigns the data to a hierarchy using parent-child relationships | |
var nodes = d3.hierarchy(treeData, function(d) { | |
return d.children; | |
}); | |
// maps the node data to the tree layout | |
nodes = treemap(nodes); | |
// append the svg object to the body of the page | |
// appends a 'group' element to 'svg' | |
// moves the 'group' element to the top left margin | |
var svg = d3.select("body").append("svg") | |
.attr("width", width + margin.left + margin.right) | |
.attr("height", height + margin.top + margin.bottom), | |
g = svg.append("g") | |
.attr("transform", | |
"translate(" + margin.left + "," + margin.top + ")"); | |
// adds the links between the nodes | |
var link = g.selectAll(".link") | |
.data( nodes.descendants().slice(1)) | |
.enter().append("path") | |
.attr("class", "link") | |
.style("stroke", function(d) { return d.data.level; }) | |
.attr("d", function(d) { | |
return "M" + d.y + "," + d.x | |
+ "C" + (d.y + d.parent.y) / 2 + "," + d.x | |
+ " " + (d.y + d.parent.y) / 2 + "," + d.parent.x | |
+ " " + d.parent.y + "," + d.parent.x; | |
}); | |
// adds each node as a group | |
var node = g.selectAll(".node") | |
.data(nodes.descendants()) | |
.enter().append("g") | |
.attr("class", function(d) { | |
return "node" + | |
(d.children ? " node--internal" : " node--leaf"); }) | |
.attr("transform", function(d) { | |
return "translate(" + d.y + "," + d.x + ")"; }); | |
// adds images as nodes | |
node.append("image") | |
.attr("xlink:href", function(d) { return d.data.icon; }) | |
.attr("x", "-12px") | |
.attr("y", "-12px") | |
.attr("width", "24px") | |
.attr("height", "24px"); | |
// adds the text to the node | |
node.append("text") | |
.attr("dy", ".35em") | |
.attr("x", function(d) { return d.children ? -13 : 13; }) | |
.style("text-anchor", function(d) { | |
return d.children ? "end" : "start"; }) | |
.text(function(d) { return d.data.name; }); | |
</script> | |
</body> |
This will not work with data urls if you are generating icons on the fly.