Public
Edited
Sep 30, 2023
10 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.select(DOM.svg(width, height))
const g = svg.append('g').attr('transform', `translate(${margin.left}, ${margin.top})`)
// Apart from aesthetics, routes serve as a trajectory for the moving particles.
// We'll compute particle positions in the next step
//
const route = g.append("g").attr('class', 'routes')
.attr("fill", "none")
.attr("stroke-opacity", .51)
.attr("stroke", "#eee")
.selectAll("path").data(routes)
.join("path")
// use custom sankey function here because we don't care of the node heights and link widths
.attr('d', sankeyLinkCustom)
.attr("stroke-width", bandHeight)
// Compute particle positions along the routes.
// This technic relies on path.getPointAtLength function that returns coordinates of a point on the path
// Another example of this technic:
// https://observablehq.com/@oluckyman/point-on-a-path-detection
//
route.each(function(nodes) {
const path = this
const length = path.getTotalLength()
const points = d3.range(length).map(l => {
const point = path.getPointAtLength(l)
return { x: point.x, y: point.y }
})
// store points for each route in the cache to use during the animation
const lastNode = nodes[nodes.length - 1]
const key = `${lastNode.data.name}`
cache[key] = { points }
})
// Create a container for particles first,
// to keep particles below the labels which are declared next
const particlesContainer = g.append('g')
// Labels
//
g.selectAll('.label').data(root.descendants().slice(1)) // `.slice(1)` to skip the root node
.join('g').attr('class', 'label')
.attr('transform', d => `translate(${d.y - bandHeight / 2}, ${d.x})`)
.attr('dominant-baseline', 'middle')
.attr('text-anchor', 'end')
// This is how we make labels visible on multicolor background
// we create two <text> with the same label
.call(label => label.append('text')
// the lower <text> serves as outline to make contrast
.attr('stroke', 'white')
.attr('stroke-width', 3)
.text(d => d.data.name))
// the upper <text> is the actual label
.call(label => label.append('text')
.attr('fill', '#444')
.text(d => d.data.name))
// Counters
//
const counters = g.selectAll('.counter').data(root.leaves())
.join('g').attr('class', 'counter')
.attr('transform', d => `translate(${width - margin.left}, ${d.x - bandHeight / 2})`)
.each(function(target, i) {
d3.select(this).selectAll('.group').data(d => d.data.groups)
.join('g').attr('class', 'group')
.attr('transform', (d, i) => `translate(${-i * 60}, 0)`)
// Align coutners to the right, because running numbers are easier for the eye to compare this way
.attr('text-anchor', 'end')
// Use monospaced font to keep digits aligned as they change during the animation
.style('font-family', 'Menlo')
// Add group titles only once, on the top
.call(g => i === 0 && g.append('text')
.attr('dominant-baseline', 'hanging')
.attr('fill', '#999')
.style('font-size', 9)
.style('text-transform', 'uppercase')
.style('letter-spacing', .7) // a rule of thumb: increase letter spacing a bit, when use uppercase
.text(d => d.key)
)
// Absolute counter values
.call(g => g.append('text').attr('class', 'absolute')
.attr('fill', d => colorScale(d.key))
.attr('font-size', 20)
.attr('dominant-baseline', 'middle')
.attr('y', bandHeight / 2 - 2)
.text(0) // will be updated during the animation
)
// Percentage counter values
.call(g => g.append('text').attr('class', 'percent')
.attr('dominant-baseline', 'hanging')
.attr('fill', '#999')
.attr('font-size', 9)
.attr('y', bandHeight / 2 + 9)
.text('0%') // will be updated during the animation
)
})

// Instead of `return svg.node()` we do this trick.
// It's needed to expose `update` function outside of this cell.
// It's Observable-specific, you can see more animations technics here:
// https://observablehq.com/@d3/learn-d3-animation?collection=@d3/learn-d3
//
return Object.assign(svg.node(), {
// update will be called on each tick, so here we'll perform our animation step
update(t) {
// add particles if needed
//
addParticlesMaybe(t)
// update counters
//
counters.each(function(d) {
const finished = particles
.filter(p => p.target.name === d.data.name)
.filter(p => p.pos >= p.length)
d3.select(this).selectAll('.group').each(function(group) {
const count = finished.filter(p => p.target.group === group.key).length
d3.select(this).select('.absolute').text(count)
d3.select(this).select('.percent').text(d3.format('.0%')(count / totalParticles))
})
})
// move particles
//
particlesContainer.selectAll('.particle').data(particles.filter(p => p.pos < p.length), d => d.id)
.join(
enter => enter.append('rect')
.attr('class', 'particle')
.attr('opacity', 0.8)
.attr('fill', d => d.color)
.attr('width', psize)
.attr('height', psize),
update => update,
exit => exit.remove()
)
// At this point we have `cache` with all possible coordinates.
// We just need to figure out which exactly coordinates to use at time `t`
//
.each(function(d) {
// every particle appears at its own time, so adjust the global time `t` to local time
const localTime = t - d.createdAt
d.pos = localTime * d.speed
// extract current and next coordinates of the point from the precomputed cache
const index = Math.floor(d.pos)
const coo = cache[d.target.name].points[index]
const nextCoo = cache[d.target.name].points[index + 1]
if (coo && nextCoo) {
// `index` is integer, but `pos` is float, so there are ticks when the particle is
// between the two precomputed points. We use `delta` to compute position between the current
// and the next coordinates to make the animation smoother
const delta = d.pos - index // try to set it to 0 to see how jerky the animation is
const x = coo.x + (nextCoo.x - coo.x) * delta
const y = coo.y + (nextCoo.y - coo.y) * delta
d3.select(this)
.attr('x', x)
.attr('y', y + d.offset)
}
})
}
})
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// Calculate x and y position of each node in the hierarchy
// Note, that `d3.cluster` builds a vertical hierarhy, but we render it horizontally,
// so we will switch `x` and `y` everywhere in the code
root = d3.cluster()
.separation(() => 1) // comment this line to layout the target nodes in groups
.size([height - margin.top - margin.bottom, width - margin.left - margin.right])(hierarchy)
Insert cell
// All routes from the root to a leaf
routes = root.leaves().map(l => root.path(l))
Insert cell
// Consider different groups of the same route as different targets
// Such data structure format simplifies particle creation and tracking
targetsAbsolute = root.leaves().flatMap(t => t.data.groups.map(g => ({
name: t.data.name,
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
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
// takes a group type (e.g. 'males' or 'females') and returns a color
colorScale = {
const groupTypes = ['females', 'males']
return d3.scaleOrdinal()
.domain(groupTypes)
.range(['plum', 'mediumslateblue'])
}
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, group }
const length = cache[target.name].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
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
bandHeight = 50
Insert cell
padding = 1.2 // padding between liks (1 = no padding)
Insert cell
height = margin.top + margin.bottom + hierarchy.leaves().length * (bandHeight * padding)
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