Published
Edited
Oct 26, 2020
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
gap1960 = gapminder.filter(d => d.year === 1960);
Insert cell
d3.min(gap1960, d => d.fertility)
Insert cell
d3.max(gap1960, d => d.fertility)
Insert cell
height = 500
Insert cell
width = 500
Insert cell
xScale = d3.scaleLinear()
.domain(d3.extent(gap1960, d => d.life_expect)).nice()
.range([margin.left, width - margin.right]);
Insert cell
yScale = d3.scaleLinear()
.domain(d3.extent(gap1960, d => d.fertility)).nice()
.range([height - margin.bottom, margin.top]);
Insert cell
margin = ({ top: 25, right: 20, bottom: 35, left: 40 })
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(xScale).ticks(width / 80))
.call(g => g.select(".domain").remove())
.call(g => g.append("text")
.attr("x", width)
.attr("y", margin.bottom - 4)
.attr("fill", "currentColor")
.attr("text-anchor", "end")
.text(gap1960.xScale))
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(yScale))
.call(g => g.select(".domain").remove())
.call(g => g.append("text")
.attr("x", -margin.left)
.attr("y", 10)
.attr("fill", "currentColor")
.attr("text-anchor", "start")
.text(gap1960.yScale))
Insert cell
rScale = d3.scaleSqrt()
.domain(d3.extent(gap1960, d => d.pop))
.range([2, 30]);
Insert cell
chart = {
const svg = d3.select(DOM.svg(width, height));
svg.append("g")
.call(xAxis);

svg.append("g")
.call(yAxis);
svg.selectAll("circle")
.data(gap1960)
.join("circle")
.attr("cx", d => xScale(d.life_expect))
.attr("cy", d => yScale(d.fertility))
.attr("r", d => rScale(d.pop))
.attr("fill", "purple")
.attr("opacity", 0.6);
svg.append("text")
.text("Life expectancy →")
.attr("dx", width / 1.4)
.attr("dy", height - margin.top + margin.bottom - 14)
.attr("font-family", "sans-serif")
.attr("font-size", 10)
svg.append("text")
.text("Fertility ↑")
.attr("x", -margin.top + 40)
.style("text-anchor", "start")
.attr("y", 10)
.attr("font-family", "sans-serif")
.attr("font-size", 10)
return svg.node();
}


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