Public
Edited
Apr 26, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// Ejemplo, se pueden definir colores mediante HEX, nombre, HSL, RGB/RGBA
swatches(["#ff3399", "hotpink", "hsl(330, 100%, 70.5%)", "rgba(128, 0, 128, 0.2)"])
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// Creamos un array con la información de los márgenes
margin = ({top: 20, right: 20, bottom: 40, left: 40})
Insert cell
// Definimos el alto del canvas
height = 350
Insert cell
// Definimos el ancho del canvas
width = 350
Insert cell
// Definimos una función que crea la escala. Básicamente, mapea (de forma lineal) a los datos
scale_x = d3.scaleLinear()
.domain([d3.min(iris, d => d.longitud_sepalo), d3.max(iris, d => d.longitud_sepalo)]) // Definimos dominio
.range([margin.left, width - margin.right]) // Definimos el rango (con un margen)
Insert cell
scale_y = d3.scaleLinear()
.domain([0, d3.max(iris, d => d.longitud_petalo)])
.range([height - margin.bottom, margin.top])
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// Opción 2...
{

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

var color2data = d3.scaleOrdinal()
.domain(["setosa", "versicolor", "virginica" ])
.range([ "#F8766D", "#00BA38", "#619CFF"])

svg.append("g")
.call(d3.axisBottom(scale_x))
.attr("transform", `translate(0,${height - margin.bottom})`);
svg.append("g")
.call(d3.axisLeft(scale_y))
.attr("transform", `translate(${margin.left},0)`);

svg.append("g")
.selectAll("circle")
.data(iris)
.join("circle")
.attr("cx", d => scale_x(d.longitud_sepalo))
.attr("cy", d => scale_y(d.longitud_petalo))
.attr("r", 3)
.attr("fill", d => color2data(d.especie));
return svg.node();
}
Insert cell
Insert cell
import {legend} from "@d3/color-legend"
Insert cell
Insert cell
{
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height);

var color_viridis = d3.scaleSequential()
.domain([d3.min(iris, d => d.area_petalo), d3.max(iris, d => d.area_petalo)])
.interpolator(d3.interpolateViridis)
svg.append("g")
.call(d3.axisBottom(scale_x))
.attr("transform", `translate(0,${height - margin.bottom})`);
svg.append("g")
.call(d3.axisLeft(scale_y))
.attr("transform", `translate(${margin.left},0)`);
svg.append("g")
.selectAll("circle")
.data(iris)
.join("circle")
.attr("cx", d => scale_x(d.longitud_sepalo))
.attr("cy", d => scale_y(d.longitud_petalo))
.attr("r", d => d.area_petalo)
.attr("fill", d => color_viridis(d.area_petalo));

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

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more