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.
- package/LICENSE +201 -0
- package/README.md +110 -2
- package/dist/charts/AreaChart.d.ts +12 -0
- package/dist/charts/AreaChart.js +35 -0
- package/dist/charts/BarChart.d.ts +36 -0
- package/dist/charts/BarChart.js +246 -0
- package/dist/charts/BoxPlotChart.d.ts +20 -0
- package/dist/charts/BoxPlotChart.js +50 -0
- package/dist/charts/BubbleChart.d.ts +15 -0
- package/dist/charts/BubbleChart.js +49 -0
- package/dist/charts/BulletChart.d.ts +15 -0
- package/dist/charts/BulletChart.js +42 -0
- package/dist/charts/CandlestickChart.d.ts +28 -0
- package/dist/charts/CandlestickChart.js +70 -0
- package/dist/charts/FunnelChart.d.ts +12 -0
- package/dist/charts/FunnelChart.js +40 -0
- package/dist/charts/GanttChart.d.ts +13 -0
- package/dist/charts/GanttChart.js +43 -0
- package/dist/charts/GaugeChart.d.ts +12 -0
- package/dist/charts/GaugeChart.js +71 -0
- package/dist/charts/Heatmap.d.ts +15 -0
- package/dist/charts/Heatmap.js +54 -0
- package/dist/charts/LineChart.d.ts +20 -0
- package/dist/charts/LineChart.js +65 -0
- package/dist/charts/PieChart.d.ts +20 -0
- package/dist/charts/PieChart.js +75 -0
- package/dist/charts/PolarAreaChart.d.ts +12 -0
- package/dist/charts/PolarAreaChart.js +36 -0
- package/dist/charts/RadarChart.d.ts +8 -0
- package/dist/charts/RadarChart.js +49 -0
- package/dist/charts/SankeyChart.d.ts +20 -0
- package/dist/charts/SankeyChart.js +114 -0
- package/dist/charts/ScatterPlot.d.ts +13 -0
- package/dist/charts/ScatterPlot.js +31 -0
- package/dist/charts/StepLineChart.d.ts +8 -0
- package/dist/charts/StepLineChart.js +32 -0
- package/dist/charts/StreamGraphChart.d.ts +12 -0
- package/dist/charts/StreamGraphChart.js +51 -0
- package/dist/charts/SunburstChart.d.ts +14 -0
- package/dist/charts/SunburstChart.js +69 -0
- package/dist/charts/TreemapChart.d.ts +13 -0
- package/dist/charts/TreemapChart.js +116 -0
- package/dist/core/colors.d.ts +29 -0
- package/dist/core/colors.js +40 -0
- package/dist/core/geometry.d.ts +16 -0
- package/dist/core/geometry.js +16 -0
- package/dist/core/paths.d.ts +21 -0
- package/dist/core/paths.js +74 -0
- package/dist/core/scales.d.ts +42 -0
- package/dist/core/scales.js +78 -0
- package/dist/core/types.d.ts +48 -0
- package/dist/core/types.js +20 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +36 -0
- package/dist/svg/Chart.d.ts +30 -0
- package/dist/svg/Chart.js +21 -0
- package/dist/svg/primitives.d.ts +86 -0
- package/dist/svg/primitives.js +77 -0
- package/dist/svg/styles.d.ts +1 -0
- package/dist/svg/styles.js +40 -0
- package/package.json +52 -4
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useId } from 'react';
|
|
3
|
+
import { Chart, frameSize } from '../svg/Chart.js';
|
|
4
|
+
import { AreaGradient, CategoryLabels, SmoothLine, ValueGrid, VertexDot } from '../svg/primitives.js';
|
|
5
|
+
import { ChartBounds } from '../core/scales.js';
|
|
6
|
+
import { areaPath, smoothPath } from '../core/paths.js';
|
|
7
|
+
import { DrafterColors } from '../core/colors.js';
|
|
8
|
+
import { finite, maxValue } from '../core/types.js';
|
|
9
|
+
/** A smooth single-series line chart with a soft gradient fill and reveal. */
|
|
10
|
+
export function LineChart({ points, color = DrafterColors.blue, ...frame }) {
|
|
11
|
+
const bounds = ChartBounds.padded(frameSize(frame));
|
|
12
|
+
const max = maxValue(points.map((p) => p.value));
|
|
13
|
+
const pixels = toPixels(points.map((p) => p.value), points.length, max, bounds);
|
|
14
|
+
return (_jsxs(Chart, { ...frame, children: [_jsx(ValueGrid, { bounds: bounds, max: max }), _jsx(CategoryLabels, { bounds: bounds, categories: points.map((p) => p.label), count: points.length }), _jsx(SmoothLine, { points: pixels, color: color, baseline: bounds.bottom })] }));
|
|
15
|
+
}
|
|
16
|
+
/** Overlaid multi-series smooth lines with vertex dots revealed as they trace. */
|
|
17
|
+
export function GroupedLineChart({ series, categories = [], ...frame }) {
|
|
18
|
+
const bounds = ChartBounds.padded(frameSize(frame));
|
|
19
|
+
const max = maxValue(series.flatMap((s) => s.values));
|
|
20
|
+
const count = pointCount(series);
|
|
21
|
+
return (_jsxs(Chart, { ...frame, children: [_jsx(ValueGrid, { bounds: bounds, max: max }), _jsx(CategoryLabels, { bounds: bounds, categories: categories, count: count }), series.map((line, li) => {
|
|
22
|
+
const pixels = toPixels(line.values, count, max, bounds);
|
|
23
|
+
return (_jsxs("g", { children: [_jsx(SmoothLine, { points: pixels, color: line.color, baseline: bounds.bottom, strokeWidth: 3.5, fill: false, endDot: false }), _jsx("g", { className: "d-wipe", children: pixels.map((p, i) => (_jsx(VertexDot, { x: p.x, y: p.y, color: line.color, radius: 3.5, className: "d-mark", children: _jsx("title", { children: tooltip(line.name, categories[i], finite(line.values[i])) }) }, i))) })] }, li));
|
|
24
|
+
})] }));
|
|
25
|
+
}
|
|
26
|
+
/** Stacked filled areas that grow up from the baseline. */
|
|
27
|
+
export function StackedLineChart({ series, categories = [], ...frame }) {
|
|
28
|
+
const gradientId = useId();
|
|
29
|
+
const bounds = ChartBounds.padded(frameSize(frame));
|
|
30
|
+
const count = pointCount(series);
|
|
31
|
+
// cumulative[k][i] = sum of series 0..k at x-index i.
|
|
32
|
+
const cumulative = [];
|
|
33
|
+
series.forEach((s, k) => {
|
|
34
|
+
cumulative.push(Array.from({ length: count }, (_, i) => (cumulative[k - 1]?.[i] ?? 0) + finite(s.values[i])));
|
|
35
|
+
});
|
|
36
|
+
const max = maxValue(cumulative[cumulative.length - 1] ?? []);
|
|
37
|
+
return (_jsxs(Chart, { ...frame, children: [_jsx(ValueGrid, { bounds: bounds, max: max }), _jsx(CategoryLabels, { bounds: bounds, categories: categories, count: count }), series
|
|
38
|
+
.map((band, k) => {
|
|
39
|
+
const pixels = toPixels(cumulative[k] ?? [], count, max, bounds);
|
|
40
|
+
const first = pixels[0];
|
|
41
|
+
const last = pixels[pixels.length - 1];
|
|
42
|
+
if (!first || !last)
|
|
43
|
+
return null;
|
|
44
|
+
const line = smoothPath(pixels);
|
|
45
|
+
const id = `${gradientId}-${k}`;
|
|
46
|
+
return (_jsxs("g", { className: "d-grow-up", children: [_jsx("defs", { children: _jsx(AreaGradient, { id: id, color: band.color, top: bounds.top, bottom: bounds.bottom, topAlpha: 0.85 }) }), _jsx("path", { d: areaPath(line, first.x, last.x, bounds.bottom), fill: `url(#${id})` }), _jsx("path", { d: line, fill: "none", stroke: band.color, strokeWidth: 3.5, strokeLinecap: "round", strokeLinejoin: "round" })] }, k));
|
|
47
|
+
})
|
|
48
|
+
.reverse()] }));
|
|
49
|
+
}
|
|
50
|
+
function pointCount(series) {
|
|
51
|
+
return series.reduce((n, s) => Math.max(n, s.values.length), 0);
|
|
52
|
+
}
|
|
53
|
+
/** Evenly spaced pixel points for `count` columns; missing values read as 0. */
|
|
54
|
+
function toPixels(values, count, max, bounds) {
|
|
55
|
+
if (count < 2 || !(max > 0))
|
|
56
|
+
return [];
|
|
57
|
+
return Array.from({ length: count }, (_, i) => ({
|
|
58
|
+
x: bounds.left + (i * bounds.width) / (count - 1),
|
|
59
|
+
y: bounds.bottom - (finite(values[i]) / max) * bounds.height,
|
|
60
|
+
}));
|
|
61
|
+
}
|
|
62
|
+
function tooltip(name, category, value) {
|
|
63
|
+
const subject = [name, category].filter(Boolean).join(' · ');
|
|
64
|
+
return subject ? `${subject}: ${value}` : `${value}`;
|
|
65
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type ChartFrameProps } from '../svg/Chart.js';
|
|
2
|
+
/** One wedge of a {@link PieChart} / {@link DonutChart}. */
|
|
3
|
+
export interface PieSlice {
|
|
4
|
+
readonly value: number;
|
|
5
|
+
readonly label: string;
|
|
6
|
+
/** Falls back to the i-th palette colour when omitted. */
|
|
7
|
+
readonly color?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface PieChartProps extends ChartFrameProps {
|
|
10
|
+
readonly slices: readonly PieSlice[];
|
|
11
|
+
}
|
|
12
|
+
/** A pie chart that sweeps in clockwise from 12 o'clock, with percent labels. */
|
|
13
|
+
export declare function PieChart({ slices, ...frame }: PieChartProps): import("react").JSX.Element;
|
|
14
|
+
export interface DonutChartProps extends ChartFrameProps {
|
|
15
|
+
readonly slices: readonly PieSlice[];
|
|
16
|
+
/** Hole radius as a fraction of the outer radius. Default 0.5. */
|
|
17
|
+
readonly holeRadiusFraction?: number;
|
|
18
|
+
}
|
|
19
|
+
/** A donut of round-capped arcs with percent labels outside the ring. */
|
|
20
|
+
export declare function DonutChart({ slices, holeRadiusFraction, ...frame }: DonutChartProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,75 @@
|
|
|
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 { arcPath, sectorPath } from '../core/paths.js';
|
|
6
|
+
import { paletteAt, theme } from '../core/colors.js';
|
|
7
|
+
const REVEAL_MS = 1100;
|
|
8
|
+
/** Slices with under this percent share get no label. */
|
|
9
|
+
const LABEL_THRESHOLD = 5;
|
|
10
|
+
/** The gap (radians) carved between donut arcs so they read as separate bands. */
|
|
11
|
+
const DONUT_GAP = (2 * Math.PI) / 180;
|
|
12
|
+
const TURN = Math.PI * 2;
|
|
13
|
+
const TOP = -Math.PI / 2;
|
|
14
|
+
/** Resolve colours and angular layout. The total is floored at 1, like every Drafter port. */
|
|
15
|
+
function resolveSlices(slices) {
|
|
16
|
+
const values = slices.map((s) => (Number.isFinite(s.value) && s.value > 0 ? s.value : 0));
|
|
17
|
+
const total = Math.max(values.reduce((a, b) => a + b, 0), 1);
|
|
18
|
+
let before = 0;
|
|
19
|
+
return slices.map((slice, i) => {
|
|
20
|
+
const fraction = (values[i] ?? 0) / total;
|
|
21
|
+
const start = TOP + before * TURN;
|
|
22
|
+
before += fraction;
|
|
23
|
+
return { label: slice.label, value: slice.value, color: slice.color ?? paletteAt(i), fraction, start };
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
function sliceTitle(slice) {
|
|
27
|
+
return `${slice.label ? `${slice.label}: ` : ''}${formatTick(slice.value)} (${Math.trunc(slice.fraction * 100)}%)`;
|
|
28
|
+
}
|
|
29
|
+
function SliceLabels({ slices, layout, distance, color, durationMs, }) {
|
|
30
|
+
return (_jsx("g", { className: "d-fade", style: delay(durationMs * 0.7), children: slices.map((slice, i) => {
|
|
31
|
+
if (slice.fraction * 100 < LABEL_THRESHOLD)
|
|
32
|
+
return null;
|
|
33
|
+
const at = layout.pointAt(slice.start + (slice.fraction * TURN) / 2, distance);
|
|
34
|
+
return (_jsx(Label, { text: `${Math.trunc(slice.fraction * 100)}%`, x: at.x, y: at.y, color: color, fontSize: 13, weight: 700, h: "center", v: "center" }, i));
|
|
35
|
+
}) }));
|
|
36
|
+
}
|
|
37
|
+
/** A pie chart that sweeps in clockwise from 12 o'clock, with percent labels. */
|
|
38
|
+
export function PieChart({ slices, ...frame }) {
|
|
39
|
+
const durationMs = frame.durationMs ?? REVEAL_MS;
|
|
40
|
+
const layout = new RadialLayout(frameSize(frame), 0.7);
|
|
41
|
+
const { x: cx, y: cy } = layout.center;
|
|
42
|
+
const radius = layout.radius;
|
|
43
|
+
const resolved = radius > 0 ? resolveSlices(slices) : [];
|
|
44
|
+
return (_jsxs(Chart, { ...frame, durationMs: durationMs, children: [_jsx(RadialSweep, { cx: cx, cy: cy, radius: radius, children: resolved.map((slice, i) => {
|
|
45
|
+
if (slice.fraction <= 0)
|
|
46
|
+
return null;
|
|
47
|
+
const shared = {
|
|
48
|
+
className: 'd-mark',
|
|
49
|
+
fill: slice.color,
|
|
50
|
+
stroke: theme.surface,
|
|
51
|
+
strokeWidth: 1.75,
|
|
52
|
+
strokeLinejoin: 'round',
|
|
53
|
+
};
|
|
54
|
+
return slice.fraction >= 0.9999 ? (_jsx("circle", { cx: cx, cy: cy, r: radius, ...shared, children: _jsx("title", { children: sliceTitle(slice) }) }, i)) : (_jsx("path", { d: sectorPath(cx, cy, 0, radius, slice.start, slice.start + slice.fraction * TURN), ...shared, children: _jsx("title", { children: sliceTitle(slice) }) }, i));
|
|
55
|
+
}) }), _jsx(SliceLabels, { slices: resolved, layout: layout, distance: radius * 0.7, color: theme.surface, durationMs: durationMs })] }));
|
|
56
|
+
}
|
|
57
|
+
/** A donut of round-capped arcs with percent labels outside the ring. */
|
|
58
|
+
export function DonutChart({ slices, holeRadiusFraction = 0.5, ...frame }) {
|
|
59
|
+
const durationMs = frame.durationMs ?? REVEAL_MS;
|
|
60
|
+
const layout = new RadialLayout(frameSize(frame), 0.6);
|
|
61
|
+
const { x: cx, y: cy } = layout.center;
|
|
62
|
+
const outerRadius = layout.radius;
|
|
63
|
+
const hole = Number.isFinite(holeRadiusFraction) ? Math.min(Math.max(holeRadiusFraction, 0), 0.95) : 0.5;
|
|
64
|
+
const innerRadius = outerRadius * hole;
|
|
65
|
+
const bandRadius = (outerRadius + innerRadius) / 2;
|
|
66
|
+
const bandWidth = outerRadius - innerRadius;
|
|
67
|
+
const resolved = outerRadius > 0 ? resolveSlices(slices) : [];
|
|
68
|
+
return (_jsxs(Chart, { ...frame, durationMs: durationMs, children: [_jsx(RadialSweep, { cx: cx, cy: cy, radius: outerRadius, children: resolved.map((slice, i) => {
|
|
69
|
+
const sweep = slice.fraction * TURN - DONUT_GAP;
|
|
70
|
+
if (sweep <= 0)
|
|
71
|
+
return null;
|
|
72
|
+
const start = slice.start + DONUT_GAP / 2;
|
|
73
|
+
return (_jsx("path", { className: "d-mark", d: arcPath(cx, cy, bandRadius, start, start + sweep), fill: "none", stroke: slice.color, strokeWidth: bandWidth, strokeLinecap: "round", children: _jsx("title", { children: sliceTitle(slice) }) }, i));
|
|
74
|
+
}) }), _jsx(SliceLabels, { slices: resolved, layout: layout, distance: outerRadius * 1.22, color: "currentColor", durationMs: durationMs })] }));
|
|
75
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type ChartFrameProps } from '../svg/Chart.js';
|
|
2
|
+
/** One equal-angle wedge whose radius encodes its value. */
|
|
3
|
+
export interface PolarSlice {
|
|
4
|
+
readonly label: string;
|
|
5
|
+
readonly value: number;
|
|
6
|
+
readonly color?: string;
|
|
7
|
+
}
|
|
8
|
+
export interface PolarAreaChartProps extends ChartFrameProps {
|
|
9
|
+
readonly slices: readonly PolarSlice[];
|
|
10
|
+
}
|
|
11
|
+
/** A polar area (coxcomb) chart: equal angles, value-scaled radii, grown from the centre. */
|
|
12
|
+
export declare function PolarAreaChart({ slices, ...frame }: PolarAreaChartProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Chart, delay, frameSize } from '../svg/Chart.js';
|
|
3
|
+
import { Label, formatTick } from '../svg/primitives.js';
|
|
4
|
+
import { RadialLayout } from '../core/scales.js';
|
|
5
|
+
import { sectorPath } from '../core/paths.js';
|
|
6
|
+
import { paletteAt, theme } from '../core/colors.js';
|
|
7
|
+
const REVEAL_MS = 900;
|
|
8
|
+
const RINGS = 4;
|
|
9
|
+
const LABEL_OFFSET = 14;
|
|
10
|
+
const TOP = -Math.PI / 2;
|
|
11
|
+
/** A polar area (coxcomb) chart: equal angles, value-scaled radii, grown from the centre. */
|
|
12
|
+
export function PolarAreaChart({ slices, ...frame }) {
|
|
13
|
+
const durationMs = frame.durationMs ?? REVEAL_MS;
|
|
14
|
+
const layout = new RadialLayout(frameSize(frame), 0.72);
|
|
15
|
+
const { x: cx, y: cy } = layout.center;
|
|
16
|
+
const maxRadius = layout.radius;
|
|
17
|
+
const count = slices.length;
|
|
18
|
+
if (count === 0 || !(maxRadius > 0))
|
|
19
|
+
return _jsx(Chart, { ...frame, durationMs: durationMs });
|
|
20
|
+
const values = slices.map((s) => (Number.isFinite(s.value) && s.value > 0 ? s.value : 0));
|
|
21
|
+
const maxVal = Math.max(...values, 0.0001);
|
|
22
|
+
const sweep = (Math.PI * 2) / count;
|
|
23
|
+
return (_jsxs(Chart, { ...frame, durationMs: durationMs, children: [_jsxs("g", { fill: "none", stroke: theme.grid, children: [Array.from({ length: RINGS }, (_, ring) => (_jsx("circle", { cx: cx, cy: cy, r: (maxRadius * (ring + 1)) / RINGS }, ring))), slices.map((_, i) => {
|
|
24
|
+
const end = layout.pointAt(TOP + i * sweep, maxRadius);
|
|
25
|
+
return _jsx("line", { x1: cx, y1: cy, x2: end.x, y2: end.y }, i);
|
|
26
|
+
})] }), slices.map((slice, i) => {
|
|
27
|
+
const radius = ((values[i] ?? 0) / maxVal) * maxRadius;
|
|
28
|
+
if (!(radius > 0))
|
|
29
|
+
return null;
|
|
30
|
+
const start = TOP + i * sweep;
|
|
31
|
+
return (_jsxs("g", { className: "d-pop", style: delay((i * durationMs * 0.4) / count), children: [_jsx("circle", { cx: cx, cy: cy, r: radius, fill: "none" }), _jsx("path", { className: "d-mark", d: sectorPath(cx, cy, 0, radius, start, start + sweep), fill: slice.color ?? paletteAt(i), fillOpacity: 0.7, stroke: "#fff", strokeOpacity: 0.55, strokeWidth: 1.25, children: _jsx("title", { children: `${slice.label}: ${formatTick(values[i] ?? 0)}` }) })] }, i));
|
|
32
|
+
}), slices.map((slice, i) => {
|
|
33
|
+
const at = layout.pointAt(TOP + (i + 0.5) * sweep, maxRadius + LABEL_OFFSET);
|
|
34
|
+
return _jsx(Label, { text: slice.label, x: at.x, y: at.y, h: "center", v: "center" }, i);
|
|
35
|
+
})] }));
|
|
36
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type ChartFrameProps } from '../svg/Chart.js';
|
|
2
|
+
import type { RadarSeries } from '../core/types.js';
|
|
3
|
+
export interface RadarChartProps extends ChartFrameProps {
|
|
4
|
+
/** Each series maps axis name → reading in 0..1 (1 reaches the outer ring). */
|
|
5
|
+
readonly series: readonly RadarSeries[];
|
|
6
|
+
}
|
|
7
|
+
/** A radar (spider) chart: translucent polygons that grow out from the centre. */
|
|
8
|
+
export declare function RadarChart({ series, ...frame }: RadarChartProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Chart, delay, frameSize } from '../svg/Chart.js';
|
|
3
|
+
import { Label, VertexDot, formatTick } from '../svg/primitives.js';
|
|
4
|
+
import { RadialLayout } from '../core/scales.js';
|
|
5
|
+
import { polylinePath } from '../core/paths.js';
|
|
6
|
+
import { theme } from '../core/colors.js';
|
|
7
|
+
const RING_COUNT = 5;
|
|
8
|
+
/** Axis labels in a stable order: the first non-empty series' sorted keys, then any extras. */
|
|
9
|
+
function orderedAxisLabels(series) {
|
|
10
|
+
const labels = [];
|
|
11
|
+
const seen = new Set();
|
|
12
|
+
for (const entry of series) {
|
|
13
|
+
for (const key of Object.keys(entry.values).sort()) {
|
|
14
|
+
if (!seen.has(key)) {
|
|
15
|
+
seen.add(key);
|
|
16
|
+
labels.push(key);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return labels;
|
|
21
|
+
}
|
|
22
|
+
function axisAngle(index, count) {
|
|
23
|
+
return (index * 2 * Math.PI) / count - Math.PI / 2;
|
|
24
|
+
}
|
|
25
|
+
/** A radar (spider) chart: translucent polygons that grow out from the centre. */
|
|
26
|
+
export function RadarChart({ series, ...frame }) {
|
|
27
|
+
const durationMs = frame.durationMs ?? 1100;
|
|
28
|
+
const layout = new RadialLayout(frameSize(frame));
|
|
29
|
+
const { center, radius } = layout;
|
|
30
|
+
const axisLabels = orderedAxisLabels(series);
|
|
31
|
+
const axisCount = axisLabels.length;
|
|
32
|
+
if (axisCount < 3 || !(radius > 0))
|
|
33
|
+
return _jsx(Chart, { ...frame });
|
|
34
|
+
return (_jsxs(Chart, { ...frame, children: [_jsxs("g", { fill: "none", stroke: theme.grid, children: [Array.from({ length: RING_COUNT }, (_, ring) => (_jsx("circle", { cx: center.x, cy: center.y, r: (radius * (ring + 1)) / RING_COUNT }, ring))), axisLabels.map((_, i) => {
|
|
35
|
+
const end = layout.pointAt(axisAngle(i, axisCount), radius);
|
|
36
|
+
return _jsx("line", { x1: center.x, y1: center.y, x2: end.x, y2: end.y }, i);
|
|
37
|
+
})] }), axisLabels.map((label, i) => {
|
|
38
|
+
const at = layout.pointAt(axisAngle(i, axisCount), radius * 1.1);
|
|
39
|
+
return _jsx(Label, { text: label, x: at.x, y: at.y, fontSize: 12, h: "center", v: "center" }, i);
|
|
40
|
+
}), series.map((entry, si) => {
|
|
41
|
+
const readings = axisLabels.map((label) => {
|
|
42
|
+
const raw = entry.values[label] ?? 0;
|
|
43
|
+
return Number.isFinite(raw) ? Math.max(raw, 0) : 0;
|
|
44
|
+
});
|
|
45
|
+
const vertices = readings.map((value, i) => layout.pointAt(axisAngle(i, axisCount), radius * value));
|
|
46
|
+
const reach = radius * Math.max(1, ...readings);
|
|
47
|
+
return (_jsxs("g", { className: "d-pop", style: delay((si * durationMs) / 8), children: [_jsx("circle", { cx: center.x, cy: center.y, r: reach, fill: "none" }), _jsx("path", { d: `${polylinePath(vertices)}Z`, fill: entry.color, fillOpacity: 0.22, stroke: entry.color, strokeOpacity: 0.9, strokeWidth: 1.75, strokeLinejoin: "round" }), vertices.map((p, i) => (_jsx(VertexDot, { x: p.x, y: p.y, color: entry.color, radius: 2.8, className: "d-mark", children: _jsx("title", { children: `${axisLabels[i] ?? ''}: ${formatTick(readings[i] ?? 0)}` }) }, i)))] }, si));
|
|
48
|
+
})] }));
|
|
49
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type ChartFrameProps } from '../svg/Chart.js';
|
|
2
|
+
/** A node, placed in an explicit `column` (0 = leftmost). */
|
|
3
|
+
export interface SankeyNode {
|
|
4
|
+
readonly id: string;
|
|
5
|
+
readonly label: string;
|
|
6
|
+
readonly column: number;
|
|
7
|
+
readonly color?: string;
|
|
8
|
+
}
|
|
9
|
+
/** A flow of `value` between two node ids. */
|
|
10
|
+
export interface SankeyLink {
|
|
11
|
+
readonly from: string;
|
|
12
|
+
readonly to: string;
|
|
13
|
+
readonly value: number;
|
|
14
|
+
}
|
|
15
|
+
export interface SankeyChartProps extends ChartFrameProps {
|
|
16
|
+
readonly nodes: readonly SankeyNode[];
|
|
17
|
+
readonly links: readonly SankeyLink[];
|
|
18
|
+
}
|
|
19
|
+
/** A Sankey diagram: node bars per column joined by gradient flow bands. */
|
|
20
|
+
export declare function SankeyChart({ nodes, links, ...frame }: SankeyChartProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useId } from 'react';
|
|
3
|
+
import { Chart, frameSize } from '../svg/Chart.js';
|
|
4
|
+
import { Label, formatTick } from '../svg/primitives.js';
|
|
5
|
+
import { paletteAt } from '../core/colors.js';
|
|
6
|
+
const REVEAL_MS = 900;
|
|
7
|
+
function layoutSankey(size, nodes, rawLinks) {
|
|
8
|
+
const inset = 0.08;
|
|
9
|
+
const chartLeft = size.width * inset;
|
|
10
|
+
const chartTop = size.height * inset;
|
|
11
|
+
const chartWidth = size.width * (1 - inset * 2);
|
|
12
|
+
const chartHeight = size.height * (1 - inset * 2);
|
|
13
|
+
if (nodes.length === 0 || !(chartWidth > 0) || !(chartHeight > 0))
|
|
14
|
+
return { nodes: [], bands: [] };
|
|
15
|
+
const ids = new Set(nodes.map((n) => n.id));
|
|
16
|
+
const links = rawLinks.filter((l) => ids.has(l.from) && ids.has(l.to) && Number.isFinite(l.value) && l.value > 0);
|
|
17
|
+
const inflow = new Map();
|
|
18
|
+
const outflow = new Map();
|
|
19
|
+
for (const link of links) {
|
|
20
|
+
outflow.set(link.from, (outflow.get(link.from) ?? 0) + link.value);
|
|
21
|
+
inflow.set(link.to, (inflow.get(link.to) ?? 0) + link.value);
|
|
22
|
+
}
|
|
23
|
+
const throughput = (id) => Math.max(inflow.get(id) ?? 0, outflow.get(id) ?? 0);
|
|
24
|
+
const columns = new Map();
|
|
25
|
+
nodes.forEach((node, i) => {
|
|
26
|
+
const column = Number.isFinite(node.column) ? node.column : 0;
|
|
27
|
+
const entry = { node, color: node.color ?? paletteAt(i) };
|
|
28
|
+
const group = columns.get(column);
|
|
29
|
+
if (group)
|
|
30
|
+
group.push(entry);
|
|
31
|
+
else
|
|
32
|
+
columns.set(column, [entry]);
|
|
33
|
+
});
|
|
34
|
+
const columnKeys = [...columns.keys()].sort((a, b) => a - b);
|
|
35
|
+
const maxColumn = columnKeys[columnKeys.length - 1] ?? 0;
|
|
36
|
+
const nodeWidth = Math.min(Math.max(chartWidth * 0.045, 6), 26);
|
|
37
|
+
const verticalGap = Math.max(chartHeight * 0.04, 6);
|
|
38
|
+
let maxSum = 1;
|
|
39
|
+
let maxCount = 0;
|
|
40
|
+
for (const group of columns.values()) {
|
|
41
|
+
maxSum = Math.max(maxSum, group.reduce((a, e) => a + throughput(e.node.id), 0));
|
|
42
|
+
maxCount = Math.max(maxCount, group.length);
|
|
43
|
+
}
|
|
44
|
+
const valueToPx = Math.max(chartHeight - Math.max(maxCount - 1, 0) * verticalGap, 1) / maxSum;
|
|
45
|
+
const placed = new Map();
|
|
46
|
+
for (const key of columnKeys) {
|
|
47
|
+
const group = [...(columns.get(key) ?? [])].sort((a, b) => a.node.label < b.node.label ? -1 : a.node.label > b.node.label ? 1 : 0);
|
|
48
|
+
const heights = group.map((e) => Math.max(throughput(e.node.id) * valueToPx, 2));
|
|
49
|
+
const stackHeight = heights.reduce((a, b) => a + b, 0) + Math.max(group.length - 1, 0) * verticalGap;
|
|
50
|
+
const x = maxColumn <= 0
|
|
51
|
+
? chartLeft + (chartWidth - nodeWidth) / 2
|
|
52
|
+
: chartLeft + (chartWidth - nodeWidth) * (key / maxColumn);
|
|
53
|
+
let cursorY = chartTop + (chartHeight - stackHeight) / 2;
|
|
54
|
+
group.forEach((entry, i) => {
|
|
55
|
+
const height = heights[i] ?? 2;
|
|
56
|
+
// Keep edge-column labels inside the frame (text width is estimated).
|
|
57
|
+
const half = (entry.node.label.length * 6) / 2;
|
|
58
|
+
const lower = Math.min(half + 2, size.width / 2);
|
|
59
|
+
const upper = Math.max(size.width - half - 2, size.width / 2);
|
|
60
|
+
placed.set(entry.node.id, {
|
|
61
|
+
label: entry.node.label,
|
|
62
|
+
x,
|
|
63
|
+
top: cursorY,
|
|
64
|
+
width: nodeWidth,
|
|
65
|
+
height,
|
|
66
|
+
color: entry.color,
|
|
67
|
+
throughput: throughput(entry.node.id),
|
|
68
|
+
labelX: Math.min(Math.max(x + nodeWidth / 2, lower), upper),
|
|
69
|
+
labelY: cursorY - 4,
|
|
70
|
+
});
|
|
71
|
+
cursorY += height + verticalGap;
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
const outOffset = new Map();
|
|
75
|
+
const inOffset = new Map();
|
|
76
|
+
const bands = [];
|
|
77
|
+
for (const link of links) {
|
|
78
|
+
const from = placed.get(link.from);
|
|
79
|
+
const to = placed.get(link.to);
|
|
80
|
+
if (!from || !to)
|
|
81
|
+
continue;
|
|
82
|
+
const fromThickness = from.height * (link.value / Math.max(from.throughput, 0.0001));
|
|
83
|
+
const toThickness = to.height * (link.value / Math.max(to.throughput, 0.0001));
|
|
84
|
+
const out = outOffset.get(link.from) ?? 0;
|
|
85
|
+
const into = inOffset.get(link.to) ?? 0;
|
|
86
|
+
outOffset.set(link.from, out + fromThickness);
|
|
87
|
+
inOffset.set(link.to, into + toThickness);
|
|
88
|
+
const startX = from.x + from.width;
|
|
89
|
+
const endX = to.x;
|
|
90
|
+
const startTop = from.top + out;
|
|
91
|
+
const startBottom = startTop + fromThickness;
|
|
92
|
+
const endTop = to.top + into;
|
|
93
|
+
const endBottom = endTop + toThickness;
|
|
94
|
+
const midX = (startX + endX) / 2;
|
|
95
|
+
const r = (v) => Math.round(v * 100) / 100;
|
|
96
|
+
bands.push({
|
|
97
|
+
d: `M${r(startX)},${r(startTop)}C${r(midX)},${r(startTop)} ${r(midX)},${r(endTop)} ${r(endX)},${r(endTop)}` +
|
|
98
|
+
`L${r(endX)},${r(endBottom)}C${r(midX)},${r(endBottom)} ${r(midX)},${r(startBottom)} ${r(startX)},${r(startBottom)}Z`,
|
|
99
|
+
startX,
|
|
100
|
+
endX,
|
|
101
|
+
fromColor: from.color,
|
|
102
|
+
toColor: to.color,
|
|
103
|
+
title: `${from.label} → ${to.label}: ${formatTick(link.value)}`,
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
return { nodes: [...placed.values()], bands };
|
|
107
|
+
}
|
|
108
|
+
/** A Sankey diagram: node bars per column joined by gradient flow bands. */
|
|
109
|
+
export function SankeyChart({ nodes, links, ...frame }) {
|
|
110
|
+
const gradientId = useId();
|
|
111
|
+
const durationMs = frame.durationMs ?? REVEAL_MS;
|
|
112
|
+
const layout = layoutSankey(frameSize(frame), nodes, links);
|
|
113
|
+
return (_jsxs(Chart, { ...frame, durationMs: durationMs, children: [_jsx("g", { className: "d-wipe", children: layout.bands.map((band, i) => (_jsxs("g", { className: "d-mark", children: [_jsxs("linearGradient", { id: `${gradientId}-${i}`, gradientUnits: "userSpaceOnUse", x1: band.startX, y1: 0, x2: band.endX, y2: 0, children: [_jsx("stop", { offset: 0, stopColor: band.fromColor, stopOpacity: 0.5 }), _jsx("stop", { offset: 1, stopColor: band.toColor, stopOpacity: 0.5 })] }), _jsx("path", { d: band.d, fill: `url(#${gradientId}-${i})` }), _jsx("title", { children: band.title })] }, i))) }), layout.nodes.map((node, i) => (_jsx("g", { className: "d-pop", children: _jsx("rect", { className: "d-mark", x: node.x, y: node.top, width: node.width, height: node.height, rx: Math.min(node.width / 2.5, node.height / 2), fill: node.color, stroke: "#fff", strokeOpacity: 0.25, children: _jsx("title", { children: `${node.label}: ${formatTick(node.throughput)}` }) }) }, i))), _jsx("g", { className: "d-fade", children: layout.nodes.map((node, i) => (_jsx(Label, { text: node.label, x: node.labelX, y: node.labelY, h: "center", v: "bottom" }, i))) })] }));
|
|
114
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type ChartFrameProps } from '../svg/Chart.js';
|
|
2
|
+
import type { ScatterPoint } from '../core/types.js';
|
|
3
|
+
export interface ScatterPlotProps extends ChartFrameProps {
|
|
4
|
+
readonly points: readonly ScatterPoint[];
|
|
5
|
+
}
|
|
6
|
+
/** A cartesian scatter plot with axis labels and haloed dots that pop in. */
|
|
7
|
+
export declare function ScatterPlot({ points, ...frame }: ScatterPlotProps): import("react").JSX.Element;
|
|
8
|
+
export interface ScatterPlotValuesProps extends ChartFrameProps {
|
|
9
|
+
/** Raw `[x, y]` pairs, coloured from the palette by position. */
|
|
10
|
+
readonly values: ReadonlyArray<readonly [number, number]>;
|
|
11
|
+
}
|
|
12
|
+
/** Convenience for raw `[x, y]` pairs. */
|
|
13
|
+
export declare function ScatterPlotValues({ values, ...frame }: ScatterPlotValuesProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Chart, frameSize } from '../svg/Chart.js';
|
|
3
|
+
import { Label } from '../svg/primitives.js';
|
|
4
|
+
import { ChartAxis, ChartBounds } from '../core/scales.js';
|
|
5
|
+
import { paletteAt, theme } from '../core/colors.js';
|
|
6
|
+
const DOT_RADIUS = 4.2;
|
|
7
|
+
/** A cartesian scatter plot with axis labels and haloed dots that pop in. */
|
|
8
|
+
export function ScatterPlot({ points, ...frame }) {
|
|
9
|
+
const size = frameSize(frame);
|
|
10
|
+
// 10% inset, but the left inset is floored so y labels never clip when small.
|
|
11
|
+
const bounds = ChartBounds.insets(size, Math.max(size.width * 0.1, 34), size.height * 0.1, size.width * 0.1, size.height * 0.1);
|
|
12
|
+
const data = points.filter((p) => Number.isFinite(p.x) && Number.isFinite(p.y));
|
|
13
|
+
const maxX = Math.max(0, ...data.map((p) => p.x));
|
|
14
|
+
const maxY = Math.max(0, ...data.map((p) => p.y));
|
|
15
|
+
if (!(maxX > 0) || !(maxY > 0))
|
|
16
|
+
return _jsx(Chart, { ...frame });
|
|
17
|
+
return (_jsxs(Chart, { ...frame, children: [_jsx("path", { d: `M${bounds.left},${bounds.top}V${bounds.bottom}H${bounds.right}`, fill: "none", stroke: theme.grid, strokeWidth: 1.5 }), ChartAxis.ticks(maxY, 4).map((value) => (_jsx(Label, { text: formatAxis(value), x: bounds.left - 7, y: bounds.bottom - (value / maxY) * bounds.height, h: "end", v: "center" }, `y${value}`))), ChartAxis.ticks(maxX, 4).map((value) => (_jsx(Label, { text: formatAxis(value), x: bounds.left + (value / maxX) * bounds.width, y: bounds.bottom + 6, h: "center" }, `x${value}`))), data.map((p, i) => {
|
|
18
|
+
const cx = bounds.left + (p.x / maxX) * bounds.width;
|
|
19
|
+
const cy = bounds.bottom - (p.y / maxY) * bounds.height;
|
|
20
|
+
const color = p.color ?? paletteAt(i);
|
|
21
|
+
return (_jsx("g", { className: "d-pop", children: _jsxs("g", { className: "d-mark", children: [_jsx("circle", { cx: cx, cy: cy, r: DOT_RADIUS * 2, fill: color, fillOpacity: 0.16 }), _jsx("circle", { cx: cx, cy: cy, r: DOT_RADIUS, fill: color, stroke: theme.surface, strokeWidth: 1.2 }), _jsx("title", { children: `${formatAxis(p.x)}, ${formatAxis(p.y)}` })] }) }, i));
|
|
22
|
+
})] }));
|
|
23
|
+
}
|
|
24
|
+
/** Convenience for raw `[x, y]` pairs. */
|
|
25
|
+
export function ScatterPlotValues({ values, ...frame }) {
|
|
26
|
+
return _jsx(ScatterPlot, { points: values.map(([x, y]) => ({ x, y })), ...frame });
|
|
27
|
+
}
|
|
28
|
+
/** Up to one fractional digit, trailing zero trimmed (3.0 → "3"). */
|
|
29
|
+
function formatAxis(value) {
|
|
30
|
+
return `${Math.round(value * 10) / 10}`;
|
|
31
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type ChartFrameProps } from '../svg/Chart.js';
|
|
2
|
+
import { type ChartPoint } from '../core/types.js';
|
|
3
|
+
export interface StepLineChartProps extends ChartFrameProps {
|
|
4
|
+
readonly points: readonly ChartPoint[];
|
|
5
|
+
readonly color?: string;
|
|
6
|
+
}
|
|
7
|
+
/** A stepped (stair) single-series line chart with a soft gradient fill and reveal. */
|
|
8
|
+
export declare function StepLineChart({ points, color, ...frame }: StepLineChartProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { useId } from 'react';
|
|
3
|
+
import { Chart, frameSize } from '../svg/Chart.js';
|
|
4
|
+
import { CategoryLabels, ValueGrid, VertexDot } from '../svg/primitives.js';
|
|
5
|
+
import { ChartBounds } from '../core/scales.js';
|
|
6
|
+
import { areaPath, polylinePath } from '../core/paths.js';
|
|
7
|
+
import { DrafterColors } from '../core/colors.js';
|
|
8
|
+
import { maxValue } from '../core/types.js';
|
|
9
|
+
/** A stepped (stair) single-series line chart with a soft gradient fill and reveal. */
|
|
10
|
+
export function StepLineChart({ points, color = DrafterColors.teal, ...frame }) {
|
|
11
|
+
const gradientId = useId();
|
|
12
|
+
const bounds = ChartBounds.padded(frameSize(frame));
|
|
13
|
+
const values = points.map((p) => (Number.isFinite(p.value) ? p.value : 0));
|
|
14
|
+
const max = maxValue(values);
|
|
15
|
+
const count = values.length;
|
|
16
|
+
const pixels = max > 0
|
|
17
|
+
? values.map((value, i) => ({
|
|
18
|
+
x: count === 1 ? bounds.left + bounds.width / 2 : bounds.left + (i / (count - 1)) * bounds.width,
|
|
19
|
+
y: bounds.bottom - (value / max) * bounds.height,
|
|
20
|
+
}))
|
|
21
|
+
: [];
|
|
22
|
+
// Horizontal to the next x at the prior y, then vertical to the next y.
|
|
23
|
+
const stairs = pixels.flatMap((p, i) => {
|
|
24
|
+
const prev = pixels[i - 1];
|
|
25
|
+
return prev ? [{ x: p.x, y: prev.y }, p] : [p];
|
|
26
|
+
});
|
|
27
|
+
const first = pixels[0];
|
|
28
|
+
const last = pixels[pixels.length - 1];
|
|
29
|
+
const line = polylinePath(stairs);
|
|
30
|
+
const top = pixels.reduce((min, p) => Math.min(min, p.y), bounds.bottom);
|
|
31
|
+
return (_jsxs(Chart, { ...frame, children: [_jsx(ValueGrid, { bounds: bounds, max: max }), _jsx(CategoryLabels, { bounds: bounds, categories: points.map((p) => p.label), count: count }), first && last && pixels.length >= 2 ? (_jsxs(_Fragment, { children: [_jsx("defs", { children: _jsxs("linearGradient", { id: gradientId, gradientUnits: "userSpaceOnUse", x1: 0, y1: top, x2: 0, y2: bounds.bottom, children: [_jsx("stop", { offset: 0, stopColor: color, stopOpacity: 0.22 }), _jsx("stop", { offset: 1, stopColor: color, stopOpacity: 0 })] }) }), _jsx("path", { className: "d-wipe", d: areaPath(line, first.x, last.x, bounds.bottom), fill: `url(#${gradientId})` }), _jsx("path", { className: "d-trace", d: line, pathLength: 1, fill: "none", stroke: color, strokeWidth: 3.5, strokeLinecap: "round", strokeLinejoin: "round" })] })) : null, _jsx("g", { className: "d-wipe", children: pixels.map((p, i) => (_jsx(VertexDot, { x: p.x, y: p.y, color: color, radius: 3, className: "d-mark", children: _jsx("title", { children: points[i]?.label ? `${points[i]?.label}: ${values[i]}` : `${values[i]}` }) }, i))) })] }));
|
|
32
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type ChartFrameProps } from '../svg/Chart.js';
|
|
2
|
+
import type { ChartSeries } from '../core/types.js';
|
|
3
|
+
export interface StreamGraphChartProps extends ChartFrameProps {
|
|
4
|
+
readonly series: readonly ChartSeries[];
|
|
5
|
+
readonly categories?: readonly string[];
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* A stream graph (themeriver): stacked series flow as smooth bands centred on
|
|
9
|
+
* the chart midline and grow symmetrically outward from it as they reveal.
|
|
10
|
+
* `categories` supplies the optional x-axis labels.
|
|
11
|
+
*/
|
|
12
|
+
export declare function StreamGraphChart({ series, categories, ...frame }: StreamGraphChartProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useId } from 'react';
|
|
3
|
+
import { Chart, frameSize } from '../svg/Chart.js';
|
|
4
|
+
import { Label } from '../svg/primitives.js';
|
|
5
|
+
import { smoothPath } from '../core/paths.js';
|
|
6
|
+
const MAX_LABELS = 6;
|
|
7
|
+
/**
|
|
8
|
+
* A stream graph (themeriver): stacked series flow as smooth bands centred on
|
|
9
|
+
* the chart midline and grow symmetrically outward from it as they reveal.
|
|
10
|
+
* `categories` supplies the optional x-axis labels.
|
|
11
|
+
*/
|
|
12
|
+
export function StreamGraphChart({ series, categories = [], ...frame }) {
|
|
13
|
+
const baseId = useId();
|
|
14
|
+
const size = frameSize(frame);
|
|
15
|
+
// Only sample the indices every series actually has.
|
|
16
|
+
const count = series.length === 0 ? 0 : Math.min(...series.map((s) => s.values.length));
|
|
17
|
+
const valueAt = (s, i) => {
|
|
18
|
+
const value = s.values[i];
|
|
19
|
+
return value !== undefined && Number.isFinite(value) ? Math.max(value, 0) : 0;
|
|
20
|
+
};
|
|
21
|
+
// 8% horizontal inset; a small vertical inset keeps the labels readable.
|
|
22
|
+
const chartLeft = size.width * 0.08;
|
|
23
|
+
const chartWidth = size.width * 0.84;
|
|
24
|
+
const chartTop = size.height * 0.06;
|
|
25
|
+
const chartHeight = size.height * 0.84;
|
|
26
|
+
const centerY = chartTop + chartHeight / 2;
|
|
27
|
+
const totals = Array.from({ length: Math.max(count, 0) }, (_, i) => series.reduce((sum, s) => sum + valueAt(s, i), 0));
|
|
28
|
+
const maxTotal = Math.max(0, ...totals);
|
|
29
|
+
if (count < 2 || !(maxTotal > 0))
|
|
30
|
+
return _jsx(Chart, { ...frame });
|
|
31
|
+
// The tallest stack fills ~80% of the available height.
|
|
32
|
+
const yScale = (chartHeight * 0.8) / maxTotal;
|
|
33
|
+
const xs = totals.map((_, i) => chartLeft + (i * chartWidth) / (count - 1));
|
|
34
|
+
const colors = [...new Set(series.map((s) => s.color))];
|
|
35
|
+
const stride = Math.max(1, Math.ceil(categories.length / MAX_LABELS));
|
|
36
|
+
// Walk top → bottom: each series stacks below the previous one.
|
|
37
|
+
let above = totals.map(() => 0);
|
|
38
|
+
const bands = series.map((s, k) => {
|
|
39
|
+
const top = xs.map((x, i) => ({ x, y: centerY + ((above[i] ?? 0) - (totals[i] ?? 0) / 2) * yScale }));
|
|
40
|
+
above = above.map((sum, i) => sum + valueAt(s, i));
|
|
41
|
+
const bottom = xs.map((x, i) => ({ x, y: centerY + ((above[i] ?? 0) - (totals[i] ?? 0) / 2) * yScale }));
|
|
42
|
+
const topEdge = smoothPath(top);
|
|
43
|
+
// Top edge left → right, bottom edge right → left, closed.
|
|
44
|
+
const outline = `${topEdge}L${smoothPath(bottom.reverse()).slice(1)}Z`;
|
|
45
|
+
return (_jsxs("g", { className: "d-mark", children: [_jsx("path", { d: outline, fill: `url(#${baseId}-${colors.indexOf(s.color)})` }), _jsx("path", { d: topEdge, fill: "none", stroke: "#fff", strokeOpacity: 0.22, strokeLinecap: "round", strokeLinejoin: "round" }), s.name ? _jsx("title", { children: s.name }) : null] }, k));
|
|
46
|
+
});
|
|
47
|
+
return (_jsxs(Chart, { ...frame, children: [_jsx("defs", { children: colors.map((color, k) => (_jsxs("linearGradient", { id: `${baseId}-${k}`, x1: 0, y1: 0, x2: 0, y2: 1, children: [_jsx("stop", { offset: 0, stopColor: color, stopOpacity: 0.99 }), _jsx("stop", { offset: 1, stopColor: color, stopOpacity: 0.97 })] }, k))) }), _jsx("g", { className: "d-grow-up", style: { transformOrigin: '50% 50%' }, children: bands }), categories.map((label, i) => {
|
|
48
|
+
const x = xs[i];
|
|
49
|
+
return x !== undefined && i % stride === 0 ? (_jsx(Label, { text: label, x: x, y: chartTop + chartHeight + 12, fontSize: 12, h: "center", v: "center" }, i)) : null;
|
|
50
|
+
})] }));
|
|
51
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type ChartFrameProps } from '../svg/Chart.js';
|
|
2
|
+
/** A root category, optionally split into children on the outer ring. */
|
|
3
|
+
export interface SunburstNode {
|
|
4
|
+
readonly label: string;
|
|
5
|
+
readonly value: number;
|
|
6
|
+
/** Children inherit their root's colour (lightened) when omitted. */
|
|
7
|
+
readonly color?: string;
|
|
8
|
+
readonly children?: readonly SunburstNode[];
|
|
9
|
+
}
|
|
10
|
+
export interface SunburstChartProps extends ChartFrameProps {
|
|
11
|
+
readonly roots: readonly SunburstNode[];
|
|
12
|
+
}
|
|
13
|
+
/** A two-ring sunburst: roots on the inner ring, their children on the outer one. */
|
|
14
|
+
export declare function SunburstChart({ roots, ...frame }: SunburstChartProps): import("react").JSX.Element;
|