Plots

Radar chart around a circle

Plot values radially outward from a circle as a filled radar chart, and rotate the chart with a handle. TypeScript with VisQuill.

import {
    VisQuill, Reactive, Boxes, Segments, Points, Circles, Curves, Vectors,
} from "@visquill/visquill-gdk"
import './styles.css'

// ─── Layout ───────────────────────────────────────────────────────────────
// The chart sits in a layer whose anchor follows the centre of the frame,
// so its own centre is (0, 0).
const radius = 150

// Fixed radar-like values — irregular so the shape reads as real data.
const rawValues = [55, 90, 40, 75, 60, 95, 30, 80, 50, 70, 85, 45]
const values    = rawValues.map(v => ({ value: v }))

/**
 * Demonstrates `Curves.filledAlongCircle` and `Curves.alongCircleUniform`.
 *
 * A fixed circle serves as the baseline for a polar (radar) chart.
 * The light-blue filled area is produced by `filledAlongCircle`; the dark
 * outline on top by `alongCircleUniform`.  A handle on the circle perimeter
 * controls `angleOffset`, rotating the entire chart.  Dragging it shows how
 * the same dataset looks at different angular orientations.
 *
 * @param div - The div element that will contain the graphic.
 */
export function run(div: HTMLDivElement) {
    const rvg    = VisQuill.create(div, "demos-curves-circle-")
    const canvas = rvg.canvas

    // ─── Instruction label ─────────────────────────────────────────────
    const instructions = canvas.text.label(
        "Drag the handle on the circle to rotate the chart",
        "instructions"
    )
    Reactive.do([rvg.frame], () => {
        Points.moveDown(Segments.midPoint(Boxes.topSide(rvg.frame)), 25, instructions)
    })

    // ─── Chart layer, kept at the centre of the frame ─────────────────
    const chart = canvas.layer()
    Reactive.do([rvg.frame], () => {
        Vectors.copy(rvg.center, chart.anchor)
    })

    // ─── Circle baseline ───────────────────────────────────────────────
    const circleVisual = chart.visuals.circle("baseline-circle")
    const circle       = chart.shapes.circle()
    Circles.circleAt([0, 0], radius, circle)
    Circles.circleAt([0, 0], radius, circleVisual)

    // ─── Filled area + outline ─────────────────────────────────────────
    const filledArea   = chart.visuals.polygon("filled-area")
    const curveOutline = chart.visuals.polygon("curve-outline")

    // ─── Rotation handle — starts at 3 o'clock (angle 0) ──────────────
    const rotHandle   = chart.handles.disk("rot-handle", [radius, 0])
    const angleOffset = chart.values.real(0)

    // Keep the handle on the circle; its angle around the centre turns the chart
    Reactive.do([rotHandle], () => {
        Circles.closestPoint(circle, rotHandle, rotHandle)
        Points.angleAround([0, 0], rotHandle, angleOffset)
    })

    // ─── Chart ─────────────────────────────────────────────────────────
    Reactive.do([angleOffset], () => {
        Curves.filledAlongCircle(circle, values, angleOffset, true, filledArea)
        Curves.alongCircleUniform(circle, values, angleOffset, true, curveOutline)
    })
}

A radar chart with a circle as its baseline, so the values grow outward from the circle's edge. Curves.filledAlongCircle builds the area and Curves.alongCircleUniform the outline; a handle rotates the chart.

How it works

Twelve values are spread evenly around a fixed circle. Each value moves its point outward from the circle by that many pixels. Curves.filledAlongCircle returns the band between the circle and these points, Curves.alongCircleUniform the outline on top:

Curves.filledAlongCircle(circle, values, angleOffset, true, filledArea)
Curves.alongCircleUniform(circle, values, angleOffset, true, curveOutline)

true puts the points to the left of the travel direction, which on a circle is outward; false would point them inward.

The handle sets angleOffset, the angle of the first value. Its own reaction keeps it on the circle and reads its angle around the centre:

Circles.closestPoint(circle, rotHandle, rotHandle)
Points.angleAround([0, 0], rotHandle, angleOffset)

API

  • Curves.filledAlongCircle: the band between a circle and its offset curve
  • Curves.alongCircleUniform: the offset curve, values spread evenly around the circle
  • Circles.circleAt: the baseline from centre and radius
  • Circles.closestPoint: keeps the handle on the circle
  • Points.angleAround: the handle's angle, written into angleOffset

See also

  • plots/curves-01-along-polyline
  • animations/07-radar-charts

API used

Also used (14)