drafter-react 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +110 -2
  3. package/dist/charts/AreaChart.d.ts +12 -0
  4. package/dist/charts/AreaChart.js +35 -0
  5. package/dist/charts/BarChart.d.ts +36 -0
  6. package/dist/charts/BarChart.js +246 -0
  7. package/dist/charts/BoxPlotChart.d.ts +20 -0
  8. package/dist/charts/BoxPlotChart.js +50 -0
  9. package/dist/charts/BubbleChart.d.ts +15 -0
  10. package/dist/charts/BubbleChart.js +49 -0
  11. package/dist/charts/BulletChart.d.ts +15 -0
  12. package/dist/charts/BulletChart.js +42 -0
  13. package/dist/charts/CandlestickChart.d.ts +28 -0
  14. package/dist/charts/CandlestickChart.js +70 -0
  15. package/dist/charts/FunnelChart.d.ts +12 -0
  16. package/dist/charts/FunnelChart.js +40 -0
  17. package/dist/charts/GanttChart.d.ts +13 -0
  18. package/dist/charts/GanttChart.js +43 -0
  19. package/dist/charts/GaugeChart.d.ts +12 -0
  20. package/dist/charts/GaugeChart.js +71 -0
  21. package/dist/charts/Heatmap.d.ts +15 -0
  22. package/dist/charts/Heatmap.js +54 -0
  23. package/dist/charts/LineChart.d.ts +20 -0
  24. package/dist/charts/LineChart.js +65 -0
  25. package/dist/charts/PieChart.d.ts +20 -0
  26. package/dist/charts/PieChart.js +75 -0
  27. package/dist/charts/PolarAreaChart.d.ts +12 -0
  28. package/dist/charts/PolarAreaChart.js +36 -0
  29. package/dist/charts/RadarChart.d.ts +8 -0
  30. package/dist/charts/RadarChart.js +49 -0
  31. package/dist/charts/SankeyChart.d.ts +20 -0
  32. package/dist/charts/SankeyChart.js +114 -0
  33. package/dist/charts/ScatterPlot.d.ts +13 -0
  34. package/dist/charts/ScatterPlot.js +31 -0
  35. package/dist/charts/StepLineChart.d.ts +8 -0
  36. package/dist/charts/StepLineChart.js +32 -0
  37. package/dist/charts/StreamGraphChart.d.ts +12 -0
  38. package/dist/charts/StreamGraphChart.js +51 -0
  39. package/dist/charts/SunburstChart.d.ts +14 -0
  40. package/dist/charts/SunburstChart.js +69 -0
  41. package/dist/charts/TreemapChart.d.ts +13 -0
  42. package/dist/charts/TreemapChart.js +116 -0
  43. package/dist/core/colors.d.ts +29 -0
  44. package/dist/core/colors.js +40 -0
  45. package/dist/core/geometry.d.ts +16 -0
  46. package/dist/core/geometry.js +16 -0
  47. package/dist/core/paths.d.ts +21 -0
  48. package/dist/core/paths.js +74 -0
  49. package/dist/core/scales.d.ts +42 -0
  50. package/dist/core/scales.js +78 -0
  51. package/dist/core/types.d.ts +48 -0
  52. package/dist/core/types.js +20 -0
  53. package/dist/index.d.ts +34 -0
  54. package/dist/index.js +36 -0
  55. package/dist/svg/Chart.d.ts +30 -0
  56. package/dist/svg/Chart.js +21 -0
  57. package/dist/svg/primitives.d.ts +86 -0
  58. package/dist/svg/primitives.js +77 -0
  59. package/dist/svg/styles.d.ts +1 -0
  60. package/dist/svg/styles.js +40 -0
  61. package/package.json +52 -4
@@ -0,0 +1,69 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { Chart, delay, frameSize } from '../svg/Chart.js';
3
+ import { Label, RadialSweep, formatTick } from '../svg/primitives.js';
4
+ import { RadialLayout } from '../core/scales.js';
5
+ import { sectorPath } from '../core/paths.js';
6
+ import { paletteAt } from '../core/colors.js';
7
+ const REVEAL_MS = 900;
8
+ /** Segments narrower than this (radians) get no label. */
9
+ const LABEL_THRESHOLD = (18 * Math.PI) / 180;
10
+ const TURN = Math.PI * 2;
11
+ const TOP = -Math.PI / 2;
12
+ function positive(value) {
13
+ return Number.isFinite(value) && value > 0 ? value : 0;
14
+ }
15
+ function buildSegments(roots, hole, mid, outer) {
16
+ const total = Math.max(roots.reduce((a, b) => a + positive(b.value), 0), 0.0001);
17
+ const segments = [];
18
+ let before = 0;
19
+ roots.forEach((root, i) => {
20
+ const rootFraction = positive(root.value) / total;
21
+ const rootColor = root.color ?? paletteAt(i);
22
+ segments.push({
23
+ inner: hole,
24
+ outer: mid,
25
+ start: TOP + before * TURN,
26
+ sweep: rootFraction * TURN,
27
+ color: rootColor,
28
+ tinted: false,
29
+ label: root.label,
30
+ value: root.value,
31
+ });
32
+ const children = root.children ?? [];
33
+ const childTotal = Math.max(children.reduce((a, b) => a + positive(b.value), 0), 0.0001);
34
+ let childBefore = 0;
35
+ for (const child of children) {
36
+ const childFraction = positive(child.value) / childTotal;
37
+ segments.push({
38
+ inner: mid,
39
+ outer,
40
+ start: TOP + (before + childBefore * rootFraction) * TURN,
41
+ sweep: childFraction * rootFraction * TURN,
42
+ color: child.color ?? rootColor,
43
+ tinted: true,
44
+ label: child.label,
45
+ value: child.value,
46
+ });
47
+ childBefore += childFraction;
48
+ }
49
+ before += rootFraction;
50
+ });
51
+ return segments.filter((s) => s.sweep > 0);
52
+ }
53
+ /** A two-ring sunburst: roots on the inner ring, their children on the outer one. */
54
+ export function SunburstChart({ roots, ...frame }) {
55
+ const durationMs = frame.durationMs ?? REVEAL_MS;
56
+ const layout = new RadialLayout(frameSize(frame), 0.92);
57
+ const { x: cx, y: cy } = layout.center;
58
+ const maxRadius = layout.radius;
59
+ const segments = maxRadius > 0 ? buildSegments(roots, maxRadius * 0.22, maxRadius * 0.6, maxRadius) : [];
60
+ return (_jsxs(Chart, { ...frame, durationMs: durationMs, children: [_jsx(RadialSweep, { cx: cx, cy: cy, radius: maxRadius, children: segments.map((s, i) => {
61
+ const d = sectorPath(cx, cy, s.inner, s.outer, s.start, s.start + s.sweep);
62
+ return (_jsxs("g", { className: "d-mark", children: [_jsx("path", { d: d, fill: s.color }), _jsx("path", { d: d, fill: "#fff", fillOpacity: s.tinted ? 0.3 : 0, stroke: "#fff", strokeOpacity: 0.5 }), _jsx("title", { children: `${s.label}: ${formatTick(s.value)}` })] }, i));
63
+ }) }), _jsx("g", { className: "d-fade", style: delay(durationMs * 0.7), children: segments.map((s, i) => {
64
+ if (s.sweep < LABEL_THRESHOLD)
65
+ return null;
66
+ const at = layout.pointAt(s.start + s.sweep / 2, (s.inner + s.outer) / 2);
67
+ return _jsx(Label, { text: s.label, x: at.x, y: at.y, color: "currentColor", h: "center", v: "center" }, i);
68
+ }) })] }));
69
+ }
@@ -0,0 +1,13 @@
1
+ import { type ChartFrameProps } from '../svg/Chart.js';
2
+ /** One tile; its area is proportional to `value`. */
3
+ export interface TreemapItem {
4
+ readonly label: string;
5
+ readonly value: number;
6
+ /** Falls back to the palette (by descending value) when omitted. */
7
+ readonly color?: string;
8
+ }
9
+ export interface TreemapChartProps extends ChartFrameProps {
10
+ readonly items: readonly TreemapItem[];
11
+ }
12
+ /** A squarified treemap of rounded gradient tiles that pop in largest-first. */
13
+ export declare function TreemapChart({ items, ...frame }: TreemapChartProps): import("react").JSX.Element;
@@ -0,0 +1,116 @@
1
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
+ import { useId } from 'react';
3
+ import { Chart, delay, frameSize } from '../svg/Chart.js';
4
+ import { Label } from '../svg/primitives.js';
5
+ import { paletteAt } from '../core/colors.js';
6
+ const REVEAL_MS = 900;
7
+ const GAP = 4;
8
+ const CORNER = 8;
9
+ function sum(items) {
10
+ return items.reduce((a, b) => a + b.value, 0);
11
+ }
12
+ /** The worst tile aspect ratio if `row` were laid along a side of `sideLength`. */
13
+ function worstAspectRatio(row, sideLength, rowSum, rectArea, total) {
14
+ if (rowSum <= 0 || sideLength <= 0)
15
+ return Number.MAX_VALUE;
16
+ const thickness = (rectArea * (rowSum / total)) / sideLength;
17
+ if (thickness <= 0)
18
+ return Number.MAX_VALUE;
19
+ let worst = 0;
20
+ for (const item of row) {
21
+ const length = (rectArea * (item.value / total)) / thickness;
22
+ if (length > 0)
23
+ worst = Math.max(worst, thickness / length, length / thickness);
24
+ }
25
+ return worst;
26
+ }
27
+ function placeRow(row, rowRect, horizontal, out) {
28
+ const rowTotal = sum(row);
29
+ if (rowTotal <= 0)
30
+ return;
31
+ let cursor = horizontal ? rowRect.left : rowRect.top;
32
+ for (const item of row) {
33
+ const frac = item.value / rowTotal;
34
+ if (horizontal) {
35
+ const width = rowRect.width * frac;
36
+ out.push({ item, rect: { ...rowRect, left: cursor, width } });
37
+ cursor += width;
38
+ }
39
+ else {
40
+ const height = rowRect.height * frac;
41
+ out.push({ item, rect: { ...rowRect, top: cursor, height } });
42
+ cursor += height;
43
+ }
44
+ }
45
+ }
46
+ /** Squarified treemap layout (Bruls et al.): greedily grow a row while aspect ratios improve. */
47
+ function squarify(items, rect, out) {
48
+ const first = items[0];
49
+ if (!first || rect.width <= 0 || rect.height <= 0)
50
+ return;
51
+ if (items.length === 1) {
52
+ out.push({ item: first, rect });
53
+ return;
54
+ }
55
+ const total = sum(items);
56
+ if (total <= 0)
57
+ return;
58
+ const horizontal = rect.width >= rect.height;
59
+ const sideLength = horizontal ? rect.height : rect.width;
60
+ const rectArea = rect.width * rect.height;
61
+ let rowEnd = 1;
62
+ let rowSum = first.value;
63
+ let bestRatio = worstAspectRatio(items.slice(0, 1), sideLength, rowSum, rectArea, total);
64
+ while (rowEnd < items.length) {
65
+ const candidateSum = rowSum + (items[rowEnd]?.value ?? 0);
66
+ const ratio = worstAspectRatio(items.slice(0, rowEnd + 1), sideLength, candidateSum, rectArea, total);
67
+ if (ratio > bestRatio)
68
+ break;
69
+ bestRatio = ratio;
70
+ rowSum = candidateSum;
71
+ rowEnd += 1;
72
+ }
73
+ const row = items.slice(0, rowEnd);
74
+ const rest = items.slice(rowEnd);
75
+ const fraction = rowSum / total;
76
+ if (horizontal) {
77
+ const rowWidth = rect.width * fraction;
78
+ placeRow(row, { ...rect, width: rowWidth }, false, out);
79
+ squarify(rest, { ...rect, left: rect.left + rowWidth, width: rect.width - rowWidth }, out);
80
+ }
81
+ else {
82
+ const rowHeight = rect.height * fraction;
83
+ placeRow(row, { ...rect, height: rowHeight }, true, out);
84
+ squarify(rest, { ...rect, top: rect.top + rowHeight, height: rect.height - rowHeight }, out);
85
+ }
86
+ }
87
+ function formatValue(value) {
88
+ return `${Math.round(value * 10) / 10}`;
89
+ }
90
+ /** A squarified treemap of rounded gradient tiles that pop in largest-first. */
91
+ export function TreemapChart({ items, ...frame }) {
92
+ const gradientId = useId();
93
+ const durationMs = frame.durationMs ?? REVEAL_MS;
94
+ const size = frameSize(frame);
95
+ const inset = Math.min(size.width, size.height) * 0.04;
96
+ const sorted = items
97
+ .filter((item) => Number.isFinite(item.value) && item.value > 0)
98
+ .sort((a, b) => b.value - a.value);
99
+ const tiles = [];
100
+ squarify(sorted, { left: inset, top: inset, width: size.width - inset * 2, height: size.height - inset * 2 }, tiles);
101
+ return (_jsx(Chart, { ...frame, durationMs: durationMs, children: tiles.map(({ item, rect }, index) => {
102
+ const x = rect.left + GAP;
103
+ const y = rect.top + GAP;
104
+ const width = rect.width - GAP * 2;
105
+ const height = rect.height - GAP * 2;
106
+ if (width <= 1 || height <= 1)
107
+ return null;
108
+ const cx = x + width / 2;
109
+ const cy = y + height / 2;
110
+ const showLabel = width >= 48 && height >= 32;
111
+ const twoLines = height >= 48;
112
+ const color = item.color ?? paletteAt(index);
113
+ const box = { x, y, width, height, rx: Math.min(CORNER, width / 2, height / 2) };
114
+ return (_jsx("g", { className: "d-pop", style: delay((index / tiles.length) * 0.4 * durationMs), children: _jsxs("g", { className: "d-mark", children: [_jsxs("linearGradient", { id: `${gradientId}-${index}`, x1: 0, y1: 0, x2: 0, y2: 1, children: [_jsx("stop", { offset: 0, stopColor: color }), _jsx("stop", { offset: 1, stopColor: color, stopOpacity: 0.78 })] }), _jsx("rect", { ...box, fill: `url(#${gradientId}-${index})`, stroke: "#fff", strokeOpacity: 0.12 }), showLabel ? (_jsxs(_Fragment, { children: [_jsx(Label, { text: item.label, x: cx, y: twoLines ? cy - 7 : cy, color: "#fff", fontSize: 13, weight: 600, h: "center", v: "center" }), twoLines ? (_jsx("g", { opacity: 0.85, children: _jsx(Label, { text: formatValue(item.value), x: cx, y: cy + 9, color: "#fff", h: "center", v: "center" }) })) : null] })) : null, _jsx("title", { children: `${item.label}: ${formatValue(item.value)}` })] }) }, index));
115
+ }) }));
116
+ }
@@ -0,0 +1,29 @@
1
+ /**
2
+ * The Drafter palette, shared with the Compose, SwiftUI, Flutter and React
3
+ * Native ports so a chart looks the same on every platform. On the web every
4
+ * colour is a CSS colour string, so `var(--brand)` and `currentColor` work too.
5
+ */
6
+ export declare const DrafterColors: {
7
+ readonly blue: "#4c8df6";
8
+ readonly teal: "#2fc4c0";
9
+ readonly violet: "#7c6bf2";
10
+ readonly amber: "#f6b24c";
11
+ readonly green: "#49c17a";
12
+ readonly coral: "#f2766b";
13
+ readonly pink: "#ec6b9a";
14
+ readonly indigo: "#5b6bf0";
15
+ };
16
+ /** The ordered categorical sequence used when a mark doesn't pick a colour. */
17
+ export declare const drafterPalette: readonly string[];
18
+ /** Pick the i-th palette colour, wrapping around. */
19
+ export declare function paletteAt(i: number): string;
20
+ /**
21
+ * Theme colours, resolved in CSS so light/dark never needs JavaScript. Override
22
+ * them from any ancestor with `--drafter-grid`, `--drafter-label` and
23
+ * `--drafter-surface`.
24
+ */
25
+ export declare const theme: {
26
+ readonly grid: "var(--_dg)";
27
+ readonly label: "var(--_dl)";
28
+ readonly surface: "var(--_ds)";
29
+ };
@@ -0,0 +1,40 @@
1
+ /**
2
+ * The Drafter palette, shared with the Compose, SwiftUI, Flutter and React
3
+ * Native ports so a chart looks the same on every platform. On the web every
4
+ * colour is a CSS colour string, so `var(--brand)` and `currentColor` work too.
5
+ */
6
+ export const DrafterColors = {
7
+ blue: '#4c8df6',
8
+ teal: '#2fc4c0',
9
+ violet: '#7c6bf2',
10
+ amber: '#f6b24c',
11
+ green: '#49c17a',
12
+ coral: '#f2766b',
13
+ pink: '#ec6b9a',
14
+ indigo: '#5b6bf0',
15
+ };
16
+ /** The ordered categorical sequence used when a mark doesn't pick a colour. */
17
+ export const drafterPalette = [
18
+ DrafterColors.blue,
19
+ DrafterColors.teal,
20
+ DrafterColors.violet,
21
+ DrafterColors.amber,
22
+ DrafterColors.green,
23
+ DrafterColors.coral,
24
+ DrafterColors.pink,
25
+ DrafterColors.indigo,
26
+ ];
27
+ /** Pick the i-th palette colour, wrapping around. */
28
+ export function paletteAt(i) {
29
+ return drafterPalette[i % drafterPalette.length];
30
+ }
31
+ /**
32
+ * Theme colours, resolved in CSS so light/dark never needs JavaScript. Override
33
+ * them from any ancestor with `--drafter-grid`, `--drafter-label` and
34
+ * `--drafter-surface`.
35
+ */
36
+ export const theme = {
37
+ grid: 'var(--_dg)',
38
+ label: 'var(--_dl)',
39
+ surface: 'var(--_ds)',
40
+ };
@@ -0,0 +1,16 @@
1
+ /** A 2-D point in pixel space. */
2
+ export interface Point {
3
+ readonly x: number;
4
+ readonly y: number;
5
+ }
6
+ /** A width/height pair. */
7
+ export interface Size {
8
+ readonly width: number;
9
+ readonly height: number;
10
+ }
11
+ export declare function pt(x: number, y: number): Point;
12
+ /** Linear interpolation. */
13
+ export declare function lerp(a: number, b: number, t: number): number;
14
+ export declare function clamp(v: number, min: number, max: number): number;
15
+ /** Distance between two points. */
16
+ export declare function distance(a: Point, b: Point): number;
@@ -0,0 +1,16 @@
1
+ export function pt(x, y) {
2
+ return { x, y };
3
+ }
4
+ /** Linear interpolation. */
5
+ export function lerp(a, b, t) {
6
+ return a + (b - a) * t;
7
+ }
8
+ export function clamp(v, min, max) {
9
+ return v < min ? min : v > max ? max : v;
10
+ }
11
+ /** Distance between two points. */
12
+ export function distance(a, b) {
13
+ const dx = a.x - b.x;
14
+ const dy = a.y - b.y;
15
+ return Math.sqrt(dx * dx + dy * dy);
16
+ }
@@ -0,0 +1,21 @@
1
+ import type { Point } from './geometry.js';
2
+ /** A straight-segment path through `points`, as an SVG `d` string. */
3
+ export declare function polylinePath(points: readonly Point[]): string;
4
+ /**
5
+ * A Catmull-Rom smoothed path through `points`, expressed as cubic Béziers with
6
+ * the classic 1/6 tangent. The control-point math is identical to every other
7
+ * Drafter port, so curves match the Compose/Swift/Flutter output exactly.
8
+ */
9
+ export declare function smoothPath(points: readonly Point[]): string;
10
+ /**
11
+ * Closes an open top edge (from {@link smoothPath} or {@link polylinePath})
12
+ * down to `baseline` and back, producing the fillable area under a curve.
13
+ */
14
+ export declare function areaPath(line: string, firstX: number, lastX: number, baseline: number): string;
15
+ /** An open circular arc from angle `a0` to `a1` (radians, clockwise, 0 = +x). */
16
+ export declare function arcPath(cx: number, cy: number, r: number, a0: number, a1: number): string;
17
+ /**
18
+ * A closed annular sector between radii `r0` (inner, 0 for a pie wedge) and
19
+ * `r1` (outer), from angle `a0` to `a1` (radians, clockwise, 0 = +x).
20
+ */
21
+ export declare function sectorPath(cx: number, cy: number, r0: number, r1: number, a0: number, a1: number): string;
@@ -0,0 +1,74 @@
1
+ /** Two decimals is sub-pixel at any realistic chart size and keeps markup small. */
2
+ function n(v) {
3
+ return Math.round(v * 100) / 100;
4
+ }
5
+ /** A straight-segment path through `points`, as an SVG `d` string. */
6
+ export function polylinePath(points) {
7
+ let d = '';
8
+ points.forEach((p, i) => {
9
+ d += `${i === 0 ? 'M' : 'L'}${n(p.x)},${n(p.y)}`;
10
+ });
11
+ return d;
12
+ }
13
+ /**
14
+ * A Catmull-Rom smoothed path through `points`, expressed as cubic Béziers with
15
+ * the classic 1/6 tangent. The control-point math is identical to every other
16
+ * Drafter port, so curves match the Compose/Swift/Flutter output exactly.
17
+ */
18
+ export function smoothPath(points) {
19
+ if (points.length < 3)
20
+ return polylinePath(points);
21
+ const at = (i) => points[Math.min(Math.max(i, 0), points.length - 1)];
22
+ let d = `M${n(at(0).x)},${n(at(0).y)}`;
23
+ for (let i = 0; i < points.length - 1; i++) {
24
+ const p0 = at(i - 1);
25
+ const p1 = at(i);
26
+ const p2 = at(i + 1);
27
+ const p3 = at(i + 2);
28
+ d +=
29
+ `C${n(p1.x + (p2.x - p0.x) / 6)},${n(p1.y + (p2.y - p0.y) / 6)}` +
30
+ ` ${n(p2.x - (p3.x - p1.x) / 6)},${n(p2.y - (p3.y - p1.y) / 6)}` +
31
+ ` ${n(p2.x)},${n(p2.y)}`;
32
+ }
33
+ return d;
34
+ }
35
+ /**
36
+ * Closes an open top edge (from {@link smoothPath} or {@link polylinePath})
37
+ * down to `baseline` and back, producing the fillable area under a curve.
38
+ */
39
+ export function areaPath(line, firstX, lastX, baseline) {
40
+ if (!line)
41
+ return '';
42
+ return `${line}L${n(lastX)},${n(baseline)}L${n(firstX)},${n(baseline)}Z`;
43
+ }
44
+ /** An open circular arc from angle `a0` to `a1` (radians, clockwise, 0 = +x). */
45
+ export function arcPath(cx, cy, r, a0, a1) {
46
+ const sweep = a1 - a0;
47
+ if (Math.abs(sweep) >= Math.PI * 2 - 1e-6) {
48
+ // SVG cannot draw a full turn as one arc: split it into two halves.
49
+ const mid = a0 + sweep / 2;
50
+ return arcPath(cx, cy, r, a0, mid) + arcTo(cx, cy, r, mid, a1);
51
+ }
52
+ return `M${n(cx + Math.cos(a0) * r)},${n(cy + Math.sin(a0) * r)}` + arcTo(cx, cy, r, a0, a1);
53
+ }
54
+ /**
55
+ * A closed annular sector between radii `r0` (inner, 0 for a pie wedge) and
56
+ * `r1` (outer), from angle `a0` to `a1` (radians, clockwise, 0 = +x).
57
+ */
58
+ export function sectorPath(cx, cy, r0, r1, a0, a1) {
59
+ if (Math.abs(a1 - a0) >= Math.PI * 2 - 1e-6) {
60
+ const mid = (a0 + a1) / 2;
61
+ return sectorPath(cx, cy, r0, r1, a0, mid) + sectorPath(cx, cy, r0, r1, mid, a1);
62
+ }
63
+ const outer = arcPath(cx, cy, r1, a0, a1);
64
+ if (r0 <= 0)
65
+ return `${outer}L${n(cx)},${n(cy)}Z`;
66
+ return (`${outer}L${n(cx + Math.cos(a1) * r0)},${n(cy + Math.sin(a1) * r0)}` +
67
+ arcTo(cx, cy, r0, a1, a0) +
68
+ 'Z');
69
+ }
70
+ function arcTo(cx, cy, r, from, to) {
71
+ const large = Math.abs(to - from) > Math.PI ? 1 : 0;
72
+ const clockwise = to > from ? 1 : 0;
73
+ return `A${n(r)},${n(r)} 0 ${large} ${clockwise} ${n(cx + Math.cos(to) * r)},${n(cy + Math.sin(to) * r)}`;
74
+ }
@@ -0,0 +1,42 @@
1
+ import type { Point, Size } from './geometry.js';
2
+ /** Axis tick helpers shared by every cartesian chart. */
3
+ export declare const ChartAxis: {
4
+ /**
5
+ * A "nice" grid step for a max value, chosen on a log-magnitude basis so ticks
6
+ * land on 1/2/5 multiples.
7
+ */
8
+ gridStep(maxValue: number): number;
9
+ /** Evenly spaced tick values from 0..max, inclusive, with `count` steps. */
10
+ ticks(max: number, count: number): number[];
11
+ };
12
+ /** The inset plotting rectangle for a cartesian chart (line/bar/scatter). */
13
+ export declare class ChartBounds {
14
+ readonly left: number;
15
+ readonly top: number;
16
+ readonly width: number;
17
+ readonly height: number;
18
+ private constructor();
19
+ /** Insets `size` by a fractional `padding` on every edge (default 10%). */
20
+ static padded(size: Size, padding?: number): ChartBounds;
21
+ /** Insets `size` by explicit edge insets (use when axis labels need room). */
22
+ static insets(size: Size, left: number, top: number, right: number, bottom: number): ChartBounds;
23
+ get right(): number;
24
+ get bottom(): number;
25
+ }
26
+ /** Center + radius for a radial chart (pie/gauge/radar/polar/sunburst). */
27
+ export declare class RadialLayout {
28
+ readonly center: Point;
29
+ readonly radius: number;
30
+ /** `scale` is the fraction of `min(width,height)/2` the radius fills. */
31
+ constructor(size: Size, scale?: number);
32
+ /**
33
+ * The point on a ray at `angle` (radians, 0 = +x, clockwise as y grows down)
34
+ * at the given `distance` from center.
35
+ */
36
+ pointAt(angle: number, distance: number): Point;
37
+ }
38
+ /**
39
+ * Pads or truncates `labels` to exactly `count` entries, so a mismatched label
40
+ * array can never drive a different number of columns than the data.
41
+ */
42
+ export declare function normalizedLabels(labels: readonly string[], count: number): string[];
@@ -0,0 +1,78 @@
1
+ /** Axis tick helpers shared by every cartesian chart. */
2
+ export const ChartAxis = {
3
+ /**
4
+ * A "nice" grid step for a max value, chosen on a log-magnitude basis so ticks
5
+ * land on 1/2/5 multiples.
6
+ */
7
+ gridStep(maxValue) {
8
+ if (!(maxValue > 0) || !Number.isFinite(maxValue))
9
+ return 1;
10
+ const magnitude = Math.floor(Math.log(maxValue) / Math.LN10);
11
+ const base = Math.pow(10, magnitude);
12
+ if (maxValue / base > 5)
13
+ return base * 2;
14
+ if (maxValue / base > 2)
15
+ return base;
16
+ return base / 2;
17
+ },
18
+ /** Evenly spaced tick values from 0..max, inclusive, with `count` steps. */
19
+ ticks(max, count) {
20
+ if (count <= 0)
21
+ return [];
22
+ const out = [];
23
+ for (let i = 0; i <= count; i++)
24
+ out.push((max * i) / count);
25
+ return out;
26
+ },
27
+ };
28
+ /** The inset plotting rectangle for a cartesian chart (line/bar/scatter). */
29
+ export class ChartBounds {
30
+ constructor(left, top, width, height) {
31
+ this.left = left;
32
+ this.top = top;
33
+ this.width = width;
34
+ this.height = height;
35
+ }
36
+ /** Insets `size` by a fractional `padding` on every edge (default 10%). */
37
+ static padded(size, padding = 0.1) {
38
+ return new ChartBounds(size.width * padding, size.height * padding, size.width * (1 - padding * 2), size.height * (1 - padding * 2));
39
+ }
40
+ /** Insets `size` by explicit edge insets (use when axis labels need room). */
41
+ static insets(size, left, top, right, bottom) {
42
+ return new ChartBounds(left, top, size.width - left - right, size.height - top - bottom);
43
+ }
44
+ get right() {
45
+ return this.left + this.width;
46
+ }
47
+ get bottom() {
48
+ return this.top + this.height;
49
+ }
50
+ }
51
+ /** Center + radius for a radial chart (pie/gauge/radar/polar/sunburst). */
52
+ export class RadialLayout {
53
+ /** `scale` is the fraction of `min(width,height)/2` the radius fills. */
54
+ constructor(size, scale = 0.8) {
55
+ this.center = { x: size.width / 2, y: size.height / 2 };
56
+ this.radius = (Math.min(size.width, size.height) / 2) * scale;
57
+ }
58
+ /**
59
+ * The point on a ray at `angle` (radians, 0 = +x, clockwise as y grows down)
60
+ * at the given `distance` from center.
61
+ */
62
+ pointAt(angle, distance) {
63
+ return {
64
+ x: this.center.x + Math.cos(angle) * distance,
65
+ y: this.center.y + Math.sin(angle) * distance,
66
+ };
67
+ }
68
+ }
69
+ /**
70
+ * Pads or truncates `labels` to exactly `count` entries, so a mismatched label
71
+ * array can never drive a different number of columns than the data.
72
+ */
73
+ export function normalizedLabels(labels, count) {
74
+ const out = [];
75
+ for (let i = 0; i < count; i++)
76
+ out.push(labels[i] ?? '');
77
+ return out;
78
+ }
@@ -0,0 +1,48 @@
1
+ /**
2
+ * The Drafter data model — small, explicit value types shared by every chart.
3
+ * Colours are CSS colour strings.
4
+ */
5
+ /** A single labeled data point: one label bound to one value. */
6
+ export interface ChartPoint {
7
+ readonly label: string;
8
+ readonly value: number;
9
+ }
10
+ /** Build a labeled point, or an unlabeled one when `label` is omitted. */
11
+ export declare function point(value: number, label?: string): ChartPoint;
12
+ /**
13
+ * A named, colored series of values for multi-series charts (grouped/stacked
14
+ * lines and bars, stream graphs). The colour is bound to the series, so there
15
+ * is no separate `colors` array to fall out of sync.
16
+ */
17
+ export interface ChartSeries {
18
+ readonly name?: string;
19
+ readonly color: string;
20
+ readonly values: readonly number[];
21
+ }
22
+ /** A single bar with an optional explicit colour (falls back to the palette). */
23
+ export interface BarItem {
24
+ readonly label: string;
25
+ readonly value: number;
26
+ readonly color?: string;
27
+ }
28
+ /** A single waterfall step: a labeled incremental change with optional colour. */
29
+ export interface WaterfallStep {
30
+ readonly label: string;
31
+ readonly value: number;
32
+ readonly color?: string;
33
+ }
34
+ /** A single scatter point with an optional explicit colour. */
35
+ export interface ScatterPoint {
36
+ readonly x: number;
37
+ readonly y: number;
38
+ readonly color?: string;
39
+ }
40
+ /** A radar series: a colour bound to a set of axis → value readings. */
41
+ export interface RadarSeries {
42
+ readonly color: string;
43
+ readonly values: Readonly<Record<string, number>>;
44
+ }
45
+ /** Max of a value list, or 0 when empty. */
46
+ export declare function maxValue(values: readonly number[]): number;
47
+ /** A missing, NaN or infinite reading counts as 0, so it can never reach the markup. */
48
+ export declare function finite(value: number | undefined): number;
@@ -0,0 +1,20 @@
1
+ /**
2
+ * The Drafter data model — small, explicit value types shared by every chart.
3
+ * Colours are CSS colour strings.
4
+ */
5
+ /** Build a labeled point, or an unlabeled one when `label` is omitted. */
6
+ export function point(value, label = '') {
7
+ return { label, value };
8
+ }
9
+ /** Max of a value list, or 0 when empty. */
10
+ export function maxValue(values) {
11
+ let m = 0;
12
+ for (const v of values)
13
+ if (v > m)
14
+ m = v;
15
+ return m;
16
+ }
17
+ /** A missing, NaN or infinite reading counts as 0, so it can never reach the markup. */
18
+ export function finite(value) {
19
+ return value !== undefined && Number.isFinite(value) ? value : 0;
20
+ }
@@ -0,0 +1,34 @@
1
+ /**
2
+ * drafter-react — Drafter charts for React and Next.js.
3
+ *
4
+ * Every chart is a stateless SVG component: it renders in a Server Component,
5
+ * animates with CSS alone and ships no client JavaScript.
6
+ */
7
+ export { DrafterColors, drafterPalette, paletteAt, theme } from './core/colors.js';
8
+ export * from './core/geometry.js';
9
+ export * from './core/scales.js';
10
+ export * from './core/paths.js';
11
+ export * from './core/types.js';
12
+ export { Chart, frameSize, delay } from './svg/Chart.js';
13
+ export type { ChartFrameProps } from './svg/Chart.js';
14
+ export * from './svg/primitives.js';
15
+ export * from './charts/LineChart.js';
16
+ export * from './charts/AreaChart.js';
17
+ export * from './charts/StepLineChart.js';
18
+ export * from './charts/BarChart.js';
19
+ export * from './charts/ScatterPlot.js';
20
+ export * from './charts/BubbleChart.js';
21
+ export * from './charts/CandlestickChart.js';
22
+ export * from './charts/StreamGraphChart.js';
23
+ export * from './charts/BoxPlotChart.js';
24
+ export * from './charts/PieChart.js';
25
+ export * from './charts/RadarChart.js';
26
+ export * from './charts/GaugeChart.js';
27
+ export * from './charts/PolarAreaChart.js';
28
+ export * from './charts/SunburstChart.js';
29
+ export * from './charts/TreemapChart.js';
30
+ export * from './charts/FunnelChart.js';
31
+ export * from './charts/SankeyChart.js';
32
+ export * from './charts/Heatmap.js';
33
+ export * from './charts/GanttChart.js';
34
+ export * from './charts/BulletChart.js';
package/dist/index.js ADDED
@@ -0,0 +1,36 @@
1
+ /**
2
+ * drafter-react — Drafter charts for React and Next.js.
3
+ *
4
+ * Every chart is a stateless SVG component: it renders in a Server Component,
5
+ * animates with CSS alone and ships no client JavaScript.
6
+ */
7
+ // Core (pure, framework-free)
8
+ export { DrafterColors, drafterPalette, paletteAt, theme } from './core/colors.js';
9
+ export * from './core/geometry.js';
10
+ export * from './core/scales.js';
11
+ export * from './core/paths.js';
12
+ export * from './core/types.js';
13
+ // Host + shared rendering primitives, for building custom charts
14
+ export { Chart, frameSize, delay } from './svg/Chart.js';
15
+ export * from './svg/primitives.js';
16
+ // Charts
17
+ export * from './charts/LineChart.js';
18
+ export * from './charts/AreaChart.js';
19
+ export * from './charts/StepLineChart.js';
20
+ export * from './charts/BarChart.js';
21
+ export * from './charts/ScatterPlot.js';
22
+ export * from './charts/BubbleChart.js';
23
+ export * from './charts/CandlestickChart.js';
24
+ export * from './charts/StreamGraphChart.js';
25
+ export * from './charts/BoxPlotChart.js';
26
+ export * from './charts/PieChart.js';
27
+ export * from './charts/RadarChart.js';
28
+ export * from './charts/GaugeChart.js';
29
+ export * from './charts/PolarAreaChart.js';
30
+ export * from './charts/SunburstChart.js';
31
+ export * from './charts/TreemapChart.js';
32
+ export * from './charts/FunnelChart.js';
33
+ export * from './charts/SankeyChart.js';
34
+ export * from './charts/Heatmap.js';
35
+ export * from './charts/GanttChart.js';
36
+ export * from './charts/BulletChart.js';