Public
Edited
Sep 27, 2023
3 forks
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
1+2
Insert cell
5
6
Insert cell
{
//a = "Bye"
let x = 1
x = x+1
return(x+2)
}
Insert cell
a = "Hello" // "Goodbye "
Insert cell
a
Insert cell
//a = "something else"
Insert cell
a + " World" // Reactive
Insert cell
b = c + 1
Insert cell
c = b + 1
Insert cell
c
Insert cell
myfunc = x => x+2
Insert cell
myfunc(1)
Insert cell
Insert cell
[1,2,3]
Insert cell
({a:1, b:2, c:3})
Insert cell
Insert cell
data = FileAttachment("States2010.csv").csv({typed: true})
Insert cell
data.length
Insert cell
data.columns
// header row
Insert cell
data.columns.length
Insert cell
Insert cell
viewof table1 = Inputs.table(data)
Insert cell
// interactively filtered table rows
table1
Insert cell
table1
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
table2
Insert cell
Insert cell
d3 = require("d3")
Insert cell
Insert cell
// Get the first 5 states
data.slice(4, 6)
Insert cell
Insert cell
// Which state is the wealthiest?
d3.max(data, r => r.IncomePerCapita)
Insert cell
data[d3.maxIndex(data, r => r.IncomePerCapita)]
Insert cell
// What is the total population of USA?
d3.sum(data, r => r.Population)
Insert cell
Insert cell
// Extract populations
data.map( (r,i,a) => r.Population)
Insert cell
data.map((r, i, a) => i)
Insert cell
// Find the population change from 2000 to 2010
data.map(r => r.Population - r.Pop2000)
Insert cell
// add new column to table. extra parens are important
data2 = data.map(r => ({...r, PopDelta: r.Population - r.Pop2000}))
Insert cell
Insert cell
// Which states are poor?
data.filter(r => r.IncomePerCapita < 30000)
Insert cell
Insert cell
// always sort a copy of the data
// need a comparison function
[...data].sort((a,b) => a.IncomePerCapita - b.IncomePerCapita)
Insert cell
Insert cell
// GroupBy, bin function
d3.group(data, r => Math.floor(r.IncomePerCapita / 10000))
Insert cell
// RollUp, aggregation function, bin function
d3.rollup(
data,
(g) => d3.mean(g, (r) => r.PercentCollegeGrad),
(r) => Math.floor(r.IncomePerCapita / 10000)
)
Insert cell
// RollUp, aggregation function, bin function
Array.from(d3.rollup(data, g => d3.mean(g, r=>r.PercentCollegeGrad), r => Math.floor(r.IncomePerCapita / 10000)))
Insert cell
Insert cell
alphabet
Insert cell
data
X
IncomePerCapita
Y
PercentCollegeGrad
Color
Size
Facet X
Facet Y
Mark
Auto
Type Chart, then Shift-Enter. Ctrl-space for more options.

Insert cell
Plot.plot({
marks: [
Plot.dot(data, { x: "IncomePerCapita", y: "PercentCollegeGrad", tip: true })
]
})
Insert cell
Plot.plot({
marks: [
Plot.barX(data, {y: "Name", x: "IncomePerCapita", sort: {y: "x", reverse: true}}),
Plot.ruleX([0])
],
height: 700,
marginLeft: 120,
style: { color: "steelblue" }
})
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