chart = {
const root = treemap(data);
const total = root.value;
const svg = d3
.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [0, 0, width, height])
.style("font", "10px sans-serif")
.style("transform", "");
const shadow = DOM.uid("shadow");
const percent = d3.format(".1%");
svg
.append("filter")
.attr("id", shadow.id)
.append("feDropShadow")
.attr("flood-opacity", 0.3)
.attr("dx", 0)
.attr("stdDeviation", 3);
let groups = [...d3.group(root, (d) => d.height)];
groups.sort((a, b) => b[0] - a[0]);
const node = svg
.selectAll("g")
.data(groups)
.join("g")
.attr("filter", shadow)
.selectAll("g")
.data((d) => d[1])
.join("g")
.on("click", function () {
let d = d3.select(this).datum();
if (d) changeSelector(d.data.dimension, d.data.name);
})
.attr("transform", (d) => `translate(${d.x0},${d.y0})`);
node.append("title").text(
(d) =>
`${d
.ancestors()
.reverse()
.map((d) => d.data.name)
.join("/")}\n${format(d.value)} (${percent(d.value / total)})`
);
node
.append("rect")
.attr("id", (d) => (d.nodeUid = DOM.uid("node")).id)
.attr("fill", (d) => {
return d.height == 0 ? leafColor(d.data.name) : color(d.height);
})
.attr("width", (d) => d.x1 - d.x0)
.attr("height", (d) => d.y1 - d.y0);
node
.append("clipPath")
.attr("id", (d) => (d.clipUid = DOM.uid("clip")).id)
.append("use")
.attr("xlink:href", (d) => d.nodeUid.href);
node
.append("text")
.attr("clip-path", (d) => d.clipUid)
.selectAll("tspan")
.data((d) => {
let tspans = d.data.name
.split(/(?=[A-Z][^A-Z])/g)
.concat(`${format(d.value)} (${percent(d.value / total)})`);
return tspans;
})
.join("tspan")
.attr("fill-opacity", (d, i, nodes) =>
i === nodes.length - 1 ? 0.7 : null
)
.text((d) => d);
node
.filter((d) => d.children)
.selectAll("tspan")
.attr("dx", 3)
.attr("y", 13);
node
.filter((d) => !d.children)
.selectAll("tspan")
.attr("x", 3)
.attr(
"y",
(d, i, nodes) => `${(i === nodes.length - 1) * 0.3 + 1.1 + i * 0.9}em`
);
return svg.node();
}