Plots

Bar chart along a path

Grow one bar from each segment of a path so the chart follows its shape; drag the vertices and the bars follow. TypeScript with VisQuill.

import {
    VisQuill, Reactive, Boxes, Segments, Points, Bars, type Rectangle,
} from "@visquill/visquill-gdk"
import './styles.css'

// ─── Layout ───────────────────────────────────────────────────────────────
// 7 vertices → 6 segments → 6 bars.
const pathPoints: [number, number][] = [
    [120, 310], [280, 155], [470, 120], [660, 175],
    [800, 300], [750, 420], [350, 440],
]

const values = [55, 80, 45, 90, 60, 75].map(v => ({ value: v }))

/**
 * Demonstrates bars placed along a polyline, one per segment.
 *
 * Each bar is attached to the segment between two adjacent vertices via
 * `Bars.fromSegment`.  Dragging any vertex reshapes the segment it belongs
 * to and its bar follows.  The path is open — there is no bar on a closing
 * edge because no closing edge exists.
 *
 * @param div - The div element that will contain the graphic.
 */
export function run(div: HTMLDivElement) {
    const rvg    = VisQuill.create(div, "demos-bars-polyline-")
    const canvas = rvg.canvas

    // ─── Instruction label ─────────────────────────────────────────────
    const instructions = canvas.text.label(
        "Drag vertices to reshape · one bar per segment",
        "instructions"
    )
    Reactive.do([rvg.frame], () => {
        Points.moveDown(Segments.midPoint(Boxes.topSide(rvg.frame)), 25, instructions)
    })

    // ─── Vertex handles ────────────────────────────────────────────────
    const handles = pathPoints.map(pos => canvas.handles.disk("vertex", pos))

    // ─── Connector segments ────────────────────────────────────────────
    for (let i = 0; i < handles.length - 1; i++)
        canvas.visuals.segment("baseline", handles[i], handles[i + 1])

    // ─── Bar visuals — one per segment ─────────────────────────────────
    const barVisuals = Array.from({ length: values.length }, () =>
        canvas.visuals.polygon("bar")
    )

    // ─── Reactive block ────────────────────────────────────────────────
    Reactive.do([...handles], () => {
        for (let i = 0; i < values.length; i++)
            Bars.fromSegment(handles[i], handles[i + 1], values[i], true, barVisuals[i] as Rectangle)
    }, true)
}

A bar chart along an open path, one bar per segment. Bars.fromSegment takes position, direction and width from the segment, so the bars follow when a vertex is dragged.

How it works

Bars.fromSegment takes the two end points of a segment and a height. The segment is the base of the bar, so its width and direction come from the two points. The vertices are handles, so each bar follows them.

Reactive.do([...handles], () => {
    for (let i = 0; i < values.length; i++)
        Bars.fromSegment(handles[i], handles[i + 1], values[i], true, barVisuals[i] as Rectangle)
}, true)

Seven vertices give six segments and six bars. With left set to true, each bar grows to the left of the direction from handles[i] to handles[i + 1].

API

  • Bars.fromSegment: a bar whose base is a segment, with the given height

See also

  • plots/bars-03-along-polygon
  • plots/bars-01-anchoring

API used

Also used (11)