This “icicle” diagram uses d3.layout.partition to divide space with area proportional to the value of nodes in a tree. See also the zoomable icicle.
| <!DOCTYPE html> | |
| <meta charset="utf-8"> | |
| <title>Partition - Icicle</title> | |
| <style> | |
| .node { | |
| fill: #ddd; | |
| stroke: #fff; | |
| } | |
| .label { | |
| font: 10px sans-serif; | |
| text-anchor: middle; | |
| } | |
| </style> | |
| <body> | |
| <script src="//d3js.org/d3.v3.min.js"></script> | |
| <script> | |
| var width = 960, | |
| height = 500; | |
| var color = d3.scale.category20(); | |
| var svg = d3.select("body").append("svg") | |
| .attr("width", width) | |
| .attr("height", height); | |
| var partition = d3.layout.partition() | |
| .size([width, height]) | |
| .value(function(d) { return d.size; }); | |
| d3.json("/mbostock/raw/4063550/flare.json", function(error, root) { | |
| if (error) throw error; | |
| var nodes = partition.nodes(root); | |
| svg.selectAll(".node") | |
| .data(nodes) | |
| .enter().append("rect") | |
| .attr("class", "node") | |
| .attr("x", function(d) { return d.x; }) | |
| .attr("y", function(d) { return d.y; }) | |
| .attr("width", function(d) { return d.dx; }) | |
| .attr("height", function(d) { return d.dy; }) | |
| .style("fill", function(d) { return color((d.children ? d : d.parent).name); }); | |
| svg.selectAll(".label") | |
| .data(nodes.filter(function(d) { return d.dx > 6; })) | |
| .enter().append("text") | |
| .attr("class", "label") | |
| .attr("dy", ".35em") | |
| .attr("transform", function(d) { return "translate(" + (d.x + d.dx / 2) + "," + (d.y + d.dy / 2) + ")rotate(90)"; }) | |
| .text(function(d) { return d.name; }); | |
| }); | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment