chart = {
const path = d3.geoPath();
const ticks = [0, 2, 4, 6, 8, 10, 12, 14];
path.projection(projection);
const x = d3
.scaleLinear()
.domain(d3.extent(color.domain()))
.rangeRound([width / 2 - 250, width / 2 + 250]);
const svg = d3
.select(DOM.svg(width, height))
.style("width", "100%")
.style("height", "auto");
const defs = svg.append("defs");
const g = svg.append("g").attr("transform", `translate(0,${height - 30})`);
const linearGradient = defs
.append("linearGradient")
.attr("id", "linear-gradient");
linearGradient
.selectAll("stop")
.data(
ticks.map((t, i, n) => ({
offset: `${(100 * i) / n.length}%`,
color: color(t)
}))
)
.enter()
.append("stop")
.attr("offset", d => d.offset)
.attr("stop-color", d => d.color);
g.append("rect")
.attr("height", 8)
.attr("x", x(0))
.attr("width", x(14) - x(0))
.style("fill", `url(${location}#linear-gradient)`);
g.append("text")
.attr("class", "caption")
.attr("x", x.range()[0])
.attr("y", -6)
.attr("fill", "#000")
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.text(`Number of Constitutions up to ${year}`);
g.call(
d3
.axisBottom(x)
.tickSize(13)
.tickValues(ticks)
)
.select(".domain")
.remove();
svg
.append("g")
.selectAll("path")
.data(topojson.feature(world, world.objects.countries).features)
.enter()
.append("path")
.attr("fill", d =>
life.has(d.id) && life.get(d.id)[year]
? color(+life.get(d.id)[year])
: "#eee"
)
.attr("d", path)
.append("title")
.text(d =>
life.has(d.id) && life.get(d.id)[year]
? format(life.get(d.id))
: "Unknown"
);
svg
.append("path")
.datum(topojson.mesh(world, world.objects.countries, (a, b) => a !== b))
.attr("fill", "none")
.attr("stroke", "white")
.attr("stroke-linejoin", "round")
.attr("d", path);
return svg.node();
}