{
const width = 420;
const height = 350;
const margin = { top: 20, right: 50, bottom: 40, left: 60 };
const color = d3.scaleSequential(d3.interpolateReds)
.domain(d3.extent(iris, d => d.area_petalo));
const radius = d3.scaleLinear()
.domain(d3.extent(iris, d => d.area_petalo))
.range([3, 10]);
const svg = d3.create("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.style("font-family", "sans-serif");
const plot = svg.append("g")
.attr("transform", `translate(${margin.left},${margin.top})`);
plot.append("g")
.attr("transform", `translate(0,${height})`)
.call(d3.axisBottom(x));
plot.append("g")
.call(d3.axisLeft(y));
plot.append("text")
.attr("x", width / 2)
.attr("y", height + 35)
.attr("text-anchor", "middle")
.attr("font-size", 12)
.text("Longitud del sépalo");
plot.append("text")
.attr("transform", "rotate(-90)")
.attr("x", -height / 2)
.attr("y", -45)
.attr("text-anchor", "middle")
.attr("font-size", 12)
.text("Longitud del pétalo");
plot.selectAll("circle")
.data(iris)
.join("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);
const legendHeight = 200;
const legendWidth = 10;
const legendX = width + 40;
const legendY = (height - legendHeight) / 2;
const defs = svg.append("defs");
const gradient = defs.append("linearGradient")
.attr("id", "legend-gradient-vertical")
.attr("x1", "0%").attr("x2", "0%")
.attr("y1", "100%").attr("y2", "0%");
gradient.selectAll("stop")
.data(d3.ticks(0, 1, 10))
.join("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)
.attr("fill", "url(#legend-gradient-vertical)");
svg.append("g")
.attr("transform", `translate(${legendX + legendWidth},${legendY})`)
.call(d3.axisRight(d3.scaleLinear()
.domain(color.domain())
.range([legendHeight, 0]))
.ticks(6)
.tickFormat(d3.format(".1f")));
svg.append("text")
.attr("x", legendX + 20)
.attr("y", legendY - 8)
.attr("text-anchor", "middle")
.attr("font-size", 12)
.text("Área del pétalo");
return svg.node();
}