Animated stacked radar charts
Three draggable stacked radar charts with smooth transitions between datasets, sizes and ray lengths. TypeScript with VisQuill.
// Demonstrates stacked radar charts as draggable nodes.
// Three charts are placed on the canvas, each bound to a rotating data slice.
// Clicking the controls cycles through data sets and chart geometry with eased transitions.
import { VisQuill, Animate, Attach, Boxes, Reactive, Svg, type Point, type RvgGroup } from "@visquill/visquill-gdk"
import { createRadarChart, type RadarChart } from "./radar"
import './styles.css'
// ─── Data ─────────────────────────────────────────────────────────────────────
const NUMBER_OF_BLOCKS = 5
const NUMBER_OF_CURVES = 6
const NUMBER_OF_RAYS = 8
const blocks = generateBlocks(NUMBER_OF_BLOCKS, NUMBER_OF_CURVES, NUMBER_OF_RAYS, 0, 100)
const maxStackedValue = findMaxStackedValueTotal(blocks)
// ─── Chart positions ───────────────────────────────────────────────────────────
const LOCATIONS: Point[] = [
{ x: 220, y: 320 },
{ x: 780, y: 230 },
{ x: 580, y: 580 },
]
// ─── Buttons: each click moves on to the next value of its list ──────────────
const radii = [20, 50, 100, 0]
const lengths = [150, 200, 50, 100]
const BUTTON = { x: 10, y: 20, width: 140, height: 24, gap: 8 }
// ─── Entry point ───────────────────────────────────────────────────────────────
export function run(div: HTMLDivElement) {
const rvg = VisQuill.create(div, "demos-")
const canvas = rvg.layer("animations-radar-charts-")
// ── Reactive state ────────────────────────────────────────────────────────
const dataIndex = canvas.values.real(0)
const innerRadius = canvas.values.real(radii[0])
const rayLength = canvas.values.real(lengths[0])
// ── Chart nodes ───────────────────────────────────────────────────────────
const charts = LOCATIONS.map((point, index) => {
const chart = createRadarChart(canvas, {
numberOfRays: NUMBER_OF_RAYS,
numberOfCurves: NUMBER_OF_CURVES,
stackedCurves: true,
innerRadius: 20,
rayLength: 150,
})
// Draggable handle — makes each chart repositionable at runtime
const handle = canvas.handles.disk("chart-handle", point)
Attach.pointToPoint(chart.group.anchor, handle)
return { chart, index }
})
// ── Reactive bindings ─────────────────────────────────────────────────────
// Rotate through data blocks so each chart shows a different slice
Reactive.do([dataIndex], () => {
charts.forEach(({ chart, index }) => {
const block = blocks[(dataIndex.value + index) % blocks.length]
assignData(chart, block)
})
})
// Animate inner radius across all charts
Reactive.do([innerRadius], () => {
charts.forEach(({ chart }) =>
chart.innerRadius.value = innerRadius.value
)
})
// Animate ray length across all charts
Reactive.do([rayLength], () => {
charts.forEach(({ chart }) =>
chart.rayLength.value = rayLength.value
)
})
// ── Buttons ───────────────────────────────────────────────────────────────
// The data index jumps; radius and ray length are animated to their next value
cycleButton(canvas, 0, "Data", blocks.map((_, i) => i), "",
i => dataIndex.value = i)
cycleButton(canvas, 1, "Inner rim", radii, " px",
r => Animate.real.eased(innerRadius, r, 1000))
cycleButton(canvas, 2, "Rays", lengths, " px",
l => Animate.real.eased(rayLength, l, 1000))
const top = BUTTON.y + 3 * (BUTTON.height + BUTTON.gap) + 10
canvas.text.labelAt("Click a button for its next value · drag a chart to move it", [BUTTON.x, top], "note")
}
// ─── Button ───────────────────────────────────────────────────────────────────
// A box with a label "<title>: <value>". A click shows the next value of the list
// and passes it to onChange.
function cycleButton(group: RvgGroup, row: number, title: string, values: number[], unit: string,
onChange: (value: number) => void): void {
const top = BUTTON.y + row * (BUTTON.height + BUTTON.gap)
const box = group.visuals.box("button", [BUTTON.x, top], [BUTTON.x + BUTTON.width, top + BUTTON.height])
const label = group.text.labelAt("", Boxes.center(box), "button-label")
let index = 0
const show = () => label.value = `${title}: ${values[index]}${unit}`
show()
Svg.get(box).addEventListener("click", () => {
index = (index + 1) % values.length
show()
onChange(values[index])
})
}
// ─── Helpers ──────────────────────────────────────────────────────────────────
function assignData(chart: RadarChart, values: number[][]): void {
for (let i = 0; i < values.length; i++) {
for (let j = 0; j < values[i].length; j++) {
Animate.real.eased(chart.data[i][j], values[i][j] / maxStackedValue, 1000)
}
}
}
// ─── Data generation ──────────────────────────────────────────────────────────
function generateBlocks(
numberOfBlocks: number,
numberOfCurves: number,
numberOfRays: number,
minValue: number,
maxValue: number,
): number[][][] {
return Array.from({ length: numberOfBlocks }, () =>
Array.from({ length: numberOfCurves }, () =>
Array.from({ length: numberOfRays }, () =>
Math.floor(Math.random() * (maxValue - minValue + 1)) + minValue
)
)
)
}
function findMaxStackedValue(block: number[][]): number {
const numberOfRays = block[0].length
return Math.max(
...Array.from({ length: numberOfRays }, (_, rayIndex) =>
block.reduce((sum, curve) => sum + curve[rayIndex], 0)
)
)
}
function findMaxStackedValueTotal(data: number[][][]): number {
return Math.max(...data.map(findMaxStackedValue))
}Three draggable stacked radar charts that animate to new data. Radius, ray length and every data
value are numbers animated with Animate.real.eased, and one reaction per chart redraws it.
How it works
createRadarChart (in radar.ts) returns the chart's inputs as Real values: innerRadius, rayLength, and data, one value per curve and ray. One reaction redraws the chart whenever any of them changes:
Reactive.do(data.flat().concat(outerRadius), () => {
Circles.circleAt([0, 0], innerRadius, innerCircle)
Circles.circleAt([0, 0], outerRadius, outerCircle)
// …
updateCurves()
})
The rays are spread over the circles with Distribute.pointsOnCircle. A value is a ratio along its ray, and stacked curves add up the values below them:
ratio = options.stackedCurves ? ratio + value : value
Points.placeOnSegment(ray.at(0), ray.at(1), ratio, curve.at(rayIndex))
Changing the data animates every value with Animate.real.eased; the buttons animate the radius and the ray length to their next value. All values are divided by the largest stack in the data, so no stack goes past the outer circle.
API
Animate.real.eased: animates aRealto a target valueReactive.do: runs a function whenever its inputs changePoints.placeOnSegment: the point at a ratio along a segmentDistribute.pointsOnCircle: spreads points evenly over a circleCircles.circleAt: a circle from centre and radius
See also
animations/01-easing




