chart = {
const nodes = data;
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
svg.append("rect")
.attr("width", "100%")
.attr("height", "100%")
.attr("class", "svgBackground");
svg.append("g")
.call(xAxis);
svg.append("g")
.call(yAxis);
const bubble = svg.append("g")
.attr("stroke-width", 1)
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("opacity", 0.75)
.attr("cx", d => x(d.birthRate))
.attr("cy", d => y(d.deathRate))
.attr("r", d => r(d.population))
.attr("stroke", d => color(d.population))
.attr("fill", d => color(d.population))
bubble.append("title")
.text(tooltip)
const label = svg.append("g")
.attr("font-family", "Yanone Kaffeesatz")
.attr("font-weight", 700)
.attr("text-anchor", "middle")
.selectAll("text")
.data(data)
.join("text")
.attr("id", "isoCode")
.attr("opacity", 0)
.attr("dy", "0.35em")
.attr("x", d => d.x0)
.attr("y", d => d.y0)
.attr("font-size", d => r(d.population)*1.5)
.attr("fill", d => color(d.population))
.text(d => d.code);
label.append("title")
.text(tooltip);
const legend1 = svg.append("g")
.attr("transform", `translate(${width - 340} ${height - 90})`)
.append(() => legend({
color: color,
title: "Population (in millions)",
ticks: 4,
tickFormat: d => d3.format(",.0f")(d / 1000000)
}))
const simulation = d3.forceSimulation(nodes)
.force("collide", d3.forceCollide(d => d.radius * 0.7))
.force("x", d3.forceX(d => d.x0))
.force("y", d3.forceY(d => d.y0));
simulation.on("tick", () => {
label.attr("x", d => d.x)
.attr("y", d => d.y);
});
invalidation.then(() => simulation.stop());
return svg.node();
}