Published
Edited
Jun 25, 2021
Insert cell
Insert cell
Insert cell
function format_data(data) {
return{
longitud_sepalo : +data.Sepal_Length,
longitud_petalo : +data.Petal_Length,
ancho_sepalo: +data.Sepal_Width,
ancho_petalo: +data.Petal_Width,
especie : data.Species,
}
}
Insert cell
iris = d3.csv("https://raw.githubusercontent.com/holtzy/D3-graph-gallery/master/DATA/iris.csv",format_data)
Insert cell
heigth = 350
Insert cell
width = 350
Insert cell
margin = ({top:40, left:40, down:40, right:40})
Insert cell
import {select} from "@jashkenas/inputs"
Insert cell
viewof varx = select({
description: "selecciona variable para el eje x",
options: ["longitud_petalo", "longitud_sepalo", "ancho_petalo", "ancho_sepalo"],
multiple: false
})
Insert cell
viewof vary = select({
description: "Selecciona variable para el eje y",
options: ["longitud_petalo", "longitud_sepalo", "ancho_petalo", "ancho_sepalo"],
multiple: false
})
Insert cell
species = ["setosa","versicolor","virginica"]
Insert cell
viewof spec = select({
description: "Selecciona especie",
options: species,
multiple: false
})
Insert cell
species_iris = iris.filter(d => d.especie == spec)
Insert cell
dom_sepalo = d3.extent(iris,d => d.longitud_sepalo)
Insert cell
function pickx(d){
if (varx == "longitud_sepalo"){
return d.longitud_sepalo
}
else{
if (varx=="longitud_petalo"){
return d.longitud_petalo
}
else{
if (varx=='ancho_sepalo'){
return d.ancho_sepalo
}
else{
return d.ancho_petalo
}
}
}
}

Insert cell
domx = d3.extent(species_iris, d => pickx(d))
Insert cell
function picky(d){
if (vary == "longitud_sepalo"){
return d.longitud_sepalo
}
else{
if (vary=="longitud_petalo"){
return d.longitud_petalo
}
else{
if (vary=='ancho_sepalo'){
return d.ancho_sepalo
}
else{
return d.ancho_petalo
}
}
}
}
Insert cell
domy = d3.extent(species_iris, d => picky(d))
Insert cell
ranx = [margin.left, width-margin.right]
Insert cell
rany = [heigth-margin.down, margin.top]
Insert cell
x = d3.scaleLinear().
domain(domx).
range(ranx)
Insert cell
y = d3.scaleLinear().
domain(domy).
range(rany)
Insert cell
{
// 1. Creación de un área de dibujo (350x350 pixeles).
// Si lo consideran necesario, pueden modificar el tamaño del canvas.
// También es posible que necesiten más de una celda para resolverlo.
const svg = d3.create("svg")
.attr("width",width)
.attr("height",heigth);
d3.axisBottom(x)(svg.append('g').attr("transform", `translate(0,${heigth-margin.down})`));
d3.axisLeft(y)(svg.append('g').attr("transform", `translate(${margin.left},0)`));

svg.append("g")
.selectAll("circle")
.data(species_iris)
.join("circle")
.attr("cx",d => x(pickx(d)))
.attr("cy", d => y(picky(d)))
.attr("r",5)
.attr("fill", 'red');

svg.append("text")
.attr("text-anchor", "middle")
.attr("x", width - 170)
.attr("y", heigth - 5)
.text(varx);

svg.append("text")
.attr("text-anchor", "end")
.attr("transform", "rotate(-90)")
.attr("x", -120)
.attr("y", 12)
.text(vary)

svg.append("text")
.attr("text-anchor", "middle")
.attr("x", width - 170)
.attr("y", 20)
.text("Especie: " + spec);

// n. Retornamos el canvas.
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