Public
Edited
Apr 16
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: 100})
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'))
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
{
const svg = d3.create('svg')
.attr('width', widthPop)
.attr('height', heightPop);

const x = d3.scaleBand()
.domain(populations.map(d => d.country))
.range([marginPop.left, widthPop - marginPop.right])
.padding(0.1);

const y = d3.scaleLinear()
.domain([0, d3.max(populations, d => d.population)]).nice()
.range([heightPop - marginPop.bottom, marginPop.top]);

const xAxis = d3.axisBottom(x);
const yAxis = d3.axisLeft(y)
.tickFormat(d3.format(".2s"));

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

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

svg.append('g')
.attr('transform', `translate(0, ${heightPop - marginPop.bottom})`)
.call(xAxis)
.call(g => g.select('.domain').remove())
.selectAll('text')
.attr('transform', 'rotate(-45)')
.style('text-anchor', 'end');

svg.append('text')
.attr('fill', 'black')
.attr('font-family', 'sans-serif')
.attr('x', widthPop / 2)
.attr('y', heightPop - 5)
.attr('text-anchor', 'middle')
.text("País");

return svg.node();
}

Insert cell
// tu código aquí

{
const svg = d3.create('svg')
.attr('width', widthPop)
.attr('height', heightPop);

const x = d3.scaleBand()
.domain(populations.map(d => d.country))
.range([marginPop.left, widthPop - marginPop.right])
.padding(0.2);

const y = d3.scaleLinear()
.domain([0, d3.max(populations, d => d.population)]).nice()
.range([heightPop - marginPop.bottom, marginPop.top]);

const xAxis = d3.axisBottom(x);
const yAxis = d3.axisLeft(y)
.tickFormat(d3.format(".2s"));

svg.append('g')
.selectAll('line')
.data(populations)
.join('line')
.attr('x1', d => x(d.country) + x.bandwidth() / 2)
.attr('x2', d => x(d.country) + x.bandwidth() / 2)
.attr('y1', y(0))
.attr('y2', d => y(d.population))
.attr('stroke', 'gray')
.attr('stroke-width', 2);

svg.append('g')
.selectAll('circle')
.data(populations)
.join('circle')
.attr('cx', d => x(d.country) + x.bandwidth() / 2)
.attr('cy', d => y(d.population))
.attr('r', 5)
.attr('fill', '#69b3a2')
.attr('stroke', 'black');

const avg = d3.mean(populations, d => d.population);
svg.append("line")
.attr("x1", marginPop.left)
.attr("x2", widthPop - marginPop.right)
.attr("y1", y(avg))
.attr("y2", y(avg))
.attr("stroke", "red")
.attr("stroke-width", 2)
.attr("stroke-dasharray", "4,4");

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

svg.append('g')
.attr('transform', `translate(0, ${heightPop - marginPop.bottom})`)
.call(xAxis)
.call(g => g.select('.domain').remove())
.selectAll('text')
.attr('transform', 'rotate(-45)')
.style('text-anchor', 'end');

svg.append('text')
.attr('fill', 'black')
.attr('font-family', 'sans-serif')
.attr('x', widthPop / 2)
.attr('y', heightPop - 5)
.attr('text-anchor', 'middle')
.text("País");

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 = 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(d3.group(diamonds, d => d.color).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(), cutMap =>
d3.max(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
*/
const groups = svg.append('g')
.selectAll('g')
.data(colorToCutToCount)
.join('g')
.attr('transform', ([color]) => `translate(${groupX(color)},0)`);


/*
Tarea B:
- agregar las barras a cada grupo
- definir los atributos x, width, y, height y fill
*/
groups.selectAll('rect')
.data(([color, cutMap]) => Array.from(cutMap, ([cut, count]) => ({ cut, count })))
.join('rect')
.attr('x', d => barX(d.cut))
.attr('width', barX.bandwidth())
.attr('y', d => y(d.count))
.attr('height', d => heightDiamond - marginDiamond.bottom - 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-anchor', '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-anchor', 'middle')
.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