Separate

Demers Cartogram

// Demonstrates Separate.boxes for a Demers cartogram: each town on a
// generated map is a square sized by its population, moved apart in x and
// y until no squares overlap. The years run from 2000 to 2040 and back;
// every frame passes the last layout as "previous", so the squares keep
// their sides of each other while they grow and shrink.

import {
    VisQuill, Reactive, Boxes, Points, Polygons, Values, Separate, Animate, Svg,
    type Box, type RvgAnimation,
} from "@visquill/visquill-gdk"
import { generateCountry } from "./towns"
import './styles.css'

// ─── Layout ───────────────────────────────────────────────────────────────
const maxSide      = 90      // side of the largest population of all years
const padding      = 1
const firstYear    = 2000
const lastYear     = 2040
const loop         = 24000   // ms, there and back
const buttonWidth  = 70
const buttonHeight = 24

export function run(div: HTMLDivElement) {
    const rvg    = VisQuill.create(div, "demos-separate-demers-")
    const canvas = rvg.canvas

    // ─── Map: outline and towns at their places ───────────────────────
    const country  = generateCountry(4)
    const towns    = country.towns
    const outline  = canvas.visuals.polygon("outline")
    const homes: Box[] = towns.map(() => canvas.shapes.box())
    const squares  = towns.map(t => canvas.visuals.box(`region-${t.region}`))
    const population = (i: number, year: number) => towns[i].population * Math.exp(towns[i].growth * (year - firstYear))
    const largest  = Math.max(...towns.map((_, i) => Math.max(population(i, firstYear), population(i, lastYear))))

    // The map's coordinates (x -1.3 to 1.3, y -0.9 to 0.9) scaled into the
    // frame below the controls.
    const center = canvas.values.point()
    const scale  = canvas.values.real(1)
    Reactive.do([rvg.frame], () => {
        Points.moveDown(Boxes.center(rvg.frame), 35, center)
        scale.value = Math.min(Boxes.width(rvg.frame).value / 2.8, (Boxes.height(rvg.frame).value - 150) / 1.9)
        Polygons.smooth(country.outline.map(p => Points.move(center, p.x * scale.value, p.y * scale.value)), 2, outline)
    })

    // ─── Year and placement ───────────────────────────────────────────
    const yearLabel = canvas.text.label("", "year")
    let year   = firstYear
    let placed = false
    const place = () => {
        // A square centred on the town, its area proportional to the population.
        towns.forEach((t, i) => {
            const side = Values.sqrtScale(population(i, year), largest, maxSide).value
            Boxes.box(Points.move(center, t.x * scale.value - side / 2, t.y * scale.value - side / 2), side, side, homes[i])
        })
        Separate.boxes(homes, {
            padding,
            previous: placed ? squares : undefined,
        }, squares)
        placed = true
        yearLabel.value = String(Math.round(year))
    }
    // A new frame size places the squares afresh.
    Reactive.do([center, scale], () => { placed = false; 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 note   = canvas.text.label("Square area = population · the west grows fast, the south shrinks", "note")
    Reactive.do([rvg.frame], () => {
        const left = Points.move(Boxes.topLeft(rvg.frame), 20, 30)
        Boxes.box(left, buttonWidth, buttonHeight, button)
        Boxes.center(button, label)
        Points.move(left, buttonWidth + 16, buttonHeight / 2, note)
        Points.move(Boxes.topRight(rvg.frame), -24, 45, yearLabel)
    })
    Svg.get(button).addEventListener("click", () => {
        if (animation) {
            animation.cancel()
            animation   = null
            label.value = "play"
        } else {
            play()
            label.value = "pause"
        }
    })
    play()
}

A Demers cartogram on a generated map: each town is a square whose area is its population. The squares are moved apart until none overlap. The years run from 2000 to 2040 and back; the west grows fast, the south shrinks.

How it works

Each square starts centred on its town (homes). Separate.boxes moves the squares in x and y so that they don't overlap, each as little as possible, and writes the result into the visuals:

Separate.boxes(homes, {
    padding,
    previous: placed ? squares : undefined,
}, squares)
  • padding: the gap between two squares.
  • previous: the last layout. Squares keep their sides of each other, so they don't jump while they grow and shrink. A new frame size places them afresh.

Notes

The map and the populations are generated (towns.ts). The squares are placed in every frame, without an animation between layouts.

API

  • Separate.boxes: moves boxes apart in x and y
  • Values.sqrtScale: square side from the population
  • Polygons.smooth: the country outline
  • Animate.progress.repeat: the year loop

See also

  • separate/separate-09-dorling

API used

Also used (22)