Line Dots
A line chart whose points each ride a spring, so a change ripples along the line, with a bubble that slides between points.
Preview
"use client";
import { useState } from "react";
import { useAutoplay } from "../demo-context";
import { LineDots } from "./LineDots";
const sets = [
[24, 38, 31, 56, 49, 72, 66, 84],
[52, 40, 61, 47, 78, 58, 88, 74],
[16, 30, 48, 42, 64, 60, 82, 96],
];
const labels = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug"];
export default function Demo() {
const [turn, setTurn] = useState(0);
useAutoplay(() => setTurn((t) => t + 1), 2000);
return <LineDots label="Signups" values={sets[turn % sets.length]} labels={labels} max={100} color="#7c5cff" />;
}Installation
npx @usefluid/cli add line-dotspnpm dlx @usefluid/cli add line-dotsyarn dlx @usefluid/cli add line-dotsbunx @usefluid/cli add line-dotsInstall the engine.
Terminal npm i @usefluid/motionCopy these files into your project.
components/fluid/line-dots/LineDots.tsx "use client"; import { useSpring } from "@usefluid/motion/react"; import { type CSSProperties, type KeyboardEvent, type RefObject, useCallback, useId, useLayoutEffect, useMemo, useRef, useState } from "react"; import { linePath, pointsOf } from "./line"; import styles from "./LineDots.module.css"; const H = 180; type Props = { label: string; values: number[]; labels: string[]; max?: number; format?: Intl.NumberFormatOptions; locale?: string; color?: string; }; function Dot({ i, value, live, paint, dot, }: { i: number; value: number; live: RefObject<number[]>; paint(): void; dot: (el: SVGCircleElement | null) => void; }) { useSpring( { v: value }, ({ v }) => { live.current[i] = v; paint(); }, { w: Math.max(9, 16 - i * 0.5), z: 0.62 }, ); return <circle ref={dot} r={5} className={styles.dot} />; } export function LineDots({ label, values, labels, max, format, locale, color }: Props) { const box = useRef<HTMLDivElement>(null); const line = useRef<SVGPathElement>(null); const area = useRef<SVGPathElement>(null); const bubble = useRef<HTMLSpanElement>(null); const dots = useRef<(SVGCircleElement | null)[]>([]); const live = useRef<number[]>([...values]); const at = useRef(values.length - 1); const [width, setWidth] = useState(400); const widthRef = useRef(width); const [active, setActive] = useState(values.length - 1); const gradient = `fluid-line-${useId().replace(/[^a-zA-Z0-9]/g, "")}`; const key = JSON.stringify(format ?? {}); const number = useMemo(() => new Intl.NumberFormat(locale, JSON.parse(key)), [locale, key]); const top = max ?? Math.max(1, ...values); const current = Math.min(active, values.length - 1); const paint = useCallback(() => { const w = widthRef.current; const pts = pointsOf(live.current.slice(0, values.length), top, w, H); if (!pts.length) return; const d = linePath(pts); line.current?.setAttribute("d", d); area.current?.setAttribute("d", `${d}L${pts[pts.length - 1].x} ${H}L${pts[0].x} ${H}Z`); pts.forEach((p, i) => { dots.current[i]?.setAttribute("cx", `${p.x}`); dots.current[i]?.setAttribute("cy", `${p.y}`); }); if (bubble.current) { const f = Math.min(pts.length - 1, Math.max(0, at.current)); const a = pts[Math.floor(f)]; const b = pts[Math.ceil(f)]; const t = f - Math.floor(f); bubble.current.style.transform = `translate(${a.x + (b.x - a.x) * t}px, ${a.y + (b.y - a.y) * t}px)`; } }, [top, values.length]); useLayoutEffect(() => { const el = box.current; if (!el) return; const measure = () => { widthRef.current = el.clientWidth || 400; setWidth(widthRef.current); paint(); }; measure(); const observer = new ResizeObserver(measure); observer.observe(el); return () => observer.disconnect(); }, [paint]); useSpring( { i: current }, ({ i }) => { at.current = i; paint(); }, { w: 16, z: 0.75 }, ); const move = (e: KeyboardEvent, i: number) => { const next = e.key === "ArrowRight" ? i + 1 : e.key === "ArrowLeft" ? i - 1 : e.key === "Home" ? 0 : e.key === "End" ? values.length - 1 : -1; if (next < 0 || next >= values.length) return; e.preventDefault(); setActive(next); box.current?.querySelectorAll<HTMLElement>("button")[next]?.focus(); }; return ( <figure className={styles.root} style={color ? ({ "--fluid-accent": color, "--fluid-on-accent": "#ffffff" } as CSSProperties) : undefined}> <div ref={box} className={styles.plot} role="group" aria-label={label}> <svg viewBox={`0 0 ${width} ${H}`} className={styles.chart} aria-hidden> <defs> <linearGradient id={gradient} x1="0" y1="0" x2="0" y2="1"> <stop offset="0" style={{ stopColor: "var(--fluid-accent)", stopOpacity: 0.28 }} /> <stop offset="1" style={{ stopColor: "var(--fluid-accent)", stopOpacity: 0 }} /> </linearGradient> </defs> <path ref={area} fill={`url(#${gradient})`} /> <path ref={line} className={styles.line} /> {values.map((v, i) => ( <Dot key={labels[i] ?? i} i={i} value={v} live={live} paint={paint} dot={(el) => { dots.current[i] = el; }} /> ))} </svg> <span ref={bubble} className={styles.bubble} aria-hidden> <span>{values.length ? number.format(values[current]) : ""}</span> </span> <div className={styles.hits}> {values.map((v, i) => ( <button key={labels[i] ?? i} type="button" className={styles.hit} data-active={i === current ? "" : undefined} tabIndex={i === current ? 0 : -1} aria-label={`${labels[i] ?? i + 1}: ${number.format(v)}`} onPointerEnter={() => setActive(i)} onFocus={() => setActive(i)} onKeyDown={(e) => move(e, i)} /> ))} </div> </div> <div className={styles.axis} aria-hidden> {labels.map((l) => ( <span key={l}>{l}</span> ))} </div> </figure> ); }components/fluid/line-dots/line.test.ts import { describe, expect, it } from "vitest"; import { parsePath } from "../../packages/motion/src/index"; import { linePath, pointsOf } from "./line"; describe("pointsOf", () => { it("spreads points from edge to edge with the highest value near the top", () => { const pts = pointsOf([0, 5, 10], 10, 300, 120); expect(pts.map((p) => p.x)).toEqual([0, 150, 300]); expect(pts[2].y).toBeLessThan(pts[1].y); expect(pts[1].y).toBeLessThan(pts[0].y); expect(pts[0].y).toBeLessThanOrEqual(120); expect(pts[2].y).toBeGreaterThanOrEqual(0); }); it("puts a single value in the middle", () => { expect(pointsOf([4], 10, 300, 120)[0].x).toBe(150); }); }); describe("linePath", () => { it("draws a smooth line through every point that the path parser accepts", () => { const pts = pointsOf([3, 8, 5, 12], 12, 300, 120); const d = linePath(pts); expect(() => parsePath(d)).not.toThrow(); expect(d.startsWith(`M${pts[0].x} ${pts[0].y}`)).toBe(true); expect(d.endsWith(`${pts[3].x} ${pts[3].y}`)).toBe(true); }); it("returns an empty path for no points", () => { expect(linePath([])).toBe(""); }); });components/fluid/line-dots/line.ts export type Point = { x: number; y: number }; const PAD = 10; const r = (v: number) => Math.round(v * 100) / 100; export function pointsOf(values: number[], max: number, width: number, height: number): Point[] { const span = height - PAD * 2; return values.map((v, i) => ({ x: values.length === 1 ? width / 2 : r((i / (values.length - 1)) * width), y: r(height - PAD - Math.min(1, Math.max(0, v / (max || 1))) * span), })); } export function linePath(points: Point[]): string { if (!points.length) return ""; let d = `M${points[0].x} ${points[0].y}`; for (let i = 0; i < points.length - 1; i++) { const p0 = points[Math.max(0, i - 1)]; const p1 = points[i]; const p2 = points[i + 1]; const p3 = points[Math.min(points.length - 1, i + 2)]; d += `C${r(p1.x + (p2.x - p0.x) / 6)} ${r(p1.y + (p2.y - p0.y) / 6)} ${r(p2.x - (p3.x - p1.x) / 6)} ${r(p2.y - (p3.y - p1.y) / 6)} ${p2.x} ${p2.y}`; } return d; }components/fluid/line-dots/LineDots.module.css .root { display: grid; gap: 10px; width: min(100%, 520px); margin: 0; padding: 44px 22px 16px; border-radius: 24px; background: var(--fluid-surface); color: var(--fluid-text); font-family: var(--fluid-font); } .plot { position: relative; height: 180px; } .chart { display: block; width: 100%; height: 100%; overflow: visible; } .line { fill: none; stroke: var(--fluid-accent); stroke-width: 3; stroke-linecap: round; } .dot { fill: var(--fluid-surface); stroke: var(--fluid-accent); stroke-width: 3; } .bubble { position: absolute; top: 0; left: 0; pointer-events: none; } .bubble span { position: absolute; bottom: 12px; padding: 6px 10px; border-radius: 999px; background: var(--fluid-text); color: var(--fluid-surface); font-size: 13px; font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; transform: translateX(-50%); } .hits { position: absolute; inset: 0; display: flex; } .hit { flex: 1; padding: 0; border: 0; border-radius: 12px; background: none; cursor: pointer; } .hit:focus-visible { outline: 2px solid var(--fluid-focus); outline-offset: 2px; } .axis { display: flex; justify-content: space-between; color: var(--fluid-text-2); font-size: 12px; }Import the theme once, then point its variables at your own tokens.
layout.tsx import "@usefluid/motion/theme.css";
Usage
import { LineDots } from "./LineDots";
<LineDots label="Signups" values={[24, 38, 31, 56]} labels={["Jan", "Feb", "Mar", "Apr"]} max={100} />API reference
| Prop | Type | Default | Description |
|---|---|---|---|
label | string | — | Accessible name of the chart. |
values | number[] | — | The points, from left to right. Change them and the line flows to them. |
labels | string[] | — | One label per point, unique, shown under the chart. |
max | number | the largest value | The value at the top. Keep it fixed when the values change. |
format | Intl.NumberFormatOptions | — | How values are written, as in Intl.NumberFormat. |
locale | string | the browser's | Locale for the values. |
color | string | `--fluid-accent` | Colour of the line, the dots and the fill. |
Keyboard and screen readers
Each point is a button named with its label and value. Tab reaches the chart once; the arrow keys, Home and End move between points, and the bubble follows. With reduced motion the line changes at once.
Notes
The chart measures its width, so the dots stay round at any size. pointsOf and linePath in line.ts draw the line, if you want it in your own svg. Every colour comes from the --fluid-* variables, and the piece is yours to change.
How it is made
Every point has its own spring, a little slower from left to right, and the line and its fill are redrawn through the points on each frame, so the curve bends like a rope settling. The value bubble slides between points on a spring of its own. Each point is a button read with its label and value.
For your agent
For your agent
Add Line Dots to my project with the Fluid library. Use LineDots for the time series of my dashboard, with my real labels and a fixed max. Read my project first. Use my logo, my colours and my font through the --fluid-* variables. Keep 44px touch targets, visible focus and reduced motion. If I have no SVG logo, ask before making one up.