Donut chart labels that make room for each other
A donut chart of a made-up city budget with its labels in two columns. Labels of small slices move apart along their column, with leader lines.
// Demonstrates Separate.boxesAlongAxis for chart labels: a donut chart of a
// made-up city budget, with its labels in two columns left and right of
// the ring. Each label wants to sit at the height of its slice; the labels
// of small neighbouring slices are pushed apart along the column, and leader
// lines lead back to the slices. The years run from 2000 to 2040 and back.
// With "off", every label stays at its slice's height and labels collide.
import {
VisQuill, Reactive, Boxes, Points, Arcs, Polylines, Separate, Animate, Svg,
type Box, type RvgAnimation,
} from "@visquill/visquill-gdk"
import { budget } from "./spending"
import './styles.css'
// ─── Layout ───────────────────────────────────────────────────────────────
const ringRadius = 150 // middle of the ring (its width is set in styles.css)
const ringWidth = 50
const sliceGap = 0.012 // radians between two slices
const columnGap = 70 // from the ring's outer edge to the label columns
const labelHeight = 24 // hard-coded label height in px (font size 20px)
const padding = 3
const firstYear = 2000
const lastYear = 2040
const loop = 24000 // ms, there and back
const buttonWidth = 70
const buttonHeight = 24
const buttonGap = 6
export function run(div: HTMLDivElement) {
const rvg = VisQuill.create(div, "demos-separate-donut-labels-")
const canvas = rvg.canvas
// ─── Slices, leader lines and labels ──────────────────────────────
const items = budget
const slices = items.map((_, i) => canvas.visuals.arc(`slice-${i % 6}`))
const leaders = items.map(() => canvas.visuals.polyline("leader"))
const labels = items.map(() => canvas.text.label("", "label"))
const yearLabel = canvas.text.label("", "year")
const center = canvas.values.point()
Reactive.do([rvg.frame], () => {
Points.moveDown(Boxes.center(rvg.frame), 20, center)
Points.copy(center, yearLabel)
})
let year = firstYear
let separate = true
const place = () => {
const t = (year - firstYear) / (lastYear - firstYear)
const shares = items.map(item => item.from + (item.to - item.from) * t)
const total = shares.reduce((sum, s) => sum + s, 0)
const outer = ringRadius + ringWidth / 2
// Slices clockwise from 12 o'clock; each label wants the height of
// its slice's middle, in the column on the slice's side.
const homes: { box: Box, right: boolean, anchor: { x: number, y: number } }[] = []
let start = -Math.PI / 2
shares.forEach((share, i) => {
const sweep = 2 * Math.PI * share / total
Arcs.arcAt(center, ringRadius, start + sliceGap / 2, sweep - sliceGap, slices[i])
const middle = start + sweep / 2
const anchor = Points.placeOnCircle(center, outer + 4, middle)
const right = Math.cos(middle) >= 0
homes.push({ box: Boxes.box({ x: 0, y: anchor.y - labelHeight / 2 }, 1, labelHeight), right, anchor })
labels[i].value = `${items[i].name} ${Math.round(100 * share / total)} %`
start += sweep
})
// One column per side: the boxes move along y only, between the
// top and the bottom of the column.
for (const right of [true, false]) {
const side = homes.map((home, i) => ({ ...home, i })).filter(home => home.right === right)
const boxes = side.map(home => home.box)
const placed = separate ? Separate.boxesAlongAxis(boxes, "y", {
padding,
min: center.y - outer - 40,
max: center.y + outer + 40,
}) : boxes
const sign = right ? 1 : -1
const columnX = center.x + sign * (outer + columnGap)
side.forEach((home, k) => {
const y = Boxes.center(placed[k]).y
Polylines.polyline([
home.anchor,
{ x: center.x + sign * (outer + columnGap - 24), y },
{ x: columnX - sign * 4, y },
], leaders[home.i])
Points.copy({ x: columnX, y }, labels[home.i])
labels[home.i].applyStyleDirective(right ? "@class-remove left" : "@class left")
})
}
yearLabel.value = String(Math.round(year))
}
Reactive.do([center], place)
// ─── Animation, with pause and play ───────────────────────────────
// The year eases from 2000 to 2040 and back, so the loop has no jump.
let progress = 0
let animation: RvgAnimation | null = null
const play = () => {
const start = progress
animation = Animate.progress.repeat(loop, t => {
progress = (start + t) % 1
year = firstYear + (lastYear - firstYear) * (1 - Math.cos(2 * Math.PI * progress)) / 2
place()
})
}
const button = canvas.visuals.box("button")
const label = canvas.text.label("pause", "button-label")
const title = canvas.text.label("separate", "row-title")
const toggles = ["on", "off"].map(name =>
({ box: canvas.visuals.box("button"), label: canvas.text.label(name, "button-label") }))
const note = canvas.text.label("", "note")
const notes = [
"Labels of small slices are pushed apart along their column (Separate.boxesAlongAxis).",
"Each label stays at the height of its slice: labels of small slices collide.",
]
Reactive.do([rvg.frame], () => {
const left = Points.move(Boxes.topLeft(rvg.frame), 20, 30)
Boxes.box(left, buttonWidth, buttonHeight, button)
Boxes.center(button, label)
const row = Points.moveDown(left, buttonHeight + 12)
Points.move(row, 0, buttonHeight / 2, title)
toggles.forEach(({ box, label }, i) => {
Boxes.box(Points.moveRight(row, 80 + i * (buttonWidth + buttonGap)), buttonWidth, buttonHeight, box)
Boxes.center(box, label)
})
Points.move(row, 80 + 2 * (buttonWidth + buttonGap) + 10, buttonHeight / 2, note)
})
const select = (index: number) => {
toggles.forEach(({ box, label }, i) => {
const directive = i === index ? "@class active" : "@class-remove active"
box.applyStyleDirective(directive)
label.applyStyleDirective(directive)
})
note.value = notes[index]
separate = index === 0
place()
}
toggles.forEach(({ box }, i) => Svg.get(box).addEventListener("click", () => select(i)))
select(0)
Svg.get(button).addEventListener("click", () => {
if (animation) {
animation.cancel()
animation = null
label.value = "play"
} else {
play()
label.value = "pause"
}
})
play()
}A donut chart of a made-up city budget from 2000 to 2040, with its labels in two columns beside the ring.
Separate.boxesAlongAxis moves the labels of small slices apart along their column, and the
off button shows them colliding.
How it works
Each label wants to sit at the height of its slice's middle, in the column on the slice's side. Its box has a
hard-coded height (24 px for 20 px text). Per column, Separate.boxesAlongAxis moves the boxes along y only:
const placed = separate ? Separate.boxesAlongAxis(boxes, "y", {
padding,
min: center.y - outer - 40,
max: center.y + outer + 40,
}) : boxes
"y": the boxes move along the column, keep their order, and each moves as little as possible.min,max: the column's top and bottom.- With
separateoff, the boxes stay where they are.
Notes
The budget is made up (spending.ts). A label stays in its column; a slice whose middle crossed 12 or 6 o'clock would
move its label to the other column.
API
Separate.boxesAlongAxis: moves boxes apart along one axisArcs.arcAt: the slicesPolylines.polyline: the leader linesAnimate.progress.repeat: the year loop
See also
separate/separate-01-peak-labels




