Public
Edited
Jan 5
1 fork
Importers
Insert cell
Insert cell
Insert cell
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])
.nice()
.range([heightPop - marginPop.bottom, marginPop.top])
Insert cell
countries = populations.map(d => d.country)
Insert cell
countryScaleBand = d3.scaleBand()
.domain(countries)
.range([marginPop.left, widthPop - marginPop.right])
.padding(0.2)
Insert cell
countryScale = d3.scalePoint()
.domain(countries)
.range([marginPop.left, widthPop - marginPop.right])
.padding(0.2)
Insert cell
Insert cell
xAxisPop = d3.axisBottom(countryScale)
Insert cell
yAxisPop = d3.axisLeft(populationScale).tickFormat(d3.format('~s'))
Insert cell
Insert cell
Insert cell
{
// create and select an svg element
const svg = d3.create('svg')
.attr('width', widthPop)
.attr('height', heightPop);
// bind our data to lines
svg.append('g')
.selectAll('rect')
.data(populations)
.join('rect')
.attr('x', d => countryScaleBand(d.country))
.attr('width', d => countryScaleBand.bandwidth())
.attr('y', d => populationScale(d.population))
.attr('height', d => populationScale(0) - populationScale(d.population))
.attr('fill', 'steelblue');
// add a group for the y-axis
svg.append('g')
.attr('transform', `translate(${marginPop.left})`)
.call(yAxisPop);
// add a group for the x-axis
svg.append('g')
.attr('transform', `translate(0,${heightPop - marginPop.bottom})`)
.call(d3.axisBottom(countryScaleBand))
.append('text')
.attr('fill', 'black')
.attr('font-family', 'sans-serif')
.attr('x', widthPop / 2)
.attr('y', 40)
.text("Countries");

// calculate the mean score
const meanPopulation = d3.mean(populations, d => d.population);
// add a line for the mean
svg.append('line')
.attr('x1', marginPop.left)
.attr('x2', widthPop - marginPop.right)
.attr('y1', populationScale(meanPopulation))
.attr('y2', populationScale(meanPopulation))
.attr('stroke', 'red');
return svg.node();
}
Insert cell
Insert cell
{
// create and select an svg element
const svg = d3.create('svg')
.attr('width', widthPop)
.attr('height', heightPop);
// bind our data to lines
svg.append('g')
.selectAll('line')
.data(populations)
.join('line')
.attr('x1', d => countryScale(d.country))
.attr('x2', d => countryScale(d.country))
.attr('y1', d => populationScale(d.population))
.attr('y2', d => heightPop - marginPop.bottom)
.attr('stroke', 'lightgray');
// bind our data to circles
svg.append('g')
.selectAll('circle')
.data(populations)
.join('circle')
.attr('cx', d => countryScale(d.country))
.attr('cy', d => populationScale(d.population))
.attr('r', 5)
.attr('fill', 'steelblue');
// calculate the mean score
const meanPopulation = d3.mean(populations, d => d.population);
// add a line for the mean
svg.append('line')
.attr('x1', marginPop.left)
.attr('x2', widthPop - marginPop.right)
.attr('y1', populationScale(meanPopulation))
.attr('y2', populationScale(meanPopulation))
.attr('stroke', 'red');
// add a group for the y-axis
svg.append('g')
.attr('transform', `translate(${marginPop.left})`)
.call(yAxisPop);
// add a group for the x-axis
svg.append('g')
.attr('transform', `translate(0,${heightPop - marginPop.bottom})`)
.call(xAxisPop)
.append('text')
.attr('fill', 'black')
.attr('font-family', 'sans-serif')
.attr('x', widthPop / 2)
.attr('y', 40)
.text("Countries");
return svg.node();
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
colorToCutToCount = d3.rollup(
diamonds,
group => group.length,
diamond => diamond.color,
diamond => diamond.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 = [...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
Array.from(colorToCutToCount)
Insert cell
maxCount = d3.max(
colorToCutToCount,
([color, cutToCount]) => d3.max(cutToCount, ([cut, count]) => count)
)
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
Insert cell
diamondsChart = {
// set up
const svg = d3.create('svg')
.attr('width', widthDiamond)
.attr('height', heightDiamond);
/*
Task A:
- add one g to svg for each group of bars
- use colorToCutToCount as the data
- position each group using the groupX scale
*/
const groups = svg.selectAll('g')
.data(colorToCutToCount)
.join('g')
.attr('transform', ([color, cutToCount]) => `translate(${groupX(color)})`);
/*
Task B:
- add the bars to each group
- set the x, width, y, height, and fill attributes
*/
groups.selectAll('rect')
.data(([color, cutToCount]) => cutToCount)
.join('rect')
.attr('x', d => barX(d[0]))
.attr('width', barX.bandwidth())
.attr('y', ([cut, count]) => y(count))
.attr('height', ([cut, count]) => y(0) - y(count))
.attr('fill', ([cut, count]) => color(cut));
// add axes
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