{
const svg = d3.create("svg")
.attr("width",350)
.attr("height",350);
const x = d3.scaleLinear()
.domain(d3.extent(datos, d => d.longitud_sepalo))
.range([margin.left, width - margin.right]);
const y = d3.scaleLinear()
.domain(d3.extent(datos, d => d.longitud_petalo))
.range([height - margin.bottom, margin.top]);
const color = d3.scaleSequential()
.domain(d3.extent(datos, d => d.area_petalo))
.interpolator(d3.interpolateViridis);
const radius = d3.scaleSqrt()
.domain(d3.extent(datos, d => d.area_petalo))
.range([2, 10]);
svg.append("g")
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x));
svg.append("g")
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y));
svg.selectAll("circle")
.data(datos)
.enter()
.append("circle")
.attr("cx", d => x(d.longitud_sepalo))
.attr("cy", d => y(d.longitud_petalo))
.attr("r", d => radius(d.area_petalo))
.attr("fill", d => color(d.area_petalo))
.attr("opacity", 0.8);
svg.append("text")
.attr("x", width / 2)
.attr("y", margin.top - 10)
.attr("text-anchor", "middle")
.style("font-size", "14px")
.text("Scatterplot: sépalo vs pétalo");
const legendWidth = 120;
const legendHeight = 10;
const legendX = width - legendWidth - 10;
const legendY = height - 80;
const defs = svg.append("defs");
const linearGradient = defs.append("linearGradient")
.attr("id", "legend-gradient");
linearGradient.selectAll("stop")
.data(d3.ticks(0, 1, 10))
.enter()
.append("stop")
.attr("offset", d => `${d * 100}%`)
.attr("stop-color", d => color(color.domain()[0] + d * (color.domain()[1] - color.domain()[0])));
svg.append("rect")
.attr("x", legendX)
.attr("y", legendY)
.attr("width", legendWidth)
.attr("height", legendHeight)
.style("fill", "url(#legend-gradient)");
svg.append("text")
.attr("x", legendX)
.attr("y", legendY - 5)
.text("Área del pétalo")
.style("font-size", "10px");
svg.append("text")
.attr("x", legendX)
.attr("y", legendY + 20)
.text(Math.round(color.domain()[0]))
.style("font-size", "10px");
svg.append("text")
.attr("x", legendX + legendWidth - 10)
.attr("y", legendY + 20)
.attr("text-anchor", "end")
.text(Math.round(color.domain()[1]))
.style("font-size", "10px");
return svg.node();
}