{
const width = 350;
const height = 350;
const margin = { top: 20, right: 100, bottom: 40, left: 50 };
const especies = [...new Set(iris.map(d => d.Species))];
const color = d3.scaleOrdinal()
.domain(especies)
.range(d3.schemeSet1);
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})`);
const escalaX = d3.scaleLinear()
.domain(d3.extent(iris, d => +d[campo_x])).nice()
.range([0, width]);
const escalaY = d3.scaleLinear()
.domain(d3.extent(iris, d => +d[campo_y])).nice()
.range([height, 0]);
plot.append("g")
.attr("transform", `translate(0,${height})`)
.call(d3.axisBottom(escalaX));
plot.append("g")
.call(d3.axisLeft(escalaY));
plot.append("text")
.attr("x", width / 2)
.attr("y", height + 30)
.attr("text-anchor", "middle")
.attr("font-size", 12)
.text(campo_x);
plot.append("text")
.attr("transform", "rotate(-90)")
.attr("x", -height / 2)
.attr("y", -40)
.attr("text-anchor", "middle")
.attr("font-size", 12)
.text(campo_y);
plot.selectAll("circle")
.data(iris)
.join("circle")
.attr("cx", d => escalaX(d[campo_x]))
.attr("cy", d => escalaY(d[campo_y]))
.attr("r", 4)
.attr("fill", d => color(d.Species))
.attr("opacity", 0.8);
const legend = svg.append("g")
.attr("transform", `translate(${width + margin.left + 10}, ${margin.top})`);
especies.forEach((especie, i) => {
const g = legend.append("g").attr("transform", `translate(0, ${i * 20})`);
g.append("rect")
.attr("width", 12)
.attr("height", 12)
.attr("fill", color(especie));
g.append("text")
.attr("x", 16)
.attr("y", 10)
.attr("font-size", 11)
.text(especie);
});
return svg.node();
}