Published
Edited
Jun 25, 2021
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
viewof color_steps = slider({
min: 0,
max: 30,
step: 1,
value: 10,
title: "Cantidad de colores",
description: "Control para la cantidad de colores a visualizar en la paleta."
})
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
swatches([blue1, blue2])
Insert cell
blue1 = 'hsl(240,100%,15%)'
Insert cell
blue2 = 'hsl(180,100%,85%)'
Insert cell
Insert cell
swatches(d3.quantize(d3.interpolateRgb(blue1,blue2), color_steps_blue))
Insert cell
swatches([pink1,pink2])
Insert cell
pink1 = '#fec5bb'
Insert cell
pink2 = '#582f0e'
Insert cell
viewof color_steps_pink = slider({
min: 0,
max: 50,
step: 1,
value: 10,
title: "Cantidad de colores",
description: "Control para la cantidad de colores a visualizar en la paleta."
})
Insert cell
swatches(d3.quantize(d3.interpolateRgb(pink1,pink2), color_steps_pink))
Insert cell
black = 'rgb(0,0,0)'
Insert cell
red = 'rgb(255,0,0)'
Insert cell
yellow = 'rgb(255,168,0)'
Insert cell
Insert cell
swatches(d3.merge([d3.quantize(d3.interpolateRgb(black,red), (color_steps_red+ color_steps_red%2)/2),d3.quantize(d3.interpolateRgb(red,yellow), (color_steps_red-color_steps_red%2)/2) ]))
Insert cell
/* Extended Array */
colores = [{"name":"Sizzling Red","hex":"ff595e","rgb":[255,89,94],"cmyk":[0,65,63,0],"hsb":[358,65,100],"hsl":[358,100,67],"lab":[61,63,32]},{"name":"Sunglow","hex":"ffca3a","rgb":[255,202,58],"cmyk":[0,21,77,0],"hsb":[44,77,100],"hsl":[44,100,61],"lab":[84,6,74]},{"name":"Yellow Green","hex":"8ac926","rgb":[138,201,38],"cmyk":[31,0,81,21],"hsb":[83,81,79],"hsl":[83,68,47],"lab":[74,-43,68]},{"name":"Green Blue Crayola","hex":"1982c4","rgb":[25,130,196],"cmyk":[87,34,0,23],"hsb":[203,87,77],"hsl":[203,77,43],"lab":[52,-4,-42]},{"name":"Royal Purple","hex":"6a4c93","rgb":[106,76,147],"cmyk":[28,48,0,42],"hsb":[265,48,58],"hsl":[265,32,44],"lab":[38,28,-35]}]
Insert cell
green = 'rgb(0,255,0)'
Insert cell
blue = 'rgb(0,0,255)'
Insert cell
section = (color_steps_multi-color_steps_multi%4)/4
Insert cell
rest = color_steps_multi - 3*section
Insert cell
viewof color_steps_multi = slider({
min: 0,
max: 50,
step: 1,
value: 10,
title: "Cantidad de colores",
description: "Control para la cantidad de colores a visualizar en la paleta."
})
Insert cell
swatches(d3.merge([d3.quantize(d3.interpolateRgb(red,'yellow'),rest),d3.quantize(d3.interpolateRgb('yellow',green),section),d3.quantize(d3.interpolateRgb(green,blue),section),d3.quantize(d3.interpolateRgb(blue,'purple'),section)]))
Insert cell
Insert cell
// Resolución ejercicios

Insert cell
swatches([redl,yellowl,greenl, bluel,purplel])
Insert cell
redl = 'hsl(0,100%,' + lum_steps +'%)'
Insert cell
yellowl= 'hsl(50,100%,' + lum_steps +'%)'
Insert cell
greenl = 'hsl(100,100%,' + lum_steps +'%)'
Insert cell
bluel = 'hsl(220,100%,' + lum_steps +'%)'
Insert cell
purplel = 'hsl(280,100%,' + lum_steps +'%)'
Insert cell
restl = color_steps_l - 3*sectionl
Insert cell
sectionl = (color_steps_l-color_steps_l%4)/4
Insert cell
Insert cell
viewof lum_steps = slider({
min: 0,
max: 100,
step: 1,
value: 10,
title: "Luminosidad",
description: "Control para la luminosidad del color."
})
Insert cell
swatches(d3.merge([d3.quantize(d3.interpolateRgb(redl,yellowl),restl),d3.quantize(d3.interpolateRgb(yellowl,greenl),sectionl),d3.quantize(d3.interpolateRgb(greenl,bluel),sectionl),d3.quantize(d3.interpolateRgb(bluel,purplel),sectionl)]))
Insert cell
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
// Resolución ejercicio c)
iris = d3.csv("https://raw.githubusercontent.com/holtzy/D3-graph-gallery/master/DATA/iris.csv",format_data)
Insert cell
function format_data(data) {
return{
longitud_sepalo : +data.Sepal_Length,
longitud_petalo : +data.Petal_Length,
especie : data.Species,
area_petalo : data.Petal_Length*data.Petal_Width/2
}
}
Insert cell
Insert cell
// Resolución ejercicio d)
heigth = 350
Insert cell
width = 350
Insert cell
margin_left = 30
Insert cell
margin_right = 20
Insert cell
margin_top = 10
Insert cell
margin_down = 40
Insert cell
dom_sepalo = d3.extent(iris,d => d.longitud_sepalo)
Insert cell
range_sepalo = [margin_left,width-margin_right]
Insert cell
dom_petalo = d3.extent(iris,d => d.longitud_petalo)
Insert cell
range_petalo = [heigth - margin_down, margin_top]
Insert cell
x = d3.scaleLinear().
domain(dom_sepalo).
range(range_sepalo)
Insert cell
y = d3.scaleLinear().
domain(dom_petalo).
range(range_petalo)
Insert cell
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(iris)
.join("circle")
.attr("cx",d => x(d.longitud_sepalo))
.attr("cy", d => y(d.longitud_petalo))
.attr("r",2.5)
.attr("fill", 'red');

// n. Retornamos el canvas.
return svg.node();
}
Insert cell
Insert cell
color = d3.scaleSequential()
.domain([0, d3.max(iris, d => d.area_petalo)])
.interpolator(d3.interpolateRgb("yellow", "red"))
Insert cell
radio = d3.scaleLinear()
.domain([0, d3.max(iris, d => d.area_petalo)])
.range([0,9])
Insert cell
import {legend} from "@d3/color-legend"
Insert cell
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(iris)
.join("circle")
.attr("cx",d => x(d.longitud_sepalo))
.attr("cy", d => y(d.longitud_petalo))
.attr("r",d => radio(d.area_petalo))
.attr("fill", d => color(d.area_petalo));

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