Public
Edited
Apr 28
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: 32})
Insert cell
widthPop = width
Insert cell
heightPop = 600
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'))
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
// Codigo aqui
xBandScale = d3.scaleBand().domain(countries).range([0, widthPop - marginPop.right]).padding(0.4)
Insert cell
yLinearScale = d3.scaleLinear().domain([0, maxPopulation]).range([heightPop - marginPop.top, 0])
Insert cell
yLinearScale(0)
Insert cell
yLinearScale(1200000000)
Insert cell
xAxisBandScale = d3.axisBottom(xBandScale)
Insert cell
yAxisLinealScale = d3.axisLeft(yLinearScale).tickFormat(d3.format('~s'))
Insert cell
promedio = d3.mean(populations, p => p.population)
Insert cell
{
const svg = d3.create('svg')
.attr('width', widthPop)
.attr('height', heightPop);

// Dibujamos las barras
svg.append('g')
.selectAll('rect')
.data(populations)
.join('rect')
.attr('x', c => xBandScale(c.country))
.attr('y', c => yLinearScale(c.population))
.attr('width', xBandScale.bandwidth())
.attr('height', c => heightPop - yLinearScale(c.population) - marginPop.bottom)
.attr('fill', 'steelblue')

svg.append('line')
.attr('x1', marginPop.left - marginPop.right)
.attr('x2', widthPop - marginPop.right)
.attr('y1', yLinearScale(promedio))
.attr('y2', yLinearScale(promedio))
.attr('stroke', 'red')
.attr('stroke-width', 2);

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

// Eje Y
svg.append('g')
.attr('transform', `translate(${marginPop.left - marginPop.right}, ${marginPop.top - marginPop.bottom})`)
.call(yAxisLinealScale)
.call(g => g.select('.domain').remove())

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
diamondsChart
Insert cell
Insert cell
colorToCutToCount = diamonds.reduce((accumulator, d) => {
if(!accumulator[d.color]){
accumulator[d.color] = {}
}
if(!accumulator[d.color][d.cut]){
accumulator[d.color][d.cut] = 0
}
accumulator[d.color][d.cut] += 1;

return accumulator;
}, {})
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 = Object.keys(colorToCutToCount).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(Object.values(colorToCutToCount), c => d3.max(Object.values(c)))
Insert cell
Insert cell
y = d3.scaleLinear()
.domain([0, maxCount]).nice()
.range([heightDiamond - marginDiamond.bottom, marginDiamond.top])
Insert cell
y(0)
Insert cell
y(118)
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
*/

/*
Tarea B:
- agregar las barras a cada grupo
- definir los atributos x, width, y, height y fill
*/
svg.selectAll('g')
.data(Object.keys(colorToCutToCount))
.join('g')
.attr('transform', color => `translate(${groupX(color)}, 0)`)
.selectAll('rect')
.data(color => Object.entries(colorToCutToCount[color]))
.join('rect')
.attr('x', ([cut, count]) => barX(cut))
.attr('y', ([cut, count]) => y(count))
.attr('width', barX.bandwidth())
.attr('height', ([cut, count]) => y(0) - y(count))
.attr('fill', ([cut, count]) => color(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