Published
Edited
Nov 22, 2020
Insert cell
Insert cell
Insert cell
Insert cell
line = d3.line()
.defined(d => !isNaN(d))
.x((d, i) => x(data.dates[i]))
.y(d => y(d))
Insert cell
d3 = require('d3@6')
Insert cell
color = "steelblue"
Insert cell
height = 500
Insert cell
margin = ({top: 30, right: 20, bottom: 30, left: 60})
Insert cell
x = d3.scaleUtc()
.domain(d3.extent(data.dates)).nice()
.range([margin.left, width - margin.right])
Insert cell
y = d3.scaleLinear()
.domain([0, d3.max(data.series, d => d3.max(d.values))]).nice()
.range([height - margin.bottom, margin.top])
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).ticks(width / 80).tickSizeOuter(0))
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y).ticks().tickFormat(d3.format('~s')))
.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(data.y))
Insert cell
import {select} from "@jashkenas/inputs"
Insert cell
full_data = d3.csvParse(await FileAttachment("population_csv.csv").text(), d3.autoType);
Insert cell
[...d3.group(full_data, d => d['Country Name']).keys()]
Insert cell
data = {
//const data = await d3.csv('https://datahub.io/core/population/r/population.csv', d3.autoType);
const data = full_data;
let series = d3.rollups(data, v => v.sort((a, b) => d3.ascending(a['Year'], b['Year'])), d => d['Country Name']);
series = d3.rollups(series, v => v[0][1].map(k => k['Value']), d => d[0])
series = series.map(d => ({name: d[0], values: d[1]}))
series = d3.filter(series, d => countries.includes(d.name))
return {
y: 'Population',
x: 'Time',
series: series,
dates: [ ...d3.group(data, d => d['Year']).keys()].map(d3.utcParse("%Y"))
}
}
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