Public
Edited
Apr 21
Fork of Lab - Barras
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
populations
Insert cell
Insert cell
marginPop = ({top: 10, bottom: 45, left: 75, right: 10})
Insert cell
widthPop = width
Insert cell
heightPop = 400
Insert cell
Insert cell
maxPopulation = d3.max(populations, d => d.population)
Insert cell
populationScale = d3.scaleLinear()
.domain([0, maxPopulation])
.range([marginPop.left, widthPop - marginPop.right])
Insert cell
countries = populations.map(d => d.country)
Insert cell
countryScale = d3.scaleBand()
.domain(countries)
.range([marginPop.top, heightPop - marginPop.bottom])
.padding(0.2)
Insert cell
Insert cell
xAxisPop = d3.axisBottom(populationScale).tickFormat(d3.format('~s'))f
Insert cell
yAxisPop = d3.axisLeft(countryScale)
Insert cell
Insert cell
{
const svg = d3.create('svg')
.attr('width', widthPop)
.attr('height', heightPop);
// barras
svg.append('g')
.selectAll('rect')
.data(populations)
.join('rect')
.attr('x', populationScale(0))
.attr('y', d => countryScale(d.country))
.attr('width', d => populationScale(d.population) - populationScale(0)) // pasar la población a la escala x para obtener el ancho
.attr('height', countryScale.bandwidth()) // obtener el alto de cada banda de la escala
.attr('fill', 'steelblue');
// eje y
svg.append('g')
.attr('transform', `translate(${marginPop.left})`)
.call(yAxisPop)
.call(g => g.select('.domain').remove()); // eliminar la línea base del eje
// eje x
svg.append('g')
.attr('transform', `translate(0, ${heightPop - marginPop.bottom})`)
.call(xAxisPop)
.call(g => g.select('.domain').remove())
// agregar una etiqueta al eje x
.append('text')
.attr('fill', 'black')
.attr('font-family', 'sans-serif')
.attr('x', width / 2)
.attr('y', 40)
.text("Población");
return svg.node();
}
Insert cell
populationScaleVertical = d3.scaleLinear()
.domain([0, maxPopulation])
.range([heightPop - marginPop.bottom, marginPop.top]);
Insert cell
countryScaleVertical = d3.scaleBand()
.domain(countries)
.range([marginPop.left, widthPop - marginPop.right])
.padding(0.2);
Insert cell
xAxisPopVertical = d3.axisBottom(countryScaleVertical);
Insert cell
yAxisPopVertical = d3.axisLeft(populationScaleVertical).tickFormat(d3.format('~s'));
Insert cell
{
const svg = d3.create('svg')
.attr('width', widthPop)
.attr('height', heightPop);

svg.append('g')
.selectAll('rect')
.data(populations)
.join('rect')
.attr('x', d => countryScaleVertical(d.country))
.attr('y', d => populationScaleVertical(d.population))
.attr('width', countryScaleVertical.bandwidth())
.attr('height', d => heightPop - marginPop.bottom - populationScaleVertical(d.population))
.attr('fill', 'steelblue');

svg.append('g')
.attr('transform', `translate(${marginPop.left}, 0)`)
.call(yAxisPopVertical)
.call(g => g.select('.domain').remove());

svg.append('g')
.attr('transform', `translate(0, ${heightPop - marginPop.bottom})`)
.call(xAxisPopVertical)
.call(g => g.select('.domain').remove());

svg.append('text')
.attr('fill', 'black')
.attr('font-family', 'sans-serif')
.attr('x', widthPop / 2)
.attr('y', heightPop)
.text('Población');

return svg.node();}
Insert cell
meanPopulation = d3.mean(populations, d => d.population)
Insert cell
{
const svg = d3.create('svg')
.attr('width', widthPop)
.attr('height', heightPop);

svg.append('g')
.selectAll('rect')
.data(populations)
.join('rect')
.attr('x', d => countryScaleVertical(d.country))
.attr('y', d => populationScaleVertical(d.population))
.attr('width', countryScaleVertical.bandwidth())
.attr('height', d => heightPop - marginPop.bottom - populationScaleVertical(d.population))
.attr('fill', 'steelblue');


svg.append('line')
.attr('x1', marginPop.left)
.attr('x2', widthPop - marginPop.right)
.attr('y1', populationScaleVertical(meanPopulation))
.attr('y2', populationScaleVertical(meanPopulation))
.attr('stroke', 'red')
.attr('stroke-width', 2)
svg.append('g')
.attr('transform', `translate(${marginPop.left}, 0)`)
.call(yAxisPopVertical)
.call(g => g.select('.domain').remove());

svg.append('g')
.attr('transform', `translate(0, ${heightPop - marginPop.bottom})`)
.call(xAxisPopVertical)
.call(g => g.select('.domain').remove());

svg.append('text')
.attr('fill', 'black')
.attr('font-family', 'sans-serif')
.attr('x', widthPop / 2)
.attr('y', heightPop)
.text('Población');

return svg.node();
}
Insert cell
{
const svg = d3.create('svg')
.attr('width', widthPop)
.attr('height', heightPop);

svg.append('g')
.selectAll('line')
.data(populations)
.join('line')
.attr('x1', d => countryScaleVertical(d.country))
.attr('x2', d => countryScaleVertical(d.country))
.attr('y1', d => populationScaleVertical(d.population))
.attr('y2', d => populationScaleVertical(0) )
.attr('width', countryScaleVertical.bandwidth())
.attr('height', d => heightPop - marginPop.bottom - populationScaleVertical(d.population))
.attr('fill', 'steelblue')
.attr("stroke", "grey");

svg.selectAll("mycircle")
.data(populations)
.join('circle')
.attr("cx", d => countryScaleVertical(d.country))
.attr("cy", d => populationScaleVertical(d.population) )
.attr("r", "4")
.attr('fill', 'sky blue')
.attr("stroke", "sky blue")

svg.append('g')
.attr('transform', `translate(${marginPop.left}, 0)`)
.call(yAxisPopVertical)
.call(g => g.select('.domain').remove());

svg.append('g')
.attr('transform', `translate(0, ${heightPop - marginPop.bottom})`)
.call(xAxisPopVertical)
.call(g => g.select('.domain').remove());

svg.append('text')
.attr('fill', 'black')
.attr('font-family', 'sans-serif')
.attr('x', widthPop / 2)
.attr('y', heightPop)
.text('Población');

return svg.node();}
Insert cell
Insert cell
diamonds = FileAttachment("diamonds-10k.csv").csv({typed: true})
Insert cell
diamonds
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
Insert cell
html`<div style='font-family: sans-serif; font-size: 10px; font-weight: bold;'>Diamond Cut</div>
${swatches({color})}`
Insert cell
Insert cell
Insert cell
colorToCutToCount = d3.rollup(
diamonds,
v => v.length,
d => d.color,
d => d.cut
);
Insert cell
Insert cell
marginDiamond = ({top: 10, bottom: 50, left: 85, right: 10})
Insert cell
widthDiamond = width
Insert cell
heightDiamond = 600
Insert cell
Insert cell
Insert cell
diamondColors = Array.from(colorToCutToCount.keys()).sort();
Insert cell
Insert cell
groupX = d3.scaleBand()
.domain(diamondColors)
.range([marginDiamond.left, widthDiamond - marginDiamond.right])
.padding(0.1)
Insert cell
Insert cell
new Set(diamonds.map(d => d.cut))
Insert cell
diamondCuts = ['Premium', 'Ideal', 'Very Good', 'Good', 'Fair']
Insert cell
Insert cell
barX = d3.scaleBand()
.domain(diamondCuts)
.range([0, groupX.bandwidth()])
.padding(0.1);
Insert cell
Insert cell
maxCount = d3.max(
Array.from(colorToCutToCount.values()).flatMap(
cutMap => Array.from(cutMap.values())
)
);
Insert cell
Insert cell
y = d3.scaleLinear()
.domain([0, maxCount])
.nice()
.range([heightDiamond - marginDiamond.bottom, marginDiamond.top]);

Insert cell
Insert cell
color = d3.scaleOrdinal()
.domain(diamondCuts)
.range(['#bd0026', '#f03b20', '#fd8d3c', '#fecc5c', '#ffffb2'])
Insert cell
Insert cell
xAxisDiamond = d3.axisBottom(groupX)
Insert cell
yAxisDiamond = d3.axisLeft(y)
Insert cell
Insert cell
legend = html`<div style='font-family: sans-serif; font-size: 10px; font-weight: bold;'>Diamond Cut</div>
${swatches({color})}`
Insert cell
{
// set up
const svg = d3.create('svg')
.attr('width', widthDiamond)
.attr('height', heightDiamond);
/*
Tarea A:
- agregar un <g> al svg por cada grupo de barras
- usar colorToCutToCount como los datos
- posicionar cada grupo usando la escala groupX
*/
svg.selectAll('g.color-group')
.data(Array.from(colorToCutToCount.entries()))
.join('g')
.attr('class', 'color-group')
.attr('transform', d => `translate(${groupX(d[0])},0)`)

/*
Tarea B:
- agregar las barras a cada grupo
- definir los atributos x, width, y, height y fill
*/
.selectAll('rect')
.data(d => diamondCuts.map(cut => ({
cut: cut,
count: d[1].get(cut) || 0
})))
.join('rect')
.attr('x', d => barX(d.cut))
.attr('width', barX.bandwidth())
.attr('y', d => y(d.count))
.attr('height', d => y(0) - y(d.count))
.attr('fill', d => color(d.cut));

// adicionar ejes

svg.append('g')
.attr('transform', `translate(0,${heightDiamond - marginDiamond.bottom})`)
.call(xAxisDiamond)
.call(g => g.select('.domain').remove())
.append('text')
.attr('x', widthDiamond / 2)
.attr('y', 30)
.attr('dominant-baseline', 'hanging')
.attr('text-align', 'middle')
.attr('fill', 'black')
.text('Diamond Color');
svg.append('g')
.attr('transform', `translate(${marginDiamond.left})`)
.call(yAxisDiamond)
.call(g => g.select('.domain').remove())
.append('text')
.attr('x', -50)
.attr('y', heightDiamond / 2)
.attr('dominant-baseline', 'middle')
.attr('text-align', 'end')
.attr('fill', 'black')
.text('Count');
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