chart = {
let svg;
if (!this) {
svg = d3
.create("svg")
.attr("width", width)
.attr("height", height);
} else {
svg = d3.select(this);
}
make_treemap_layout(hierarchy.sum(d => d[metric]));
svg
.selectAll("g")
.data(hierarchy.leaves())
.join(enter => {
const gs = enter
.append("g")
.attr("transform", d => `translate(${width / 2}, ${height / 2})`);
gs.append("text")
.text(d => d.data.country_code)
.style("font-size", "10px")
.attr("y", 10);
gs.append("rect")
.style("stroke", "white")
.style("fill", d => color(d.data.region))
.style("fill-opacity", .5);
return gs;
})
.transition()
.ease(d3.easeBounceOut)
.duration(1500)
.delay((d, i) => i * 20)
.attr("transform", d => `translate(${d.x0}, ${d.y0})`)
.selectAll("rect")
.attr("width", d => d.x1 - d.x0)
.attr("height", d => d.y1 - d.y0);
return svg.node();
}