Plot data along a curved baseline
Draw a filled data curve perpendicular to a winding route; drag the window and the baseline and curve rebuild for the visible segment.
/**
* Demonstrates plotting data beside a baseline that is derived from the
* visible stretch of a winding route.
*
* Polylines.clipToBox finds the stretch of the route inside the window, from
* the first entry to the last exit, and Offsets.besideRoute builds a baseline
* beside it. Each data point has a ratio along the route; the visible ones
* are mapped to ratios along the baseline, and
* Curves.alongPolylineAtRatios / Curves.filledAlongPolylineAtRatios draw the
* values as distances from it: the outline and the band down to the baseline.
*
* The same `left` builds the baseline and plots the curve, so the curve
* always points away from the route. Dragging the window corners changes
* which stretch is in view; the buttons switch the side.
*/
import {
VisQuill, Reactive, Boxes, Points, Segments, Polylines,
Offsets, Curves, Svg,
} from "@visquill/visquill-gdk";
import "./styles.css";
// ── Layout ────────────────────────────────────────────────────────────────────
const POLYLINE_POINTS: [number, number][] = [
[ 60, 400], [120, 240], [190, 460], [270, 190],
[350, 360], [420, 150], [510, 420], [590, 220],
[670, 380], [750, 170], [830, 340], [900, 440],
];
const SMOOTHING = 3;
const WINDOW_TL: [number, number] = [220, 170];
const WINDOW_BR: [number, number] = [660, 470];
const OFFSET = 60;
const CORNER_POINTS = 6;
const SAMPLE_COUNT = 200;
const buttonWidth = 70;
const buttonHeight = 24;
const buttonGap = 6;
// ── Simulated data ────────────────────────────────────────────────────────────
//
// Values at ratios along the route (0 = start, 1 = end). In a real use case
// these would be measurements snapped to their position along the route.
const DATA = Array.from({ length: SAMPLE_COUNT }, (_, i) => {
const t = i / SAMPLE_COUNT;
const v = 60 * Math.exp(-((t - 0.25) ** 2) / 0.01)
+ 90 * Math.exp(-((t - 0.65) ** 2) / 0.015)
+ 20 * Math.sin(t * Math.PI * 8) * Math.exp(-t * 2);
return { pos: t, value: Math.max(0, v) };
});
export function run(div: HTMLDivElement) {
const rvg = VisQuill.create(div, "demos-curve-baseline-");
const canvas = rvg.canvas;
// ── Instructions ──────────────────────────────────────────────────────────
const instructions = canvas.text.label(
"Drag the window corners — the curve updates to the visible stretch",
"instructions"
);
Reactive.do([rvg.frame], () => {
Points.moveDown(Segments.midPoint(Boxes.topSide(rvg.frame)), 25, instructions);
});
// ── Source polyline ───────────────────────────────────────────────────────
const polyline = Polylines.smooth(POLYLINE_POINTS, SMOOTHING);
// The whole route dimmed, the stretch inside the window on top of it
Polylines.copy(polyline, canvas.visuals.polyline("route"));
const stretch = canvas.visuals.polyline("stretch");
// ── Draggable window ──────────────────────────────────────────────────────
const hTL = canvas.handles.disk("window-handle", WINDOW_TL);
const hBR = canvas.handles.disk("window-handle", WINDOW_BR);
const windowBox = canvas.visuals.box("window-rect");
Reactive.do([hTL, hBR], () => {
Boxes.spannedBox(hTL, hBR, windowBox);
});
// ── Side buttons ──────────────────────────────────────────────────────────
const left = canvas.values.bool(true);
const title = canvas.text.label("side", "row-title");
const buttons = ["left", "right"].map(name =>
({ box: canvas.visuals.box("button"), label: canvas.text.label(name, "button-label") }));
const note = canvas.text.label("", "note");
const notes = [
"Left of the direction of travel: the route runs left to right, so the curve lies above it.",
"Right of the direction of travel: below the route.",
];
Reactive.do([rvg.frame], () => {
const rowLeft = Points.move(Boxes.bottomLeft(rvg.frame), 20, -70);
Points.move(rowLeft, 0, buttonHeight / 2, title);
buttons.forEach(({ box, label }, i) => {
Boxes.box(Points.moveRight(rowLeft, 50 + i * (buttonWidth + buttonGap)), buttonWidth, buttonHeight, box);
Boxes.center(box, label);
});
Points.move(rowLeft, 50, buttonHeight + 16, note);
});
const select = (index: number) => {
buttons.forEach(({ box, label }, i) => {
const directive = i === index ? "@class active" : "@class-remove active";
box.applyStyleDirective(directive);
label.applyStyleDirective(directive);
});
note.value = notes[index];
left.value = index === 0;
};
buttons.forEach(({ box }, i) => Svg.get(box).addEventListener("click", () => select(i)));
select(0);
// ── Plot visuals ──────────────────────────────────────────────────────────
const baseline = canvas.visuals.polyline("baseline");
const curveFill = canvas.visuals.polygon("curve-fill");
const curveOutline = canvas.visuals.polyline("curve-outline");
// ── Reactive update ───────────────────────────────────────────────────────
const ranges: [number, number][] = [];
Reactive.do([windowBox, left], () => {
// ── 1. Stretch and baseline ───────────────────────────────────────────
Polylines.clipToBox(polyline, windowBox, ranges);
const visible = ranges.length === 0 ? [] : DATA.filter(d =>
d.pos >= ranges[0][0] && d.pos <= ranges[ranges.length - 1][1]);
if (visible.length < 2) {
stretch.elements.length = 0;
baseline.elements.length = 0;
curveFill.elements.length = 0;
curveOutline.elements.length = 0;
return;
}
const from = ranges[0][0];
const to = ranges[ranges.length - 1][1];
Polylines.slice(polyline, from, to, stretch);
Offsets.besideRoute(stretch, OFFSET, left, CORNER_POINTS, baseline);
// ── 2. Plot ───────────────────────────────────────────────────────────
//
// A ratio along the stretch is the same ratio along the baseline: both
// run from the stretch's start to its end. The values are distances
// from the baseline on the same side as the baseline itself.
const ratios = visible.map(d => (d.pos - from) / (to - from));
const values = visible.map(d => d.value);
Curves.alongPolylineAtRatios(baseline, ratios, values, left, curveOutline);
Curves.filledAlongPolylineAtRatios(baseline, ratios, values, left, curveFill);
});
}Data plotted along a baseline derived from the visible part of a route: its convex hull, offset
outward. Each value is placed sideways with Pins.rotateLeft and Pins.moveForward.
How it works
Polylines.clipToBox finds the stretch inside the window, from the first entry to the last exit. Offsets.besideRoute builds the baseline beside it (see geometry/polylines-09-convex-hull):
Polylines.slice(polyline, from, to, stretch);
Offsets.besideRoute(stretch, OFFSET, left, CORNER_POINTS, baseline);
Each data point has a ratio along the route. The stretch and the baseline both run from the stretch's start to its end, so a ratio along the stretch is used as the same ratio along the baseline:
const ratios = visible.map(d => (d.pos - from) / (to - from));
const values = visible.map(d => d.value);
Curves.alongPolylineAtRatios(baseline, ratios, values, left, curveOutline);
Curves.filledAlongPolylineAtRatios(baseline, ratios, values, left, curveFill);
The first call draws the outline, the second the band between it and the baseline. The same left that built the baseline puts the curve on its outer side, so the curve always points away from the route.
API
Polylines.clipToBox,Polylines.slice: the visible stretchOffsets.besideRoute: the baseline beside itCurves.alongPolylineAtRatios: the curve at ratios along the baselineCurves.filledAlongPolylineAtRatios: the band down to the baseline
See also
geometry/polylines-09-convex-hullplots/curves-04-filled-curve




