D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
newsroomdev
Full window
Github gist
Nested Treemap
forked from
mbostock
's block:
Nested Treemap
<!DOCTYPE html> <meta charset="utf-8"> <style> text { font: 10px sans-serif; } tspan:last-child { font-size: 9px; fill-opacity: 0.8; } .node rect { shape-rendering: crispEdges; } .node--hover rect { stroke: #000; } </style> <svg width="960" height="1060"></svg> <script src="//d3js.org/d3.v4.0.0-alpha.35.min.js"></script> <script> var svg = d3.select("svg"), width = +svg.attr("width"), height = +svg.attr("height"); var format = d3.format(",d"); var color = d3.scaleCool() .domain([0, 15]); var stratify = d3.stratify() .parentId(function(d) { return d.id.substring(0, d.id.lastIndexOf(".")); }); var treemap = d3.treemap() .size([width, height]) .paddingOuter(3) .paddingTop(19) .paddingInner(1) .round(true); d3.csv("flare.csv", function(error, data) { if (error) throw error; var root = stratify(data) .sum(function(d) { return d.value; }) .sort(function(a, b) { return b.height - a.height || b.value - a.value; }); treemap(root); var cell = svg .selectAll(".node") .data(root.descendants()) .enter().append("g") .attr("transform", function(d) { return "translate(" + d.x0 + "," + d.y0 + ")"; }) .attr("class", "node") .each(function(d) { d.node = this; }) .on("mouseover", hovered(true)) .on("mouseout", hovered(false)); cell.append("rect") .attr("id", function(d) { return "rect-" + d.id; }) .attr("width", function(d) { return d.x1 - d.x0; }) .attr("height", function(d) { return d.y1 - d.y0; }) .style("fill", function(d) { return color(d.depth); }); cell.append("clipPath") .attr("id", function(d) { return "clip-" + d.id; }) .append("use") .attr("xlink:href", function(d) { return "#rect-" + d.id + ""; }); var label = cell.append("text") .attr("clip-path", function(d) { return "url(#clip-" + d.id + ")"; }); label .filter(function(d) { return d.children; }) .selectAll("tspan") .data(function(d) { return d.id.substring(d.id.lastIndexOf(".") + 1).split(/(?=[A-Z][^A-Z])/g).concat("\xa0" + format(d.value)); }) .enter().append("tspan") .attr("x", function(d, i) { return i ? null : 4; }) .attr("y", 13) .text(function(d) { return d; }); label .filter(function(d) { return !d.children; }) .selectAll("tspan") .data(function(d) { return d.id.substring(d.id.lastIndexOf(".") + 1).split(/(?=[A-Z][^A-Z])/g).concat(format(d.value)); }) .enter().append("tspan") .attr("x", 4) .attr("y", function(d, i) { return 13 + i * 10; }) .text(function(d) { return d; }); cell.append("title") .text(function(d) { return d.id + "\n" + format(d.value); }); }); function hovered(hover) { return function(d) { d3.selectAll(d.ancestors().map(function(d) { return d.node; })) .classed("node--hover", hover) .select("rect") .attr("width", function(d) { return d.x1 - d.x0 - hover; }) .attr("height", function(d) { return d.y1 - d.y0 - hover; }); }; } </script>
https://d3js.org/d3.v4.0.0-alpha.35.min.js