scatterplot = {
const margin = { top: 100, right: 100, bottom: 50, left: 100 };
const width = 800 - margin.left - margin.right;
const height = 400 - margin.top - margin.bottom;
const svg = d3.select(DOM.svg(width + margin.left + margin.right, height + margin.top + margin.bottom));
const g = svg.append("g")
.attr("transform", `translate(${margin.left},${margin.top})`);
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("ScatterPlot: Fertilidade vs Expectativa de Vida");
const xScale = d3.scaleLinear()
.domain([0, d3.max(countries, d => d.fertility) + 1])
.range([0, width]);
const yScale = d3.scaleLinear()
.domain([0, d3.max(countries, d => d.life_expect) + 10])
.range([height, 0]);
const tooltip = d3.select("body")
.append("div")
.style("position", "absolute")
.style("background", "#f9f9f9")
.style("padding", "6px")
.style("border", "1px solid #d3d3d3")
.style("border-radius", "4px")
.style("pointer-events", "none")
.style("font-size", "12px")
.style("display", "none");
g.selectAll("circle")
.data(countries)
.enter()
.append("circle")
.attr("cx", d => xScale(d.fertility))
.attr("cy", d => yScale(d.life_expect))
.attr("r", 4)
.attr("fill", d => {if(d.country === "Afghanistan") return "#8B0000";
return "green";})
.on("mouseover", function(event, d) {
tooltip
.style("display", "block")
.html(`
<strong>${d.country}</strong><br/>
Fertilidade: ${d.fertility}<br/>
Expectativa de vida: ${d.life_expect}
`);
})
.on("mousemove", function(event) {
tooltip
.style("left", (event.pageX + 10) + "px")
.style("top", (event.pageY - 20) + "px");
})
.on("mouseout", function() {
tooltip.style("display", "none");
});
g.append("g")
.attr("transform", `translate(0, ${height})`)
.call(d3.axisBottom(xScale))
.append("text")
.attr("x", width / 2)
.attr("y", 40)
.attr("fill", "black")
.style("font-size", "12px")
.style("text-anchor", "middle")
.text("Taxa de Fertilidade");
g.append("g")
.call(d3.axisLeft(yScale))
.append("text")
.attr("transform", "rotate(-90)")
.attr("x", -height / 2)
.attr("y", -margin.left + 70)
.attr("fill", "black")
.style("font-size", "12px")
.style("text-anchor", "middle")
.text("Expectativa de Vida");
return svg.node();
}