Developers · Releases · GDK 1.1
What's new in VisQuill GDK 1.1
GDK 1.1 adds functions that place shapes and labels, grids as plain data, helpers for profiles along routes, and more control over animations and text. This page shows what each part is for. The API reference lists every option, and the lab has a live demo for most of them.
Before you update, read the breaking changes in the CHANGELOG. They concern Points.mapToCircle, Values.modulus, circles in Arcs functions, and the file name of the UMD build.
The computations on this page take the current input and return a result. They do not update by themselves. To keep a result up to date, call the function inside Reactive.do. Where a function takes a result argument, visuals passed there show the result directly.
On this page: Placing shapes without overlap · Grids · Lines and routes · Animation · Text and labels · Smaller additions · Other changes
Placing shapes without overlap
Separate
Separate moves shapes apart so that they don't overlap, each one as little as possible.
Separate.circlesmoves circles in any direction, e.g. for a Dorling cartogram. Optionally they stay inside an area (within), keep a preferred gap (spacing), or move less by weight (weights).Separate.boxesdoes the same for boxes, e.g. for a Demers cartogram or glyphs at map points.Separate.circlesAlongAxiskeeps circles at their place along an axis and moves them off it, e.g. a beeswarm.Separate.boxesAlongAxismoves boxes along an axis only, e.g. labels at the end of lines.
While the input changes (dragging, new data), pass the last result as previous. Shapes that nothing pushes then stay where they are. The result can still change in steps, so animate towards it, for example with Animate.shape.followAll.
import { VisQuill, Separate, Animate, Reactive, Circles, Polygons, Circle } from "@visquill/visquill-gdk";
declare const container: HTMLDivElement;
declare const countries: { x: number; y: number; r: number }[];
declare const values: { x: number; r: number }[];
const graphic = VisQuill.create(container, "ex-");
const canvas = graphic.canvas;
const frame = Polygons.polygon([[0, 0], [800, 0], [800, 500], [0, 500]]);
// Dorling map: circles at their countries, moved apart inside the frame.
const homes = countries.map(c => Circles.circleAt([c.x, c.y], c.r));
const bubbles = countries.map(() => canvas.visuals.circle("bubble"));
let last: Circle[] | undefined;
export function update() {
last = Separate.circles(homes, { padding: 1, within: { polygon: frame }, previous: last });
Animate.shape.followAll(bubbles, last); // the result can jump; following it keeps the motion smooth
}
update();
// Beeswarm: dots along the x axis, moved off it; the dragged dot stays where it is.
const dots = values.map(v => Circles.circleAt([v.x, 250], v.r));
const swarm = values.map(() => canvas.visuals.circle("dot"));
const dragged = canvas.handles.disk("handle", [400, 250]);
let placed: Circle[] = Separate.circlesAlongAxis(dots, "x", { padding: 1 });
Reactive.do([dragged], () => {
const obstacle = Circles.circleAt(dragged, 10);
placed = Separate.circlesAlongAxis(dots, "x", {
padding: 1, obstacles: [obstacle], previous: { circles: placed, keep: "order" },
});
Animate.shape.followAll(swarm, placed);
});
Live in the Lab: Dorling Cartogram · Beeswarm · Donut chart labels that make room for each other · Demers Cartogram
Pack
Pack places circles tightly next to each other.
Pack.circlesAroundpacks circles around a point and keeps their sizes, e.g. packed bubbles. Withgroups, each group forms its own cluster.Pack.circlesInCirclepacks circles and scales the whole packing to fit a circle.Pack.circlesAlongCircle,circlesAlongArc,circlesAlongPolygonandcirclesAlongPolylineplace circles on a curve in the order of their positions, e.g. a necklace map around a region.Circles.enclosingreturns the smallest circle around a set of circles, e.g. an outline around packed bubbles.
import { VisQuill, Pack, Circles, Animate } from "@visquill/visquill-gdk";
declare const container: HTMLDivElement;
declare const items: { value: number; continent: number }[];
declare const regions: { x: number; y: number; value: number }[];
const graphic = VisQuill.create(container, "ex-");
const canvas = graphic.canvas;
// Packed bubbles, one cluster per continent, with an outline around all of them.
const sizes = items.map(d => Circles.circleAt([0, 0], Math.sqrt(d.value)));
const packed = items.map(() => canvas.visuals.circle("bubble"));
const outline = canvas.visuals.circle("outline");
Pack.circlesAround(sizes, [400, 300], { padding: 1, groups: items.map(d => d.continent), groupPadding: 6 }, packed);
Circles.enclosing(packed, outline);
// When the values change, pack again and animate towards the new packing.
export function update(next: number[]) {
const target = Pack.circlesAround(next.map(v => Circles.circleAt([0, 0], Math.sqrt(v))), [400, 300], { padding: 1 });
Animate.shape.morphAll(packed, target, 600);
}
// Necklace map: one circle per region on a ring around the map, in the order of the regions.
const ring = Circles.circleAt([400, 300], 260);
const symbols = regions.map(r => Circles.circleAt([r.x, r.y], Math.sqrt(r.value)));
const onRing = regions.map(() => canvas.visuals.circle("symbol"));
Pack.circlesAlongCircle(symbols, ring, { padding: 2, maxScale: 1 }, onRing);
Live in the Lab: Packed Bubbles · Circle packing in groups · Necklace Map
Declutter
Declutter finds which labels can be shown at a zoom level without overlap. It returns, for each shape, the smallest zoom at which it is shown. You pass one priority per shape; where two shapes collide, the one with the higher priority stays. Compute this once, then show or hide the labels when the zoom changes.
Declutter.boxesandDeclutter.circlesfor labels and symbols on a map.Declutter.boxesAlongAxisandDeclutter.circlesAlongAxisfor a timeline or another one-axis zoom.
import { VisQuill, Declutter, Reactive } from "@visquill/visquill-gdk";
declare const container: HTMLDivElement;
declare const cities: { name: string; x: number; y: number; population: number }[];
const graphic = VisQuill.create(container, "ex-");
const canvas = graphic.canvas;
const zoom = canvas.values.real(1);
// Compute once which label to show from which zoom on, then show and hide on zoom.
const labels = cities.map(c => canvas.text.labelAt(c.name, [c.x, c.y], "city", { anchor: "start" }));
const minScale = Declutter.boxes(labels.map(l => l.extent), cities.map(c => c.population), { padding: 4, anchor: [0, 0.5] });
Reactive.do([zoom], () => {
labels.forEach((label, i) => { label.visible.value = zoom.value >= minScale[i]; });
});
Live in the Lab: City labels shown by zoom level · Zoomable Timeline
Grids
In Grids, a grid is plain data, namely two arrays with the positions of the column and row lines. Functions find the cell of a point, return cells as boxes, count points per cell, or snap a point to the grid. Any sorted arrays of positions work as lines, e.g. the tick positions of an axis.
Other functions resize grids, animate them, or map between them:
Grids.fromWeights: column and row sizes proportional to weights, e.g. a Marimekko chart.Grids.fit: sizes from a preferred size with minimum, maximum, grow and shrink, like CSS flexbox.Grids.focusandGrids.focusAt: one cell enlarged and its neighbours shrinking, e.g. a fisheye under the pointer.Grids.lerp: animate between two grids.Grids.mapandGrids.mapShape: move points and shapes from one grid to another, so a drawing follows the grid.
import { VisQuill, Grids, Reactive, Boxes, Shape } from "@visquill/visquill-gdk";
declare const container: HTMLDivElement;
declare const drawing: Shape[]; // shapes drawn on the undistorted grid
const graphic = VisQuill.create(container, "ex-");
const canvas = graphic.canvas;
const plot = Boxes.box([0, 0], 800, 500);
// Fisheye: the cell under the pointer grows, its neighbours fade out, and the drawing follows.
const base = Grids.divided(plot, 20, 12);
const lens = Grids.divided(plot, 20, 12);
const pointer = canvas.values.point([400, 250]);
const shapes = drawing.map(() => canvas.visuals.polygon("shape"));
const gridLines = Grids.lines(base).map(() => canvas.visuals.segment("grid"));
Reactive.do([pointer], () => {
Grids.focusAt(base, pointer, 3, 0.5, lens);
drawing.forEach((s, i) => Grids.mapShape(base, lens, s, shapes[i]));
Grids.lines(lens, gridLines);
});
// Column widths like CSS flex: preferred size, with limits and how much they grow.
export const table = Grids.fit(plot,
[{ preferred: 200, min: 120 }, { preferred: 300, grow: 2 }, { preferred: 100, grow: 0, shrink: 0 }],
[{ preferred: 500 }]);
Live in the Lab: Fisheye heatmap with an animated grid · Marimekko Chart · Grids: Cells · Grids: Fit · Grids: Map
Lines and routes
Simplify
Simplify reduces long lines to fewer points. It returns the indices of the points to keep, which also select the matching data rows.
Simplify.minMaxkeeps the lowest and highest point per x range. Keep four points per pixel of plot width, and the line looks the same as the full one.Simplify.lttbkeeps a given number of points that look like the whole line.Simplify.douglasPeuckerkeeps the points needed so that no point is farther than a tolerance from the line.Simplify.envelopekeeps the upper (or lower) outline, e.g. the skyline of an elevation profile.Polylines.pickdraws the line through the kept points.
import { VisQuill, Simplify, Polylines } from "@visquill/visquill-gdk";
declare const container: HTMLDivElement;
declare const rows: { t: number; price: number }[];
declare const plotWidthPx: number;
const graphic = VisQuill.create(container, "ex-");
const line = graphic.canvas.visuals.polyline("price");
// A long series drawn with as many points as the plot has pixels; the indices also pick the data rows.
const points: [number, number][] = rows.map(r => [r.t, r.price]);
const keep = Simplify.minMax(points, 4 * plotWidthPx);
Polylines.pick(points, keep, line);
export const shownRows = keep.map(i => rows[i]);
Live in the Lab: Simplifying a Long Line
Profiles along routes
These functions draw data beside a route, e.g. an elevation profile beside a trail on a map.
Offsets.besideRoutecreates a smooth baseline at a distance beside the route.Curves.alongPolylineAtRatiosandfilledAlongPolylineAtRatiosdraw values along that baseline.Polylines.pointBeside,parallelandbesideCoordinatesplace points and lines at a distance beside a shape.Polylines.clipToBoxreturns the parts of a route inside a box, e.g. the map window.
left means left of the direction of travel, so the profile stays on the same side of the route when it turns.
import { VisQuill, Polylines, Offsets, Curves, Reactive, Boxes } from "@visquill/visquill-gdk";
declare const container: HTMLDivElement;
declare const trail: [number, number][]; // on screen
declare const ratios: number[]; // where each height sample lies along the trail (0 … 1)
declare const elevations: number[]; // heights in px, one per ratio
const graphic = VisQuill.create(container, "ex-");
const canvas = graphic.canvas;
const route = Polylines.polyline(trail);
const view = canvas.values.point([0, 0]); // top left of the map window
const window = Boxes.box(view, 800, 500);
const baseline = canvas.visuals.polyline("baseline");
const band = canvas.visuals.polygon("profile");
// An elevation profile beside the trail, only for the part inside the map window.
Reactive.do([view], () => {
Boxes.box(view, 800, 500, window);
const [from, to] = Polylines.clipToBox(route, window)[0] ?? [0, 1];
Offsets.besideRoute(Polylines.slice(route, from, to), 12, true, 4, baseline);
// the samples inside the window, as ratios of the shown piece
const at: number[] = [], h: number[] = [];
ratios.forEach((r, i) => { if (r >= from && r <= to) { at.push((r - from) / (to - from)); h.push(elevations[i]); } });
Curves.filledAlongPolylineAtRatios(baseline, at, h, true, band);
});
Live in the Lab: Curved baseline beside a route · Chart along the visible part of a map route
Convex hulls
ConvexHulls.startAt returns a hull that starts in a given direction, and ConvexHulls.section the part of the hull between two directions, e.g. its upper half.
Live in the Lab: Convex hull of draggable points
Animation
Every animation now returns a handle. cancel() stops it, and done is a Promise that tells whether it completed or was cancelled. With await, animations can run in sequence.
Animate.shape.morphAll,Animate.point.easedAll,Animate.pin.easedAlland thefollowAllvariants animate many objects with one call and one handle.staggerstarts them one after another.Animate.shape.tracedraws a line along a route;Animate.pin.alongmoves a pin along a route, facing the direction of travel.Animate.progress.easedandAnimate.progress.repeatrun your own update function with the progress of an animation, e.g. for a pulse.
import { VisQuill, Animate, Polylines, Circles } from "@visquill/visquill-gdk";
declare const container: HTMLDivElement;
declare const routePoints: [number, number][];
declare const dotTargets: [number, number][];
const graphic = VisQuill.create(container, "ex-");
const canvas = graphic.canvas;
const route = Polylines.polyline(routePoints);
const line = canvas.visuals.polyline("route");
const marker = canvas.layer();
Circles.circleAt([0, 0], 6, marker.visuals.circle("marker")); // drawn around the layer's anchor
// Draw the route, then run the marker along it.
export async function play() {
const draw = Animate.shape.trace(line, route, 2500, { from: [0, 0], to: [0, 1] });
if (await draw.done) {
Animate.pin.along(marker.anchor, route, 4000, { from: 0, to: 1 });
}
}
// Many dots in one call, one after another, with one handle.
const dots = dotTargets.map(() => canvas.values.point([0, 0]));
const targets = dotTargets.map(p => canvas.values.point(p));
const intro = Animate.point.easedAll(dots, targets, 600, { stagger: 15 });
export function stopIntro() { intro.cancel(); }
// An animation of your own: a pulsing ring, back and forth until cancelled.
const ring = canvas.visuals.circle("ring");
export const pulse = Animate.progress.repeat(1200, t => { Circles.circleAt([200, 200], 8 + 6 * t, ring); }, { alternate: true });
VisQuill.clock controls the time of all animations. In manual mode, time moves only when you advance it, for example to capture an animation frame by frame, or in tests.
import { VisQuill, Animate, Boxes } from "@visquill/visquill-gdk";
declare const container: HTMLDivElement;
declare const heights: number[];
declare function captureFrame(): Promise<void>;
const canvas = VisQuill.create(container, "ex-").canvas;
const bars = heights.map((_, i) => canvas.visuals.box("bar", [40 + 30 * i, 400], [60 + 30 * i, 400]));
const targets = heights.map((h, i) => Boxes.box([40 + 30 * i, 400 - h], 20, h));
// Capture an animation frame by frame, e.g. for a video.
export async function record() {
VisQuill.clock.useManual();
Animate.shape.morphAll(bars, targets, 2000);
for (let frame = 0; frame < 120; frame++) {
VisQuill.clock.advanceFrames(1, 60); // the DOM is up to date when this returns
await captureFrame();
}
VisQuill.clock.useRealtime();
}
Live in the Lab: Trace a Route · Move a Pin Along a Route · Staggered Bars · Await a Sequence · Follow as a Group
Text and labels
- Labels take an options object as their last argument:
anchor,offset,flip,at,side. Withflip: "auto"a label turns upright when it would be upside down, and keeps its place. labelAlongCircle,labelAlongArcandlabelAlongPolygonwrite text along these shapes.sideputs the text outside or inside.label.extentis the box of a label as drawn. It updates when the text or the font changes.group.text.measurereturns the size of a text without creating a label, e.g. to plan a layout.graphic.readyturns true once the fonts are loaded; measure again then.
import { VisQuill, Reactive, Circles, Boxes } from "@visquill/visquill-gdk";
declare const container: HTMLDivElement;
declare const cities: { name: string; x: number; y: number }[];
const graphic = VisQuill.create(container, "ex-");
const canvas = graphic.canvas;
const ring = Circles.circleAt([300, 300], 200);
// A label along a circle, outside it, at the bottom (ratio 0.75), kept upright.
canvas.text.labelAlongCircle("North Atlantic", ring, "ring-label", { at: 0.75, side: "outside", offset: 6 });
// A label beside a point that turns upright when needed.
for (const c of cities) {
canvas.text.labelAt(c.name, [c.x, c.y], "city", { anchor: "start", offset: 4, flip: "auto" });
}
// Sizes before any label is shown: measure, and measure again once the fonts are loaded.
function widest() {
return Math.max(...cities.map(c => canvas.text.measure(c.name, "city").width));
}
Reactive.do([graphic.ready], () => console.log("widest name", widest()));
// A box behind a label that follows the label's text.
const title = canvas.text.labelAt("Title", [40, 40], "title");
const background = canvas.visuals.box("title-bg");
Reactive.do([title.extent], () => Boxes.pad(title.extent, 4, 4, 4, 4, background));
Live in the Lab: Labels Along Circles and Arcs · Labels Along a Polygon · Labels on a Pin
Smaller additions
Svg.iconturns SVG path data (e.g. from@mdi/js) into an icon. Its anchor sets the icon's centre, its rotation (the anchor's direction) and its size (the direction's length).- Masks take polygon regions:
group.mask.polygon. values.realArrayholds a list of numbers that updates in one step;Animate.realArrayanimates all of them.Values.sqrtScalegives a radius whose area is proportional to a value.- New geometry helpers build shapes from pins without angle math:
Pins.fromSegment,Polygons.regular,Arcs.arcAtPin,Triangles.fromPin(an arrowhead), and more in the CHANGELOG.
import { VisQuill, Svg, Animate, Polylines, Reactive } from "@visquill/visquill-gdk";
declare const container: HTMLDivElement;
declare const mdiMapMarker: string; // from @mdi/js
declare const routePoints: [number, number][];
const graphic = VisQuill.create(container, "ex-");
const canvas = graphic.canvas;
// An icon from SVG path data; its anchor places, turns and sizes it.
const icon = Svg.icon(canvas, mdiMapMarker, "icon");
Animate.pin.along(icon.anchor, Polylines.polyline(routePoints), 5000);
// A polygon region in a mask that follows draggable corners.
const corners = [canvas.handles.disk("h", [100, 100]), canvas.handles.disk("h", [300, 120]), canvas.handles.disk("h", [200, 300])];
const region = canvas.mask.polygon("white", corners);
Reactive.linkVertices(region);
import { VisQuill, Animate, Reactive, Values } from "@visquill/visquill-gdk";
declare const container: HTMLDivElement;
declare const values2020: number[];
declare const values2024: number[];
const graphic = VisQuill.create(container, "ex-");
const canvas = graphic.canvas;
// A reactive array of plain numbers: one update for all of them, animated together.
const values = canvas.values.realArray(values2020);
const bars = values2020.map(() => canvas.visuals.box("bar"));
Reactive.do([values], () => {
let i = 0;
for (const v of values) bars[i++].visible.value = v > 0;
});
export function toYear2024() { Animate.realArray.eased(values, values2024, 800); }
// Radius for a value so that the area is proportional (largest value → 40 px).
export const radius = Values.sqrtScale(120, 500, 40);
Live in the Lab: Polygon Lens
Other changes
- Handles are drawn above the other visuals of their group by default. To cover a handle, give a visual a
zIndexabove 1000. require("@visquill/visquill-gdk")works in CommonJS.- The full list, including fixes and known issues, is in the CHANGELOG.
