Public
Edited
Apr 23
Insert cell
Insert cell
Insert cell
import {select} from "@jashkenas/inputs"
Insert cell
viewof variableX = select({
title: "Variable en el eje X",
options: ["Sepal_Length", "Sepal_Width", "Petal_Length", "Petal_Width"],
value: "Sepal_Length"
})
Insert cell
viewof variableY = select({
title: "Variable en el eje Y",
options: ["Sepal_Length", "Sepal_Width", "Petal_Length", "Petal_Width"],
value: "Petal_Length"
})
Insert cell
viewof especieElegida = select({
title: "Especie a visualizar",
options: ["setosa", "versicolor", "virginica"],
value: "setosa"
})
Insert cell
datos = d3.csv("https://raw.githubusercontent.com/holtzy/D3-graph-gallery/master/DATA/iris.csv", d3.autoType)
Insert cell
datosFiltrados = datos.filter(d => d.Species === especieElegida)
Insert cell
datosFiltrados
Insert cell
{
const margin = { top: 20, right: 20, bottom: 50, left: 50 };
const width = 350;
const height = 350;
const innerWidth = width - margin.left - margin.right;
const innerHeight = height - margin.top - margin.bottom;

const x = d3.scaleLinear()
.domain(d3.extent(datosFiltrados, d => +d[variableX]))
.range([0, innerWidth]);

const y = d3.scaleLinear()
.domain(d3.extent(datosFiltrados, d => +d[variableY]))
.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})`);

g.append("rect")
.attr("width", innerWidth)
.attr("height", innerHeight)
.attr("fill", "white");


g.selectAll("circle")
.data(datosFiltrados)
.join("circle")
.attr("cx", d => x(+d[variableX]))
.attr("cy", d => y(+d[variableY]))
.attr("r", 4)
.attr("fill", "steelblue")
.attr("opacity", 0.7);


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

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


g.append("text")
.attr("x", innerWidth / 2)
.attr("y", innerHeight + 40)
.attr("text-anchor", "middle")
.attr("fill", "black")
.text(variableX);

g.append("text")
.attr("transform", "rotate(-90)")
.attr("x", -innerHeight / 2)
.attr("y", -35)
.attr("text-anchor", "middle")
.attr("fill", "black")
.text(variableY);

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