{
const width = 350;
const height = 400;
const margin = {top: 20, right: 20, bottom: 60, left: 40};
const innerWidth = width - margin.left - margin.right;
const innerHeight = 350 - margin.top - margin.bottom;
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height);
const g = svg.append("g")
.attr("transform", `translate(${margin.left},${margin.top})`);
const xScale = d3.scaleLinear()
.domain(d3.extent(iris_final, d => d.longitud_sepalo))
.range([0, innerWidth]);
const yScale = d3.scaleLinear()
.domain(d3.extent(iris_final, d => d.longitud_petalo))
.range([innerHeight, 0]);
const colorScale = d3.scaleSequential(d3.interpolateYlOrRd)
.domain(d3.extent(iris_final, d => d.area_petalo));
const rScale = d3.scaleSqrt()
.domain(d3.extent(iris_final, d => d.area_petalo))
.range([2, 10]);
g.append("g")
.attr("transform", `translate(0,${innerHeight})`)
.call(d3.axisBottom(xScale));
g.append("g")
.call(d3.axisLeft(yScale));
g.selectAll("circle")
.data(iris_final)
.join("circle")
.attr("cx", d => xScale(d.longitud_sepalo))
.attr("cy", d => yScale(d.longitud_petalo))
.attr("r", d => rScale(d.area_petalo))
.attr("fill", d => colorScale(d.area_petalo))
.attr("opacity", 0.8);
const defs = svg.append("defs");
const gradient = defs.append("linearGradient")
.attr("id", "legend-gradient")
.attr("x1", "0%")
.attr("x2", "100%");
const legendDomain = d3.extent(iris_final, d => d.area_petalo);
const legendColors = d3.quantize(d3.interpolateYlOrRd, 10);
legendColors.forEach((color, i) => {
gradient.append("stop")
.attr("offset", `${(i / (legendColors.length - 1)) * 100}%`)
.attr("stop-color", color);
});
const legendWidth = 200;
const legendHeight = 10;
const legendX = (width - legendWidth) / 2;
const legendY = 360;
svg.append("rect")
.attr("x", legendX)
.attr("y", legendY)
.attr("width", legendWidth)
.attr("height", legendHeight)
.style("fill", "url(#legend-gradient)");
const legendScale = d3.scaleLinear()
.domain(legendDomain)
.range([legendX, legendX + legendWidth]);
const legendAxis = d3.axisBottom(legendScale)
.ticks(5)
.tickSize(6)
.tickFormat(d3.format(".2f"));
svg.append("g")
.attr("transform", `translate(0,${legendY + legendHeight})`)
.call(legendAxis);
svg.append("text")
.attr("x", width / 2)
.attr("y", legendY - 5)
.attr("text-anchor", "middle")
.style("font-size", "11px")
.text("Área del pétalo");
return svg.node();
}