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
forked from me1er's block: RUL Chart area and income distribution by tenant (Treemap)
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>
//
// Examples
//
// V3 zoomable with additional information
// https://bl.ocks.org/ganeshv/6a8e9ada3ab7f2d88022
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","Jens Jetzer","Post finance","Aldi","Andere"])
.range(["#ED6E52", "#35A5D9", "#34A87F", "#F9C826", "#C5B3A5", "#C8C8C8"]);
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.descendants())
.enter()
.filter(function (d) {return (d.depth === 2)})
.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);
}
});
var labelGroup = svg.selectAll("b")
.data(root.children)
.enter()
.filter(function (d) {return (d.depth === 1)})
.append("g")
;
var label = labelGroup.append("text")
.attr("x", function(d) { return (d.x0); })
.attr("y", function(d) { return (d.y0); })
.append("tspan")
.attr("dx", "0.25em")
.attr("dy", "1em")
.text(function(d) { return d.data.name; })
.style("font-weight", "bold")
.style("font-size", "12px")
.style("opacity", function(d) { return this.getComputedTextLength() < (d.x1 - d.x0) ? 1 : 0})
.append("tspan")
.attr("x", function(d) { return (d.x0); })
.attr("y", function(d) { return (d.y0); })
.attr("dx", "0.25em")
.attr("dy", "2.25em")
.text(function(d) { return thousandSeparatorFormatter(d.value); })
.style("fill", "#555555")
.style("font-size", "10px")
.style("font-weight", "normal")
;
label.call(text1);
var subLabel = svg.selectAll("c")
.data(root.descendants())
.enter()
.filter(function (d) {return (d.depth === 2)})
.append("g")
;
subLabel.append("text")
.attr("x", function(d) {return d.x1})
.attr("y", function(d) { return d.y0; })
.attr("dx", "0.25em")
.attr("dy", "-1em")
.text(function(d) { return d.data.name; })
.call(text)
;
function text1(text) {
text.style("opacity", function(d) { return this.getComputedTextLength() < (d.x1 - d.x0) ? 1 : 0; });
};
function text(text) {
// text.attr("x", function(d) { return d.x1 - this.getComputedTextLength() - 6; })
text.attr("x", function(d) { return d.x0; })
.attr("y", function(d) { return d.y1 + 6; })
.attr("dx", "0.25em")
//.style("font-style", "italic")
.style("fill", "#555555")
.style("opacity", function(d) { return this.getComputedTextLength() < (d.x1 - d.x0) ? 1 : 0; });
};
});
function thousandSeparatorFormatter(data) {
const precision = d3.precisionFixed(2);
return (d3.format(precision)(data/1000)).replace(/,/g, '\'');
}
</script>
https://d3js.org/d3.v4.min.js