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
InvertedCountryScale = d3.scaleBand()
.domain(populations.map(d => d.country))
.range([marginPop.left, widthPop - marginPop.right])
.padding(0.5);

Insert cell

InvertedPopulationScale = d3.scaleLinear()
.domain([0, d3.max(populations, d => d.population)])
.range([heightPop - marginPop.bottom, marginPop.top])
Insert cell
Insert cell
xAxisPop = d3.axisBottom(populationScale).tickFormat(d3.format('~s'))
Insert cell
yAxisPop = d3.axisLeft(countryScale)
Insert cell
// x-axis is now the band scale (countries)
xAxisPopv2 = d3.axisBottom(InvertedCountryScale);
Insert cell
// y-axis is now the linear scale (population)
yAxisPopv2 = d3.axisLeft(InvertedPopulationScale).tickFormat(d3.format('~s'))
Insert cell
Insert cell
{
const margin = {top: 30, right: 10, bottom: 40, left: 100};

const visWidth = widthPop - margin.left - margin.right;
const visHeight = heightPop - margin.top - margin.bottom;

const svg = d3.create('svg')
.attr('width', visWidth + margin.left + margin.right)
.attr('height', visHeight + margin.top + margin.bottom);
// barras
svg.append('g')
.selectAll('rect')
.data(populations)
.join('rect')
.attr('x', d => InvertedCountryScale(d.country))
.attr('y', d => InvertedPopulationScale(d.population))
.attr('width', InvertedCountryScale.bandwidth())
.attr('height', d => InvertedPopulationScale(0) - InvertedPopulationScale(d.population))
.attr('fill', 'steelblue');
// Draw x-axis (bottom)
svg.append('g')
.attr('transform', `translate(0, ${heightPop - marginPop.bottom})`)
.call(xAxisPopv2)
.call(g => g.select('.domain').remove());
// Draw y-axis (left)
svg.append('g')
.attr('transform', `translate(${marginPop.left}, 0)`)
.call(yAxisPopv2)
.call(g => g.select('.domain').remove())
.append('text')
.attr('fill', 'black')
.attr('font-family', 'sans-serif')
.attr('x', -marginPop.left)
.attr('y', marginPop.top - 10)
.text("Población");
return svg.node();
}
Insert cell
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: 50})
Insert cell
widthDiamond = width
Insert cell
heightDiamond = 600
Insert cell
Insert cell
Insert cell
diamondColors = Array.from(new Set(diamonds.map(d => d.color))).sort()
Insert cell
Insert cell
groupX = d3.scaleBand()
.domain(diamondColors)
.range([marginDiamond.left, widthDiamond - marginDiamond.right - marginDiamond.left - 40])
.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([marginPop.left, widthPop - marginPop.right])
.padding(0.1)
Insert cell
Insert cell
maxCount = d3.max(
colorToCutToCount.values(),
d => d3.max(d.values())
)
Insert cell
Insert cell
y = d3.scaleLinear()
.domain([0, maxCount]).nice()
.range([heightDiamond, 0])
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);


// Create the inner drawing group, shifted by margins
const chart = svg.append('g')
.attr('transform', `translate(${marginDiamond.left},${marginDiamond.top})`);

// Adjusted inner width and height
const innerWidth = widthDiamond - marginDiamond.left - marginDiamond.right;
const innerHeight = heightDiamond - marginDiamond.top - marginDiamond.bottom;
/*
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 group = chart.append('g')
.selectAll('g')
.data(Array.from(colorToCutToCount)) // [ [color, Map(cut => count)], ... ]
.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
*/

//El localBarX es como un "Dentro de esta subgrupo, reparte diamondCuts en su espacio local, o sea dentro del bandwith"
const localBarX = d3.scaleBand()
.domain(diamondCuts)
.range([0, groupX.bandwidth()])
.padding(0.1);
group.selectAll('rect')
.data(([color, cutMap]) =>
Array.from(cutMap, ([cut, count]) => ({ color, cut, count }))
)
.join('rect')
.attr('x', d => {
return localBarX(d.cut);
})
.attr('width', d => {
return localBarX.bandwidth();
})
.attr('y', d => y(d.count) - marginDiamond.top - marginDiamond.bottom)
.attr('height', d => y(0) - y(d.count))
.attr('fill', d => color(d.cut));
// adicionar ejes

// X axis (at bottom of chart area)
chart.append('g')
.attr('transform', `translate(0, ${innerHeight})`)
.call(xAxisDiamond)
.call(g => g.select('.domain').remove())
.append('text')
.attr('x', innerWidth / 2)
.attr('y', 40) // below the ticks
.attr('text-anchor', 'middle')
.attr('fill', 'black')
.text('Diamond Color');

// Y axis (left side of chart area)
chart.append('g')
.call(yAxisDiamond)
.call(g => g.select('.domain').remove())
.append('text')
.attr('x', -marginDiamond.left)
.attr('y', -10)
.attr('text-anchor', 'start')
.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