Unlisted
Edited
Jul 9, 2024
Insert cell
Insert cell
statewide_summary_econ_agg_tidy.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
selectedGroup = statewide_summary_econ_agg_tidy.filter(
(d) => d.Group === groupSelected
)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
import { caption } from "bae7cbe100f70aa1"
Insert cell
import { html, svg } from "@observablehq/htl"
Insert cell
import { Plot } from "@mkfreeman/plot-tooltip"
Insert cell
import { addTooltips } from "@mkfreeman/plot-tooltip"
Insert cell
html`<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css">`
Insert cell
html`<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css">`
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
raw = ({
"Enr TX HE": {
//290 total females, 250 total males
Certificate: { males: 83, females: 54 },
Associate: { males: 229, females: 319 },
Bachelors: { males: 756, females: 1139 },
"No HE Credential": { males: 1430, females: 1391 }
// Certificate: { males: 8340, females: 5356 },
// Associate: { males: 22941, females: 31930 },
// Bachelors: { males: 75576, females: 113912 },
// "No HE Credential": { males: 142972, females: 139132 }
// Certificate: { males: 8, females: 5 },
// Associate: { males: 23, females: 32 },
// Bachelors: { males: 76, females: 114 },
// "No HE Credential": { males: 143, females: 139 }
},
"Not Enr TX HE": {
"No HE Credential": { males: 1879, females: 1481 } //438K male HS grads,
// "No HE Credential": { males: 187938, females: 148140 } //438K male HS grads,
// "No HE Credential": { males: 188, females: 148 } //438K male HS grads,
}
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// Convert the raw data from nested object format into `d3-hierarchy` compatible format,
// so we can use the power of `d3` to traverse nodes and paths to calculate distribution of particles
hierarchy = {
const isLeaf = (d) => d.hasOwnProperty("males");
// converts an object { bitA, bitB, ... } into array [{ name: 'bitA', ... }, { name: 'bitB', ... }, ...]
// `d3.hierarchy` will use this array to build its data structure
const getChildren = ({ name, ...otherProps }) =>
isLeaf(otherProps)
? undefined // leaves have no children
: Object.entries(otherProps).map(([name, obj]) => ({ name, ...obj }));
const absolutePath = (d) =>
`${d.parent ? absolutePath(d.parent) : ""}/${d.data.name}`;
return (
d3
.hierarchy({ name: "HSGrads", ...raw }, getChildren)
// convert each nodes's data into universal format: `{ name, path, groups: [{ key, value }, ...] }`
// so it does not depend on exact group names ('males', 'females')
// later it will allow to reuse the chart with other groups
.each((d) => {
const datum = {
name: d.data.name,
// `path` is needed to distinguish nodes with the same name but different ancestors
// (e.g. /root/bit501/bit601 vs /root/bit502/bit601)
path: absolutePath(d)
};
if (isLeaf(d.data)) {
datum.groups = [
{
key: "males",
value: d.data.males
},
{
key: "females",
value: d.data.females
}
];
}
d.data = datum;
})
);
}
Insert cell
// Consider different groups of the same route as different targets
// Such data structure format simplifies particle creation and tracking
targetsAbsolute = hierarchy.leaves().flatMap(t => t.data.groups.map(g => ({
name: t.data.name,
path: t.data.path,
group: g.key,
value: g.value,
})))
Insert cell
targets = {
// normalize values
const total = d3.sum(targetsAbsolute, d => d.value)
return targetsAbsolute.map(t => ({ ...t, value: t.value / total }))
}
Insert cell
// Distribution of all possible types of particles (each route and each color)
thresholds = d3.range(targets.length).map(i => d3.sum(targets.slice(0, i + 1).map(r => r.value)))
Insert cell
Insert cell
// takes a random number [0..1] and returns a target, based on distribution
targetScale = d3.scaleThreshold()
.domain(thresholds)
.range(targets)
Insert cell
Insert cell
// Randomly add from 0 to `density` particles per tick `t`
addParticlesMaybe = (t) => {
const particlesToAdd = Math.round(Math.random() * density)
for (let i = 0; i < particlesToAdd && particles.length < totalParticles; i++) {
const target = targetScale(Math.random()) // target is an object: { name, path, group }
const length = cache[target.path].points.length

const particle = {
// `id` is needed to distinguish the particles when some of them finish and disappear
id: `${t}_${i}`,
speed: speedScale(Math.random()),
color: colorScale(target.group),
// used to position a particle vertically on the band
offset: offsetScale(Math.random()),
// current position on the route (will be updated in `chart.update`)
pos: 0,
// when the particle is appeared
createdAt: t,
// total length of the route, used to determine that the particle has arrived
length,
// target where the particle is moving
target,
}
particles.push(particle)
}
}
Insert cell
// Gets a list of the nodes from the root to a leaf and returns a path thru these nodes
sankeyLinkCustom = nodes => {
const p = d3.path()
const h = bandHeight / 2
nodes.forEach((n, i) => {
if (i === 0) {
p.moveTo(n.x0, n.y0 + h)
}
p.lineTo(n.x1, n.y0 + h)
const nn = nodes[i + 1]
if (nn) {
const w = nn.x0 - n.x1
p.bezierCurveTo(
n.x1 + w / 2, n.y0 + h,
n.x1 + w / 2, nn.y0 + h,
nn.x0, nn.y0 + h
)
}
})
return p.toString()
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
curve = 0.6 // [0..1] // 0 - smooth, 1 - square
Insert cell
Insert cell
bandHeight = 80 - padding / 2
Insert cell
Insert cell
height = margin.top + margin.bottom +
[...new Set(hierarchy.leaves().map(d => d.data.name))].length * (bandHeight + padding / 2) + padding / 2
Insert cell
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