Chart Math
Smooth monotone and midpoint paths, scales, nice maxima and polar arc helpers for hand-written SVG charts.
npx shadcn@latest add https://ui.develop-better-solutions.com/r/dash-chart-math.jsonUsage
dash-chart-math-example.tsx
import { arcPath, linePath, midpointPath, monotonePath, niceMax, polar, scaleY, type Pt } from "@dbs/ui";
import { DashScope } from "./_store";
const VALUES = [22, 38, 30, 52, 44, 68, 61];
const W = 260;
const H = 110;
const max = niceMax(Math.max(...VALUES));
const pts: Pt[] = VALUES.map((v, i) => [(i / (VALUES.length - 1)) * W, scaleY(v, 0, max, H)]);
// The same points through three interpolations, plus an arc built with polar().
export default function DashChartMathExample() {
const [x, y] = polar(70, 250);
return (
<div className="w-full max-w-3xl">
<DashScope minHeight={0}>
<div className="grid gap-6 sm:grid-cols-3">
{[
["linePath", linePath(pts)],
["midpointPath", midpointPath(pts)],
["monotonePath", monotonePath(pts)],
].map(([label, d]) => (
<figure key={label} className="flex flex-col gap-2">
<svg viewBox={`-6 -6 ${W + 12} ${H + 12}`} className="w-full" role="img" aria-label={label}>
<path d={d} fill="none" stroke="var(--dash-bar)" strokeWidth="2.5" strokeLinejoin="round" />
</svg>
<figcaption className="text-xs text-[var(--dash-muted)]">{label}</figcaption>
</figure>
))}
</div>
<svg viewBox="0 0 200 200" className="mt-6 h-32" role="img" aria-label="arcPath">
<path d={arcPath(70, 0, 250)} fill="none" stroke="var(--dash-bar)" strokeWidth="16" strokeLinecap="round" />
<circle cx={x} cy={y} r="4" fill="var(--dash-ink)" />
</svg>
</DashScope>
</div>
);
}