{
const data = treemap(mekko)
.each(d => {
d.x0 += margin.left;
d.x1 += margin.left;
d.y0 += margin.top;
d.y1 += margin.top;
})
.descendants()
.filter(d => d.height === 0);
const domain = [...new Set(data.map(d => d.data.territory))];
const colorScheme = d3.schemeTableau10;
const xs = [...new Set(data.flatMap(d => [d.x0, d.x1]))];
const lines = xs.map((x, i) => {
const f = data.find(d => d.x0 === x);
return {
x: x,
m: i < xs.length - 1 ? (x + xs[i + 1]) / 2 : null,
label: f ? f.data.year : null
}
});
const plot = Plot.plot({
width: width,
height: mekkoHeight + 30,
x: {
axis: null,
type: "identity"
},
y: {
axis: null,
type: "identity"
},
color: {
domain: domain,
range: colorScheme
},
marks: [
Plot.rect(data, {
x1: "x0", x2: "x1",
y1: "y0", y2: "y1",
fill: d => d.data.territory,
title: d => `${d.data.territory}\n${d.data.total}`
}),
Plot.ruleX(lines, {
x: d => d.x,
y1: 0,
y2: mekkoHeight,
stroke: "white",
strokeWidth: 2
}),
Plot.text(lines, {
x: "m",
y: mekkoHeight + 10,
fontWeight: "bold",
textAnchor: "middle",
text: "label"
}),
Plot.text(y.ticks(5), {
x: 10,
y: d => y(d),
fontWeight: "bold",
text: d => `${d}%`
})
]
});
return wrap(
Swatches({color: d3.scaleOrdinal(domain, colorScheme)}),
plot
);
}