{
const svg = d3.create("svg")
.attr("width",350)
.attr("height",350);
svg.append("text")
.attr("x", 110)
.attr("y", 175)
.text("Área de dibujo vacía");
return svg.node();
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Scatterplot de Longitudes de Sépalos y Pétalos</title>
<script src="https://d3js.org/d3.v7.min.js"></script>
<style>
.axis {
font: 10px sans-serif;
}
.legend {
font-size: 12px;
}
</style>
</head>
<body>
<h2>Scatterplot de Longitudes de Sépalos y Pétalos</h2>
<div id="chart"></div>
<script>
const csvUrl = 'URL_DEL_CSV';
d3.csv(csvUrl).then(data => {
const processedData = data.map(d => {
const longitud_sepalo = parseFloat(d.Sepal_Length);
const longitud_petalo = parseFloat(d.Petal_Length);
const area_petalo = longitud_petalo * parseFloat(d.Petal_Width);
return {
longitud_sepalo,
longitud_petalo,
area_petalo,
especie: d.Species
};
});
const margin = { top: 20, right: 20, bottom: 30, left: 40 };
const width = 350 - margin.left - margin.right;
const height = 350 - margin.top - margin.bottom;
const xScale = d3.scaleLinear()
.domain(d3.extent(processedData, d => d.longitud_sepalo))
.range([0, width]);
const yScale = d3.scaleLinear()
.domain(d3.extent(processedData, d => d.longitud_petalo))
.range([height, 0]);
const colorScale = d3.scaleSequential(d3.interpolateBlues)
.domain(d3.extent(processedData, d => d.area_petalo));
const radiusScale = d3.scaleSqrt()
.domain(d3.extent(processedData, d => d.area_petalo))
.range([2, 10]);
const svg = d3.select("#chart").append("svg")
.attr("width", 350)
.attr("height", 350);
const g = svg.append("g")
.attr("transform", `translate(${margin.left},${margin.top})`);
const xAxis = d3.axisBottom(xScale);
const yAxis = d3.axisLeft(yScale);
g.append("g")
.attr("class", "axis")
.attr("transform", `translate(0, ${height})`)
.call(xAxis);
g.append("g")
.attr("class", "axis")
.call(yAxis);
g.selectAll("circle")
.data(processedData)
.enter()
.append("circle")
.attr("cx", d => xScale(d.longitud_sepalo))
.attr("cy", d => yScale(d.longitud_petalo))
.attr("r", d => radiusScale(d.area_petalo))
.attr("fill", d => colorScale(d.area_petalo));
const legend = g.append("g")
.attr("transform", `translate(${width - 100}, 0)`);
const legendHeight = 200;
const legendWidth = 20;
const legendScale = d3.scaleLinear()
.domain(d3.extent(processedData, d => d.area_petalo))
.range([legendHeight, 0]);
const legendAxis = d3.axisRight(legendScale)
.ticks(5);
legend.append("g")
.attr("class", "axis")
.call(legendAxis);
legend.selectAll("rect")
.data(d3.range(legendHeight))
.enter()
.append("rect")
.attr("x", 0)
.attr("y", d => d)
.attr("width", legendWidth)
.attr("height", 1)
.attr("fill", d => colorScale(legendScale.invert(d)));
legend.append("text")
.attr("x", -30)
.attr("y", legendHeight / 2)
.text("Área del Pétalo")
.attr("class", "legend");
}).catch(error => {
console.error('Error al cargar el CSV:', error);
});
</script>
</body>
</html>