{
const margin = { top: 50, right: 30, bottom: 70, left: 150 };
const width = 600 - margin.left - margin.right;
const height = 400 - margin.top - margin.bottom;
const svg = d3
.create("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom);
const g = svg
.append("g")
.attr("transform", `translate(${margin.left},${margin.top})`);
const x = d3
.scaleLinear()
.domain([0, d3.max(countries, d => +d.fertility) || 1])
.nice()
.range([0, width]);
const y = d3
.scaleLinear()
.domain([0, d3.max(countries, d => +d.p_fertility) || 1])
.nice()
.range([height, 0]);
g
.selectAll(".point")
.data(countries)
.enter()
.append("circle")
.attr("class", "point")
.attr("cx", d => x(+d.fertility))
.attr("cy", d => y(+d.p_fertility))
.attr("r", 5)
.attr("fill", "steelblue");
g
.append("g")
.attr("transform", `translate(0,${height})`)
.call(d3.axisBottom(x));
g
.append("g")
.call(d3.axisLeft(y))
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", "-0.5em")
.attr("dy", "0.15em");
svg
.append("text")
.attr("x", (width + margin.left + margin.right) / 2)
.attr("y", margin.top / 2)
.attr("text-anchor", "middle")
.style("font-size", "16px")
.style("font-weight", "bold")
.text("Fertilidade vs. Fertilidade Projetada");
svg
.append("text")
.attr("x", (width + margin.left + margin.right) / 2)
.attr("y", height + margin.top + margin.bottom - 10)
.attr("text-anchor", "middle")
.style("font-size", "12px")
.text("Taxa de Fertilidade");
svg
.append("text")
.attr("x", margin.left / 2)
.attr("y", height / 2 + margin.top)
.attr("text-anchor", "middle")
.style("font-size", "12px")
.attr("transform", `rotate(-90, ${margin.left / 2}, ${height / 2 + margin.top})`)
.text("Taxa de Fertilidade Projetada");
return svg.node();
}