D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
jorgeehernandez
Full window
Github gist
Treemap Padding
forked from
mbostock
's block:
Treemap Padding
<!DOCTYPE html> <meta charset="utf-8"> <style> .node { border: solid 1px white; font: 10px sans-serif; line-height: 12px; overflow: hidden; position: absolute; text-indent: 2px; } </style> <body> <script src="//d3js.org/d3.v3.min.js"></script> <script> var width = 960, height = 500; var color = d3.scale.category20c(); var treemap = d3.layout.treemap() .size([width, height]) .padding(4) .value(function(d) { return d.size; }); var div = d3.select("body").append("div") .style("position", "relative") .style("width", width + "px") .style("height", height + "px"); d3.json("flare.json", function(error, root) { if (error) throw error; //console.log(root) initialize(root); accumulate(root); layout(root); display(root); function display(d) { grandparent .datum(d.parent) .on("click", transition) .select("text") .text(name(d)) // color header based on grandparent's rate grandparent .datum(d.parent) .select("rect") .attr("fill", function(){console.log(color(d.rate)); return color(d['rate'])}) var g1 = svg.insert("g", ".grandparent") .datum(d) .attr("class", "depth"); var g = g1.selectAll("g") .data(d._children) .enter().append("g"); g.filter(function(d) { return d._children; }) .classed("children", true) .on("click", transition); g.selectAll(".child") .data(function(d) { return d._children || [d]; }) .enter().append("rect") .attr("class", "child") .call(rect); g.append("rect") .attr("class", "parent") .call(rect) .append("title") .text(function(d) {console.log(typeof(d.value), d.value); return d.name + ', Cases of TB: ' + d.value + ', percent change: ' + formatNumber(d.rate); }); g.append("text") .attr("dy", ".75em") .text(function(d) { return d.name; }) .call(text); function transition(d) { if (transitioning || !d) return; transitioning = true; var g2 = display(d), t1 = g1.transition().duration(750), t2 = g2.transition().duration(750); // Update the domain only after entering new elements. x.domain([d.x, d.x + d.dx]); y.domain([d.y, d.y + d.dy]); // Enable anti-aliasing during the transition. svg.style("shape-rendering", null); // Draw child nodes on top of parent nodes. svg.selectAll(".depth").sort(function(a, b) { return a.depth - b.depth; }); // Fade-in entering text. g2.selectAll("text").style("fill-opacity", 0); // Transition to the new view. t1.selectAll("text").call(text).style("fill-opacity", 0); t2.selectAll("text").call(text).style("fill-opacity", 1); t1.selectAll("rect").call(rect); t2.selectAll("rect").call(rect); // Remove the old node when the transition is finished. t1.remove().each("end", function() { svg.style("shape-rendering", "crispEdges"); transitioning = false; }); } return g; } function text(text) { text.attr("x", function(d) { return x(d.x) + 6; }) .attr("y", function(d) { return y(d.y) + 6; }) .attr("fill", function (d) {return getContrast50(color(parseFloat(d.rate)))}); } function rect(rect) { rect.attr("x", function(d) { return x(d.x); }) .attr("y", function(d) { return y(d.y); }) .attr("width", function(d) { return x(d.x + d.dx) - x(d.x); }) .attr("height", function(d) { return y(d.y + d.dy) - y(d.y); }) .attr("fill", function(d){return color(parseFloat(d.rate));}); } function name(d) { return d.parent ? name(d.parent) + "." + d.name : d.name; } }); </script>
https://d3js.org/d3.v3.min.js