Public
Edited
Apr 14, 2021
Insert cell
md`# Mineral Bubbles`
Insert cell
chart = {
const svg = d3.create('svg')
.attr('viewBox', [0, 0, width, height]);
buildCircles(svg, topData, topY);
buildCircles(svg, bottomData, bottomY);
return svg.node();
}
Insert cell
function buildCircles(svg, data, y) {
let x = deltaX / 2;
for(let i = 0; i < data.length; i++) {
svg.append('g')
.selectAll('circle')
.data(data[i])
.join('circle')
.attr('cx', x)
.attr('cy', d => y(d['Happiness Score']))
.attr('r', 3)
.style('opacity', 0.1)
.attr('fill', 'white')
x += deltaX;
}
}
Insert cell
deltaX = (width - margin.left - margin.right) / data.length;
Insert cell
bottomY = d3.scaleLinear()
.domain(d3.extent(bottomData.flat(), d => d['Happiness Score']))
.range([height - margin.bottom, (height / 2) + halfMiddleSize]);
Insert cell
topY = d3.scaleLinear()
.domain(d3.extent(topData.flat(), d => d['Happiness Score']))
.range([(height / 2) - halfMiddleSize, margin.top]);
Insert cell
bottomData = data.map(d => d.filter(d2 => d2['Happiness Score'] < 5))
Insert cell
topData = data.map(d => d.filter(d2 => d2['Happiness Score'] >= 5))
Insert cell
data = await FileAttachment("alice-in-wonderland-hedonometer.json").json()
Insert cell
halfMiddleSize = (height * middlePercent) / 2
Insert cell
middlePercent = 0.1
Insert cell
height = 600
Insert cell
margin = ({top: 10, right: 10, bottom: 10, left: 10})
Insert cell
Insert cell
d3 = require('d3@6')
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