A treemap recursively subdivides area into rectangles; the area of any node in the tree corresponds to its value. This example uses color to encode different packages of the Flare visualization toolkit. Treemap design invented by Ben Shneiderman. Squarified algorithm by Bruls, Huizing and van Wijk. Data courtesy Jeff Heer.
forked from mbostock's block: Treemap
forked from me1er's block: Treemap v4 hirarchical zoomable
xxxxxxxxxx
<style>
form {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
svg {
font: 10px sans-serif;
}
</style>
<svg width="350" height="200"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height"),
x = d3.scaleLinear().range([0, width]),
y = d3.scaleLinear().range([0, height]),
node,
root;
var wupColorScale = d3.scaleOrdinal()
.domain(["Coop","Migros","Andere","Eugen Müller","Aldi","Hans Huber"])
//.range(["#EA4424", "#008DD2", "#00A859", "#FDBA02", "#565656", "#C5B3A5"]);
.range(["#ED6E52", "#35A5D9", "#34A87F", "#F9C826", "#C5B3A5", "#C8C8C8"]);
var fader = function(color) { return d3.interpolateRgb(color, "#fff")(0.2); };
// color = d3.scaleOrdinal(d3.schemeCategory20.map(fader)),
//var color = d3.scaleOrdinal().range(d3.schemeCategory20c);
var color = wupColorScale;
var format = d3.format(",d");
var treemap = d3.treemap()
.tile(d3.treemapResquarify)
.size([width, height])
.round(true)
.paddingInner(1)
;
d3.json("flare.json", function(error, data) {
if (error) throw error;
root = d3.hierarchy(data)
.eachBefore(function(d) { d.data.id = (d.parent ? d.parent.data.id + "." : "") + d.data.name; })
.sum(d => d.size)
.sort(function(a, b) { return b.height - a.height || b.value - a.value; });
node = root;
treemap(root);
var cell = svg.selectAll("g")
.data(root.leaves())
.enter().append("g")
.attr("class", "cell")
.attr("transform", function(d) { return "translate(" + d.x0 + "," + d.y0 + ")"; });
cell.append("rect")
.attr("id", function(d) { return d.data.id; })
.attr("width", function(d) { return d.x1 - d.x0; })
.attr("height", function(d) { return d.y1 - d.y0; })
.attr("fill", function(d) {
if (d.depth == 1) {
return color(d.data.name);
} else {
return color(d.parent.data.name);
}
});
cell.append("text")
// .attr("x", function(d) { return (d.x1 - d.x0) / 2; })
// .attr("y", function(d) { return (d.y1 - d.y0) / 2; })
.attr("dy", "1em")
.text(function(d) { return d.data.name; });
cell.append("title")
.text(function(d) { return d.data.id + "\n" + format(d.value); });
d3.select(window).on("click", function() { zoom(root); });
});
/*
function zoom(d) {
var kx = width / (d.x1 - d.x0), ky = height / (d.y1 - d.y0);
x.domain([d.x0, d.x0 + d.x1]);
y.domain([d.y0, d.y0 + d.y1]);
var t = svg.selectAll("g.cell").transition()
.duration(d3.event.altKey ? 7500 : 750)
.attr("transform", function(d) { return "translate(" + x(d.x0) + "," + y(d.y0) + ")"; });
t.select("rect")
.attr("width", function(d) { return kx * (d.x1 - d.x0) - 1; })
.attr("height", function(d) { return ky * (d.y1 - d.y0) - 1; })
t.select("text")
.attr("x", function(d) { return kx * (d.x1 - d.x0) / 2; })
.attr("y", function(d) { return ky * (d.y1 - d.y0) / 2; });
node = d;
d3.event.stopPropagation();
}
*/
</script>
https://d3js.org/d3.v4.min.js