Chart along the visible part of a map route
Reproject a route on a MapLibre map every frame, find the part inside the viewport and draw a density chart along it.
/**
* Combines map reprojection, polyline clipping, a baseline beside the route
* and a curve plotted along it into a single interactive chart.
*
* A route is defined as lat/lng waypoints and reprojected to screen pixels
* on every render frame. Polylines.clipToBox finds the parts of the route
* inside the map frame; the stretch from the first entry to the last exit is
* the visible stretch. Offsets.besideRoute builds a baseline beside it, and
* Curves.alongPolylineAtRatios / Curves.filledAlongPolylineAtRatios plot the
* data on that baseline, so the chart follows the map on every pan and zoom.
*
* New concepts relative to the prerequisite demos:
* - Reactive.block to batch reprojection, clipping and plotting
* - The viewport frame as the window box (no explicit handles needed)
*/
import {
Reactive, Boxes, Polylines, Offsets, Curves,
} from "@visquill/visquill-gdk";
import { MapAdapter } from "../utils/map-adapter";
import "maplibre-gl/dist/maplibre-gl.css";
import "./styles.css";
import { ROUTE } from "./route";
// ── Simulated data ────────────────────────────────────────────────────────────
//
// Values at ratios along the route, from the source (0) to the mouth (1).
// The route reprojects every frame so pixel distances are not stable —
// ratios are the correct coordinate for map data.
const SAMPLE_COUNT = 600;
const DATA: { pos: number; value: number }[] = Array.from(
{ length: SAMPLE_COUNT }, (_, i) => {
const t = i / SAMPLE_COUNT;
const v = 70 * Math.exp(-((t - 0.35) ** 2) / 0.008)
+ 90 * Math.exp(-((t - 0.65) ** 2) / 0.012)
+ 15 * Math.sin(t * Math.PI * 10) * Math.exp(-t * 3);
return { pos: t, value: Math.max(0, v) };
}
);
// ── Constants ─────────────────────────────────────────────────────────────────
const SMOOTHING = 2;
const OFFSET = 70;
const CORNER_POINTS = 6;
const FRAME_INSET = 40;
// The chart lies right of the direction of flow (seen from the source)
const LEFT = false;
export function run(container: HTMLDivElement): void {
// ── 1. Mount ──────────────────────────────────────────────────────────────
const { map, rvg } = MapAdapter.create(container, "maps-focused-", {
center: [8.0, 49.8],
zoom: 7,
});
const canvas = rvg.canvas;
// ── 2. Route visuals ──────────────────────────────────────────────────────
const routeLayer = canvas.layer("route-");
const route = routeLayer.visuals.polyline("line");
const stretch = routeLayer.visuals.polyline("stretch");
// ── 3. Chart visuals ──────────────────────────────────────────────────────
const chartLayer = canvas.layer("chart-");
const baseline = chartLayer.visuals.polyline("baseline");
const curveFill = chartLayer.visuals.polygon("curve-fill");
const curveOutline = chartLayer.visuals.polyline("curve-outline");
// ── 4. Main update loop ───────────────────────────────────────────────────
//
// Runs on every MapLibre render frame. Reactive.block batches all writes
// so dependents fire only once per frame.
const ranges: [number, number][] = [];
map.onRender(() => {
Reactive.block(() => {
// a) Reproject
const screen = ROUTE.map(p => map.latLngToScreen(p));
Polylines.copy(Polylines.smooth(screen, SMOOTHING), route);
// b) Visible stretch: from the first entry into the inset frame
// to the last exit
Polylines.clipToBox(route, Boxes.inflate(rvg.frame, -FRAME_INSET, -FRAME_INSET), 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(route, from, to, stretch);
// c) Baseline beside the stretch and the chart on it; a ratio
// along the stretch is the same ratio along the baseline
Offsets.besideRoute(stretch, OFFSET, LEFT, CORNER_POINTS, 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);
});
});
}A chart along the part of the Rhine that is visible on the map, rebuilt on every map frame. The
visible slice is wrapped in an offset hull that serves as the baseline, and Reactive.block applies
each frame's update at once.
How it works
On every render frame, the route is projected to the screen, the visible stretch is found, and the chart is rebuilt. Reactive.block holds back the reactions until all of this is written, so nothing reacts to a half-updated state.
The visible stretch runs from the first entry into the map frame, inset by FRAME_INSET, to the last exit:
Polylines.clipToBox(route, Boxes.inflate(rvg.frame, -FRAME_INSET, -FRAME_INSET), ranges);
The chart is built as in plots/curves-05-along-baseline: a baseline beside the stretch, and the data plotted on it at ratios:
Offsets.besideRoute(stretch, OFFSET, LEFT, CORNER_POINTS, baseline);
// …
Curves.alongPolylineAtRatios(baseline, ratios, values, LEFT, curveOutline);
Curves.filledAlongPolylineAtRatios(baseline, ratios, values, LEFT, curveFill);
The data positions are ratios along the route (0 to 1), not pixels, because the route's length in pixels changes with every zoom. The baseline depends only on the stretch's points, so the chart moves with the map while panning.
API
Reactive.block: one batch of updates per framePolylines.clipToBox,Polylines.slice: the visible stretchOffsets.besideRoute: the baseline beside itCurves.alongPolylineAtRatios,Curves.filledAlongPolylineAtRatios: the chart
See also
plots/curves-05-along-baselinemaps/02-attached-polyline




