Public
Edited
Jul 13, 2024
4 stars
Insert cell
Insert cell
Inputs.table(olympians)
Insert cell
Insert cell
grouped = d3.groups(olympians, d => d.sport)
Insert cell
Insert cell
Inputs.table(grouped)
Insert cell
Insert cell
grouped1 = d3.groups(olympians, d => d.sport).map(([, olympians]) => {
const keys = Object.keys(olympians[0]);
return Object.fromEntries(keys.map(key => [key, olympians.map(d => d[key])]));
})
Insert cell
Inputs.table(grouped1, {layout: "fixed"})
Insert cell
Insert cell
data = d3.groups(olympians, d => d.sport).map(([sport, olympians]) => {
const keys = Object.keys(olympians[0]);
const {sport: _, ...columns} = Object.fromEntries(keys.map(key => [key, olympians.map(d => d[key])]));
return {sport, ...columns};
})
Insert cell
Inputs.table(data, {layout: "fixed"})
Insert cell
Insert cell
columns = Object.keys(data[0]).map(key => {
let d = data[0][key];
const isArray = Array.isArray(d);
if (isArray) d = d[0];
const type = d instanceof Date ? "date" : typeof d;
return ({key, isArray, type});
})
Insert cell
Insert cell
arrayFormats = ({
string: arr => [...new Set(arr)].join(", "),
date: arr => d3.extent(arr).map(d => d3.timeFormat("%x")(d)).join(" – "),
number: arr => d3.extent(arr).join(" – "),
object: arr => arr.toString()
})
Insert cell
Insert cell
format = Object.fromEntries(
columns.map(({ key, isArray, type }) => [
key,
isArray ? arrayFormats[type] : (d) => d.toString()
])
)
Insert cell
Insert cell
Inputs.table(data, {format, layout: "fixed", columns: ["sport", "sex", "date_of_birth", "height", "weight", "gold"]})
Insert cell
Insert cell
histogram = (key) => (arr, i, parentArr) =>
Plot.plot({
width: 80,
height: 20,
axis: null,
x: { domain: d3.extent(data.flatMap((d) => d[key])) },
marks: [
Plot.rectY(arr, Plot.binX({ y: "count" }, { inset: 0 })),
Plot.ruleY([0], { stroke: "#eee" })
]
})
Insert cell
Insert cell
arrayFormats1 = ({
string: (key) => (arr) => [...new Set(arr)].join(", "),
date: histogram,
number: histogram,
object: (key) => (arr) => arr.toString()
})
Insert cell
Insert cell
format1 = Object.fromEntries(
columns.map(({ key, isArray, type }) => [
key,
isArray ? arrayFormats1[type](key) : (d) => d.toString()
])
)
Insert cell
Insert cell
Inputs.table(data, {
format: format1,
layout: "fixed",
columns: ["sport", "sex", "date_of_birth", "height", "weight", "gold"]
})
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more