Public
Edited
Apr 23
Insert cell
Insert cell
Insert cell
irisRaw = await d3.csv(
"https://raw.githubusercontent.com/holtzy/D3-graph-gallery/master/DATA/iris.csv",
d3.autoType
)
Insert cell
viewof ejeX = Inputs.select(
["Sepal_Length", "Sepal_Width", "Petal_Length", "Petal_Width"],
{label: "Eje X"}
)
Insert cell
viewof ejeY = Inputs.select(
["Sepal_Length", "Sepal_Width", "Petal_Length", "Petal_Width"],
{label: "Eje Y"}
)
Insert cell
viewof especieSeleccionada = Inputs.select(
Array.from(new Set(irisRaw.map(d => d.Species))),
{label: "Especie"}
)
Insert cell
{
const width = 350;
const height = 350;
const margin = {top: 20, right: 20, bottom: 40, left: 40};
const innerWidth = width - margin.left - margin.right;
const innerHeight = height - margin.top - margin.bottom;

const datos = irisRaw.filter(d => d.Species === especieSeleccionada);

const x = d3.scaleLinear()
.domain(d3.extent(datos, d => d[ejeX]))
.range([0, innerWidth]);

const y = d3.scaleLinear()
.domain(d3.extent(datos, d => d[ejeY]))
.range([innerHeight, 0]);

const svg = d3.create("svg")
.attr("width", width)
.attr("height", height);

const g = svg.append("g")
.attr("transform", `translate(${margin.left},${margin.top})`);

// Ejes
g.append("g")
.attr("transform", `translate(0,${innerHeight})`)
.call(d3.axisBottom(x));

g.append("g")
.call(d3.axisLeft(y));

// Puntos
g.selectAll("circle")
.data(datos)
.enter()
.append("circle")
.attr("cx", d => x(d[ejeX]))
.attr("cy", d => y(d[ejeY]))
.attr("r", 5)
.attr("fill", "steelblue")
.attr("opacity", 0.7);

return svg.node();
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more