D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
lindep
Full window
Github gist
treemap_hierarchy
Built with
blockbuilder.org
<!DOCTYPE html> <head> <meta charset="utf-8"> <script src="//d3js.org/d3.v4.min.js"></script> <style> body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } .node circle { fill: #999; } .node text { font: 10px sans-serif; } .node--internal circle { fill: #555; } .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: #555; stroke-opacity: 0.4; stroke-width: 1.5px; } </style> </head> <body> <svg width="450" height="208"></svg> <script> var treeSite ={ "name": "007-106", "children": [ {"name": "007-001"}, {"name": "007-002", "children": [ {"name": "006-001"}, {"name": "006-002"} ] }, {"name": "007-003", "children": [ {"name": "004-001"}, {"name": "004-002"} ] }, {"name": "007-004", "children": [ {"name": "005-001" } ] }, {"name": "007-005" } ] }; var svg = d3.select("body").select("svg"), width = +svg.attr("width"), height = +svg.attr("height"), g = svg.append("g").attr("transform", "translate(100,0)"), area = g.append("rect"); var treemap = d3.tree() .size([height, width - 237]); var nodes = d3.hierarchy(treeSite, function(d) { return d.children; }); nodes = treemap(nodes); var link = g.selectAll(".link") .data( nodes.descendants().slice(1)) .enter().append("path") .attr("class", "link") .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; }); 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 + ")"; }); node.append("circle") .attr("r", 2.5); node.append("text") .attr("dy", 3) .attr("x", function(d) { return d.children ? -8 : 8; }) .style("text-anchor", function(d) { return d.children ? "end" : "start"; }) .text(function(d) { return d.data.name; }); console.log("you are now rocking with d3", width,height); </script> </body>
https://d3js.org/d3.v4.min.js