Published
Edited
Apr 4, 2022
4 forks
Importers
9 stars
Insert cell
Insert cell
data = d3.csvParse(`group,label,freq
Infants <1,0-1,16467
Children <11,1-11,30098
Teens 12-17,12-17,20354
Adults 18+,18+,12456
Elderly 65+,65+,12456`)
Insert cell
Insert cell
units = data.flatMap(d => d3.range(Math.round(d.freq / 1000)).map(() => d))
Insert cell
Insert cell
Plot.plot({
marks: [
Plot.cell(
units,
Plot.stackX({
y: (_, i) => i % 5,
fill: "label",
title: "group"
})
),
Plot.text(
units,
Plot.stackX({
y: (_, i) => i % 5,
text: "label"
})
)
],
x: { axis: null },
y: { axis: null },
color: { scheme: "warm" }
})
Insert cell
Insert cell
Plot.barX(data, { x: "freq", fill: "label", title: "group" }).plot({ color: { scheme: "warm" } })
Insert cell
Insert cell
Plot.plot({
facet: {
data: data2,
y: "facet"
},
marks: [
Plot.cell(
data2,
Plot.stackX({
transform: unitTransform("freq", 6, 1000),
y: "row",
fill: "label",
title: "group"
})
),
Plot.text(
data2,
Plot.stackX({
transform: unitTransform("freq", 6, 1000),
y: "row",
text: "label"
})
)
],
x: { axis: null },
y: { axis: null },
color: { scheme: "warm" }
})
Insert cell
// todo: use Plot.transform
transformFlatMap = (f) =>
function transform(data, facets) {
const unitFacets = [];
const unitData = [];
for (const index of facets) {
const facet = [];
for (const d of f(Array.from(index, (i) => data[i])).flat(1)) {
facet.push(unitData.push(d) - 1);
}
unitFacets.push(facet);
}
return { data: unitData, facets: unitFacets };
}
Insert cell
function unitTransform(value, rows = 5, n = 1) {
return transformFlatMap(data => {
let counter = 0;
return data.map(d =>
Array.from({ length: Math.round(d[value] / n) }, () => ({
...d,
row: counter++ % rows
}))
);
});
}
Insert cell
data2 = ["A", "B", "C"].flatMap(facet =>
data.map(d => ({ ...d, freq: 3000 + Math.random() * 20000, facet }))
)
Insert cell
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