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,50 @@
|
|
|
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 { ChartBounds } from '../core/scales.js';
|
|
5
|
+
import { DrafterColors, theme } from '../core/colors.js';
|
|
6
|
+
const GRID_LINES = 5;
|
|
7
|
+
/**
|
|
8
|
+
* A box-and-whisker chart: whiskers, translucent boxes and median lines, each
|
|
9
|
+
* group growing out from its median on the reveal.
|
|
10
|
+
*/
|
|
11
|
+
export function BoxPlotChart({ groups: input, ...frame }) {
|
|
12
|
+
const size = frameSize(frame);
|
|
13
|
+
// 10% inset, but the left inset is floored so y labels never clip.
|
|
14
|
+
const bounds = ChartBounds.insets(size, Math.max(size.width * 0.1, 34), size.height * 0.1, size.width * 0.1, size.height * 0.1);
|
|
15
|
+
const groups = input.filter((g) => [g.min, g.q1, g.median, g.q3, g.max].every(Number.isFinite));
|
|
16
|
+
if (groups.length === 0)
|
|
17
|
+
return _jsx(Chart, { ...frame });
|
|
18
|
+
const globalMin = Math.min(...groups.map((g) => g.min));
|
|
19
|
+
const globalMax = Math.max(...groups.map((g) => g.max));
|
|
20
|
+
const range = Math.max(globalMax - globalMin, 0.0001);
|
|
21
|
+
const yFor = (value) => bounds.bottom - ((value - globalMin) / range) * bounds.height;
|
|
22
|
+
const columnWidth = bounds.width / groups.length;
|
|
23
|
+
const boxWidth = Math.min(columnWidth * 0.45, 70);
|
|
24
|
+
const capHalf = boxWidth * 0.3;
|
|
25
|
+
return (_jsxs(Chart, { ...frame, children: [Array.from({ length: GRID_LINES + 1 }, (_, i) => {
|
|
26
|
+
const fraction = i / GRID_LINES;
|
|
27
|
+
const y = bounds.bottom - fraction * bounds.height;
|
|
28
|
+
return (_jsxs("g", { children: [_jsx("line", { x1: bounds.left, x2: bounds.right, y1: y, y2: y, stroke: theme.grid }), _jsx(Label, { text: formatAxis(globalMin + fraction * range), x: bounds.left - 8, y: y, h: "end", v: "center" })] }, i));
|
|
29
|
+
}), groups.map((group, index) => {
|
|
30
|
+
const color = group.color ?? DrafterColors.violet;
|
|
31
|
+
const centerX = bounds.left + columnWidth * (index + 0.5);
|
|
32
|
+
const boxLeft = centerX - boxWidth / 2;
|
|
33
|
+
const yMin = yFor(group.min);
|
|
34
|
+
const yMax = yFor(group.max);
|
|
35
|
+
const yMedian = yFor(group.median);
|
|
36
|
+
const yQ1 = yFor(group.q1);
|
|
37
|
+
const yQ3 = yFor(group.q3);
|
|
38
|
+
const boxTop = Math.min(yQ1, yQ3);
|
|
39
|
+
const boxHeight = Math.abs(yQ1 - yQ3);
|
|
40
|
+
// The group scales about its median, measured from the top of its box.
|
|
41
|
+
const top = Math.min(yMin, yMax, boxTop);
|
|
42
|
+
return (_jsxs("g", { children: [_jsxs("g", { className: "d-mark", children: [_jsxs("g", { className: "d-grow-up", style: { transformOrigin: `50% ${yMedian - top}px` }, children: [_jsx("path", { d: `M${centerX},${yMax}V${yMin}` +
|
|
43
|
+
`M${centerX - capHalf},${yMax}H${centerX + capHalf}` +
|
|
44
|
+
`M${centerX - capHalf},${yMin}H${centerX + capHalf}`, fill: "none", stroke: color, strokeWidth: 1.5 }), _jsx("rect", { x: boxLeft, y: boxTop, width: boxWidth, height: boxHeight, rx: Math.min(6, boxHeight / 2), fill: color, fillOpacity: 0.35, stroke: color, strokeWidth: 1.5 })] }), _jsx("line", { x1: boxLeft, x2: boxLeft + boxWidth, y1: yMedian, y2: yMedian, stroke: color, strokeWidth: 2.5 }), _jsx("title", { children: `${group.label ? `${group.label} · ` : ''}min ${formatAxis(group.min)}, Q1 ${formatAxis(group.q1)}, median ${formatAxis(group.median)}, Q3 ${formatAxis(group.q3)}, max ${formatAxis(group.max)}` })] }), _jsx(Label, { text: group.label, x: centerX, y: bounds.bottom + 8, h: "center" })] }, index));
|
|
45
|
+
})] }));
|
|
46
|
+
}
|
|
47
|
+
/** Up to one fractional digit, trailing zero trimmed (3.0 → "3"). */
|
|
48
|
+
function formatAxis(value) {
|
|
49
|
+
return `${Math.round(value * 10) / 10}`;
|
|
50
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type ChartFrameProps } from '../svg/Chart.js';
|
|
2
|
+
/** A single bubble: position (`x`, `y`), relative `size`, and a fill colour. */
|
|
3
|
+
export interface BubbleDatum {
|
|
4
|
+
readonly x: number;
|
|
5
|
+
readonly y: number;
|
|
6
|
+
readonly size: number;
|
|
7
|
+
/** Defaults to the Drafter blue. */
|
|
8
|
+
readonly color?: string;
|
|
9
|
+
}
|
|
10
|
+
export interface BubbleChartProps extends ChartFrameProps {
|
|
11
|
+
/** Groups of bubbles; they pop in as one staggered cascade. */
|
|
12
|
+
readonly series: ReadonlyArray<readonly BubbleDatum[]>;
|
|
13
|
+
}
|
|
14
|
+
/** A bubble chart with magnitude-based axes and a staggered reveal. */
|
|
15
|
+
export declare function BubbleChart({ series, ...frame }: BubbleChartProps): 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 } from '../svg/primitives.js';
|
|
4
|
+
import { ChartAxis } from '../core/scales.js';
|
|
5
|
+
import { DrafterColors, theme } from '../core/colors.js';
|
|
6
|
+
/** Longest total stagger, so a large data set never drags the entrance out. */
|
|
7
|
+
const MAX_CASCADE_MS = 900;
|
|
8
|
+
/** A bubble chart with magnitude-based axes and a staggered reveal. */
|
|
9
|
+
export function BubbleChart({ series, ...frame }) {
|
|
10
|
+
const size = frameSize(frame);
|
|
11
|
+
const originX = 40;
|
|
12
|
+
const originY = size.height - 20;
|
|
13
|
+
const chartWidth = size.width - 60;
|
|
14
|
+
const chartHeight = size.height - 60;
|
|
15
|
+
const all = series
|
|
16
|
+
.flat()
|
|
17
|
+
.filter((b) => Number.isFinite(b.x) && Number.isFinite(b.y) && Number.isFinite(b.size));
|
|
18
|
+
if (!(chartWidth > 0) || !(chartHeight > 0))
|
|
19
|
+
return _jsx(Chart, { ...frame });
|
|
20
|
+
const xRange = Math.max(niceCeil(Math.max(0, ...all.map((b) => b.x))), 0.0001);
|
|
21
|
+
const yRange = Math.max(niceCeil(Math.max(0, ...all.map((b) => b.y))), 0.0001);
|
|
22
|
+
const maxSize = Math.max(0, ...all.map((b) => b.size));
|
|
23
|
+
const scale = Math.min(chartWidth, chartHeight) / 6;
|
|
24
|
+
const stagger = all.length > 1 ? Math.min(80, MAX_CASCADE_MS / (all.length - 1)) : 0;
|
|
25
|
+
return (_jsxs(Chart, { ...frame, children: [gridValues(xRange).map((value) => {
|
|
26
|
+
const x = originX + (value / xRange) * chartWidth;
|
|
27
|
+
return (_jsxs("g", { children: [_jsx("line", { x1: x, x2: x, y1: originY, y2: originY - chartHeight, stroke: theme.grid }), _jsx(Label, { text: `${Math.trunc(value)}`, x: x, y: originY + 12, fontSize: 12, h: "center", v: "center" })] }, `x${value}`));
|
|
28
|
+
}), gridValues(yRange).map((value) => {
|
|
29
|
+
const y = originY - (value / yRange) * chartHeight;
|
|
30
|
+
return (_jsxs("g", { children: [_jsx("line", { x1: originX, x2: originX + chartWidth, y1: y, y2: y, stroke: theme.grid }), _jsx(Label, { text: `${Math.trunc(value)}`, x: originX - 6, y: y, fontSize: 12, h: "end", v: "center" })] }, `y${value}`));
|
|
31
|
+
}), _jsx("path", { d: `M${originX},${originY - chartHeight}V${originY}H${originX + chartWidth}`, fill: "none", stroke: theme.label }), maxSize > 0
|
|
32
|
+
? all.map((bubble, i) => {
|
|
33
|
+
const color = bubble.color ?? DrafterColors.blue;
|
|
34
|
+
const radius = (bubble.size / maxSize) * scale;
|
|
35
|
+
if (!(radius > 0))
|
|
36
|
+
return null;
|
|
37
|
+
return (_jsx("g", { className: "d-pop", style: delay(i * stagger), children: _jsx("circle", { className: "d-mark", cx: originX + (bubble.x / xRange) * chartWidth, cy: originY - (bubble.y / yRange) * chartHeight, r: radius, fill: color, fillOpacity: 0.3, stroke: color, strokeOpacity: 0.9, strokeWidth: 1.75, children: _jsx("title", { children: `${bubble.x}, ${bubble.y} · size ${bubble.size}` }) }) }, i));
|
|
38
|
+
})
|
|
39
|
+
: null] }));
|
|
40
|
+
}
|
|
41
|
+
/** Round a value up to a tidy axis bound (10s, then 25s, then 50s). */
|
|
42
|
+
function niceCeil(value) {
|
|
43
|
+
const step = value <= 50 ? 10 : value <= 100 ? 25 : 50;
|
|
44
|
+
return Math.ceil(value / step) * step;
|
|
45
|
+
}
|
|
46
|
+
function gridValues(range) {
|
|
47
|
+
const step = ChartAxis.gridStep(range);
|
|
48
|
+
return Array.from({ length: Math.max(Math.trunc(range / step), 0) + 1 }, (_, i) => i * step);
|
|
49
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type ChartFrameProps } from '../svg/Chart.js';
|
|
2
|
+
/** One bullet row: a measured value against a target, over qualitative ranges. */
|
|
3
|
+
export interface BulletMetric {
|
|
4
|
+
readonly label: string;
|
|
5
|
+
readonly value: number;
|
|
6
|
+
readonly target: number;
|
|
7
|
+
/** Upper bounds of the qualitative bands (e.g. poor / ok / good). */
|
|
8
|
+
readonly ranges: readonly number[];
|
|
9
|
+
readonly color?: string;
|
|
10
|
+
}
|
|
11
|
+
export interface BulletChartProps extends ChartFrameProps {
|
|
12
|
+
readonly metrics: readonly BulletMetric[];
|
|
13
|
+
}
|
|
14
|
+
/** Bullet charts: a value bar, a target tick and shaded ranges per metric. */
|
|
15
|
+
export declare function BulletChart({ metrics, ...frame }: BulletChartProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,42 @@
|
|
|
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 { DrafterColors } from '../core/colors.js';
|
|
5
|
+
const REVEAL_MS = 900;
|
|
6
|
+
function finite(value) {
|
|
7
|
+
return Number.isFinite(value) ? Math.max(value, 0) : 0;
|
|
8
|
+
}
|
|
9
|
+
function formatValue(value) {
|
|
10
|
+
return `${Math.round(value * 10) / 10}`;
|
|
11
|
+
}
|
|
12
|
+
/** Bullet charts: a value bar, a target tick and shaded ranges per metric. */
|
|
13
|
+
export function BulletChart({ metrics, ...frame }) {
|
|
14
|
+
const durationMs = frame.durationMs ?? REVEAL_MS;
|
|
15
|
+
const size = frameSize(frame);
|
|
16
|
+
if (metrics.length === 0)
|
|
17
|
+
return _jsx(Chart, { ...frame, durationMs: durationMs });
|
|
18
|
+
const chartLeft = size.width * 0.1;
|
|
19
|
+
const chartTop = size.height * 0.1;
|
|
20
|
+
const chartWidth = size.width * 0.8;
|
|
21
|
+
const rowSlot = (size.height * 0.8) / metrics.length;
|
|
22
|
+
const rowHeight = rowSlot * 0.55;
|
|
23
|
+
const gutter = chartWidth * 0.28;
|
|
24
|
+
const trackLeft = chartLeft + gutter;
|
|
25
|
+
const trackWidth = chartWidth - gutter;
|
|
26
|
+
return (_jsx(Chart, { ...frame, durationMs: durationMs, children: metrics.map((metric, index) => {
|
|
27
|
+
const rowTop = chartTop + rowSlot * index + (rowSlot - rowHeight) / 2;
|
|
28
|
+
const rowCenterY = rowTop + rowHeight / 2;
|
|
29
|
+
const value = finite(metric.value);
|
|
30
|
+
const target = finite(metric.target);
|
|
31
|
+
const ranges = metric.ranges.map(finite).sort((a, b) => a - b);
|
|
32
|
+
const max = Math.max(value, target, ...ranges) || 1;
|
|
33
|
+
const toX = (v) => trackLeft + (v / max) * trackWidth;
|
|
34
|
+
const measureHeight = rowHeight * 0.42;
|
|
35
|
+
const color = metric.color ?? DrafterColors.indigo;
|
|
36
|
+
const targetX = toX(target);
|
|
37
|
+
return (_jsxs("g", { className: "d-mark", children: [ranges.map((end, r) => {
|
|
38
|
+
const start = toX(r === 0 ? 0 : ranges[r - 1] ?? 0);
|
|
39
|
+
return (_jsx("rect", { x: start, y: rowTop, width: Math.max(toX(end) - start, 0), height: rowHeight, rx: 4, fill: "currentColor", fillOpacity: 0.06 + 0.07 * r }, r));
|
|
40
|
+
}), _jsx("rect", { className: "d-grow-right", x: trackLeft, y: rowCenterY - measureHeight / 2, width: Math.max(toX(value) - trackLeft, 0), height: measureHeight, rx: measureHeight / 2, fill: color }), _jsx("line", { x1: targetX, x2: targetX, y1: rowTop - 2, y2: rowTop + rowHeight + 2, stroke: "currentColor", strokeWidth: 2 }), _jsx(Label, { text: metric.label.length > 8 ? `${metric.label.substring(0, 7)}…` : metric.label, x: chartLeft, y: rowCenterY, v: "center" }), _jsx(Label, { text: formatValue(value), x: chartLeft + chartWidth, y: rowTop - 1, color: color, h: "end", v: "bottom" }), _jsx("title", { children: `${metric.label}: ${formatValue(value)} (target ${formatValue(target)})` })] }, index));
|
|
41
|
+
}) }));
|
|
42
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type ChartFrameProps } from '../svg/Chart.js';
|
|
2
|
+
/** One candle: a label plus the open / high / low / close prices. */
|
|
3
|
+
export interface Candle {
|
|
4
|
+
readonly label: string;
|
|
5
|
+
readonly open: number;
|
|
6
|
+
readonly high: number;
|
|
7
|
+
readonly low: number;
|
|
8
|
+
readonly close: number;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* A simple moving-average overlay (the classic MA5 / MA10 / MA20 study): the
|
|
12
|
+
* trailing average of closing prices over `period` candles, drawn as a smooth
|
|
13
|
+
* line in `color`.
|
|
14
|
+
*/
|
|
15
|
+
export interface MovingAverage {
|
|
16
|
+
readonly period: number;
|
|
17
|
+
readonly color: string;
|
|
18
|
+
}
|
|
19
|
+
export interface CandlestickChartProps extends ChartFrameProps {
|
|
20
|
+
readonly candles: readonly Candle[];
|
|
21
|
+
readonly movingAverages?: readonly MovingAverage[];
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* A candlestick (K-line) chart with high-low wicks, open-close bodies and
|
|
25
|
+
* optional moving-average overlays. Candles grow out from their body centre
|
|
26
|
+
* while the moving averages trace in left to right.
|
|
27
|
+
*/
|
|
28
|
+
export declare function CandlestickChart({ candles: input, movingAverages, ...frame }: CandlestickChartProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,70 @@
|
|
|
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 { ChartBounds } from '../core/scales.js';
|
|
5
|
+
import { smoothPath } from '../core/paths.js';
|
|
6
|
+
import { DrafterColors, theme } from '../core/colors.js';
|
|
7
|
+
const Y_STEPS = 4;
|
|
8
|
+
const LEGEND_FONT = 12;
|
|
9
|
+
/**
|
|
10
|
+
* A candlestick (K-line) chart with high-low wicks, open-close bodies and
|
|
11
|
+
* optional moving-average overlays. Candles grow out from their body centre
|
|
12
|
+
* while the moving averages trace in left to right.
|
|
13
|
+
*/
|
|
14
|
+
export function CandlestickChart({ candles: input, movingAverages = [], ...frame }) {
|
|
15
|
+
const size = frameSize(frame);
|
|
16
|
+
// 10% inset, but the left inset is floored so price labels never clip.
|
|
17
|
+
const bounds = ChartBounds.insets(size, Math.max(size.width * 0.1, 34), size.height * 0.1, size.width * 0.1, size.height * 0.1);
|
|
18
|
+
const candles = input.filter((c) => [c.open, c.high, c.low, c.close].every(Number.isFinite));
|
|
19
|
+
if (candles.length === 0)
|
|
20
|
+
return _jsx(Chart, { ...frame });
|
|
21
|
+
const minLow = Math.min(...candles.map((c) => c.low));
|
|
22
|
+
const maxHigh = Math.max(...candles.map((c) => c.high));
|
|
23
|
+
const range = Math.max(maxHigh - minLow, 0.0001);
|
|
24
|
+
const yFor = (value) => bounds.bottom - ((value - minLow) / range) * bounds.height;
|
|
25
|
+
const slot = bounds.width / candles.length;
|
|
26
|
+
const centerXFor = (index) => bounds.left + slot * (index + 0.5);
|
|
27
|
+
const bodyWidth = Math.max(slot * 0.6, 2);
|
|
28
|
+
const labelEvery = Math.max(1, Math.trunc(candles.length / 8));
|
|
29
|
+
let legendX = bounds.left + 4;
|
|
30
|
+
return (_jsxs(Chart, { ...frame, children: [Array.from({ length: Y_STEPS + 1 }, (_, i) => {
|
|
31
|
+
const value = minLow + range * (i / Y_STEPS);
|
|
32
|
+
const y = yFor(value);
|
|
33
|
+
return (_jsxs("g", { children: [_jsx("line", { x1: bounds.left, x2: bounds.right, y1: y, y2: y, stroke: theme.grid }), _jsx(Label, { text: formatPrice(value), x: bounds.left - 8, y: y, fontSize: 12, h: "end", v: "center" })] }, i));
|
|
34
|
+
}), _jsx("path", { d: `M${bounds.left},${bounds.top}V${bounds.bottom}H${bounds.right}`, fill: "none", stroke: theme.grid, strokeWidth: 1.5 }), candles.map((candle, index) => {
|
|
35
|
+
const color = candle.close >= candle.open ? DrafterColors.green : DrafterColors.coral;
|
|
36
|
+
const bodyTopValue = Math.max(candle.open, candle.close);
|
|
37
|
+
const bodyBottomValue = Math.min(candle.open, candle.close);
|
|
38
|
+
const centerX = centerXFor(index);
|
|
39
|
+
const centerY = yFor((bodyTopValue + bodyBottomValue) / 2);
|
|
40
|
+
const bodyHeight = Math.max(yFor(bodyBottomValue) - yFor(bodyTopValue), 2);
|
|
41
|
+
const bodyTop = centerY - bodyHeight / 2;
|
|
42
|
+
const highY = yFor(candle.high);
|
|
43
|
+
const lowY = yFor(candle.low);
|
|
44
|
+
// The glyph scales about its body centre, measured from the top of its box.
|
|
45
|
+
const boxTop = Math.min(highY, lowY, bodyTop - 2);
|
|
46
|
+
return (_jsxs("g", { children: [_jsx("g", { className: "d-grow-up", style: { transformOrigin: `50% ${centerY - boxTop}px` }, children: _jsxs("g", { className: "d-mark", children: [_jsx("line", { x1: centerX, x2: centerX, y1: highY, y2: lowY, stroke: color, strokeOpacity: 0.9, strokeWidth: 1.5 }), _jsx("rect", { x: centerX - bodyWidth / 2 - 2, y: bodyTop - 2, width: bodyWidth + 4, height: bodyHeight + 4, rx: 3, fill: color, fillOpacity: 0.18 }), _jsx("rect", { x: centerX - bodyWidth / 2, y: bodyTop, width: bodyWidth, height: bodyHeight, rx: Math.min(3, bodyWidth / 2, bodyHeight / 2), fill: color }), _jsx("title", { children: `${candle.label ? `${candle.label} · ` : ''}O ${formatPrice(candle.open)} H ${formatPrice(candle.high)} L ${formatPrice(candle.low)} C ${formatPrice(candle.close)}` })] }) }), index % labelEvery === 0 ? (_jsx(Label, { text: candle.label, x: centerX, y: bounds.bottom + 6, fontSize: 12, h: "center" })) : null] }, index));
|
|
47
|
+
}), movingAverages.map((ma, i) => {
|
|
48
|
+
const period = Math.trunc(ma.period);
|
|
49
|
+
if (!(period > 0) || period > candles.length)
|
|
50
|
+
return null;
|
|
51
|
+
const points = [];
|
|
52
|
+
for (let end = period - 1; end < candles.length; end++) {
|
|
53
|
+
const window = candles.slice(end - period + 1, end + 1);
|
|
54
|
+
const mean = window.reduce((sum, c) => sum + c.close, 0) / period;
|
|
55
|
+
points.push({ x: centerXFor(end), y: yFor(mean) });
|
|
56
|
+
}
|
|
57
|
+
if (points.length < 2)
|
|
58
|
+
return null;
|
|
59
|
+
return (_jsx("path", { className: "d-trace", d: smoothPath(points), pathLength: 1, fill: "none", stroke: ma.color, strokeWidth: 1.75, strokeLinecap: "round", strokeLinejoin: "round" }, i));
|
|
60
|
+
}), movingAverages.map((ma, i) => {
|
|
61
|
+
const text = `MA${ma.period}`;
|
|
62
|
+
const x = legendX;
|
|
63
|
+
legendX += text.length * LEGEND_FONT * 0.62 + 10;
|
|
64
|
+
return _jsx(Label, { text: text, x: x, y: bounds.top + 2, color: ma.color, fontSize: LEGEND_FONT }, i);
|
|
65
|
+
})] }));
|
|
66
|
+
}
|
|
67
|
+
/** One fractional digit, trailing zero trimmed (3.0 → "3"). */
|
|
68
|
+
function formatPrice(value) {
|
|
69
|
+
return `${Math.round(value * 10) / 10}`;
|
|
70
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type ChartFrameProps } from '../svg/Chart.js';
|
|
2
|
+
/** One stage of the funnel, widest value at the top. */
|
|
3
|
+
export interface FunnelStage {
|
|
4
|
+
readonly label: string;
|
|
5
|
+
readonly value: number;
|
|
6
|
+
readonly color?: string;
|
|
7
|
+
}
|
|
8
|
+
export interface FunnelChartProps extends ChartFrameProps {
|
|
9
|
+
readonly stages: readonly FunnelStage[];
|
|
10
|
+
}
|
|
11
|
+
/** A funnel of tapering bands, each narrowing to the width of the next stage. */
|
|
12
|
+
export declare function FunnelChart({ stages, ...frame }: FunnelChartProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useId } from 'react';
|
|
3
|
+
import { Chart, delay, 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
|
+
/** The narrowest a band may get, as a fraction of the chart width. */
|
|
8
|
+
const MIN_WIDTH_FRACTION = 0.12;
|
|
9
|
+
/** A funnel of tapering bands, each narrowing to the width of the next stage. */
|
|
10
|
+
export function FunnelChart({ stages, ...frame }) {
|
|
11
|
+
const shadeId = useId();
|
|
12
|
+
const durationMs = frame.durationMs ?? REVEAL_MS;
|
|
13
|
+
const size = frameSize(frame);
|
|
14
|
+
const count = stages.length;
|
|
15
|
+
const chartTop = size.height * 0.08;
|
|
16
|
+
const chartWidth = size.width * 0.8;
|
|
17
|
+
const chartHeight = size.height * 0.84;
|
|
18
|
+
const centerX = size.width / 2;
|
|
19
|
+
const gap = chartHeight * 0.02;
|
|
20
|
+
const bandHeight = count > 0 ? (chartHeight - gap * (count - 1)) / count : 0;
|
|
21
|
+
const values = stages.map((s) => (Number.isFinite(s.value) && s.value > 0 ? s.value : 0));
|
|
22
|
+
const max = Math.max(...values, 0) || 1;
|
|
23
|
+
const halfWidth = (value) => (chartWidth * (MIN_WIDTH_FRACTION + (1 - MIN_WIDTH_FRACTION) * (value / max))) / 2;
|
|
24
|
+
const labelHeight = 13 * 1.2;
|
|
25
|
+
const valueHeight = 11 * 1.2;
|
|
26
|
+
return (_jsx(Chart, { ...frame, durationMs: durationMs, children: bandHeight > 0
|
|
27
|
+
? stages.map((stage, i) => {
|
|
28
|
+
const value = values[i] ?? 0;
|
|
29
|
+
const top = chartTop + i * (bandHeight + gap);
|
|
30
|
+
const bottom = top + bandHeight;
|
|
31
|
+
const topHalf = halfWidth(value);
|
|
32
|
+
const bottomHalf = halfWidth(values[i + 1] ?? value);
|
|
33
|
+
const d = `M${centerX - topHalf},${top}L${centerX + topHalf},${top}` +
|
|
34
|
+
`L${centerX + bottomHalf},${bottom}L${centerX - bottomHalf},${bottom}Z`;
|
|
35
|
+
const color = stage.color ?? paletteAt(i);
|
|
36
|
+
const textTop = top + bandHeight / 2 - (labelHeight + valueHeight + 2) / 2;
|
|
37
|
+
return (_jsx("g", { className: "d-pop", style: delay((i * durationMs * 0.4) / count), children: _jsxs("g", { className: "d-mark", children: [_jsxs("linearGradient", { id: `${shadeId}-${i}`, x1: 0, y1: 0, x2: 0, y2: 1, children: [_jsx("stop", { offset: 0, stopColor: color, stopOpacity: 0.95 }), _jsx("stop", { offset: 1, stopColor: color, stopOpacity: 0.7 })] }), _jsx("path", { d: d, fill: `url(#${shadeId}-${i})` }), _jsx("line", { x1: centerX - topHalf, x2: centerX + topHalf, y1: top, y2: top, stroke: "#fff", strokeOpacity: 0.22, strokeWidth: 1.25 }), _jsx(Label, { text: stage.label, x: centerX, y: textTop + labelHeight / 2, color: "#fff", fontSize: 13, h: "center", v: "center" }), _jsx("g", { opacity: 0.8, children: _jsx(Label, { text: formatTick(value), x: centerX, y: textTop + labelHeight + 2 + valueHeight / 2, color: "#fff", h: "center", v: "center" }) }), _jsx("title", { children: `${stage.label}: ${formatTick(value)}` })] }) }, i));
|
|
38
|
+
})
|
|
39
|
+
: null }));
|
|
40
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type ChartFrameProps } from '../svg/Chart.js';
|
|
2
|
+
/** One scheduled task, measured in months from the chart origin. */
|
|
3
|
+
export interface GanttTask {
|
|
4
|
+
readonly name: string;
|
|
5
|
+
readonly startMonth: number;
|
|
6
|
+
readonly duration: number;
|
|
7
|
+
readonly color?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface GanttChartProps extends ChartFrameProps {
|
|
10
|
+
readonly tasks: readonly GanttTask[];
|
|
11
|
+
}
|
|
12
|
+
/** A Gantt chart: one rounded bar per task on a shared month axis. */
|
|
13
|
+
export declare function GanttChart({ tasks, ...frame }: GanttChartProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Chart, frameSize } from '../svg/Chart.js';
|
|
3
|
+
import { Label, formatTick } from '../svg/primitives.js';
|
|
4
|
+
import { paletteAt, theme } from '../core/colors.js';
|
|
5
|
+
const REVEAL_MS = 2000;
|
|
6
|
+
/** Beyond this many whole months, ticks fall back to just the two ends. */
|
|
7
|
+
const MAX_MONTH_TICKS = 600;
|
|
8
|
+
function finite(value) {
|
|
9
|
+
return Number.isFinite(value) ? Math.max(value, 0) : 0;
|
|
10
|
+
}
|
|
11
|
+
/** A Gantt chart: one rounded bar per task on a shared month axis. */
|
|
12
|
+
export function GanttChart({ tasks, ...frame }) {
|
|
13
|
+
const durationMs = frame.durationMs ?? REVEAL_MS;
|
|
14
|
+
const size = frameSize(frame);
|
|
15
|
+
if (tasks.length === 0)
|
|
16
|
+
return _jsx(Chart, { ...frame, durationMs: durationMs });
|
|
17
|
+
const chartLeft = size.width * 0.2;
|
|
18
|
+
const chartTop = size.height * 0.1;
|
|
19
|
+
const chartWidth = size.width * 0.7;
|
|
20
|
+
const chartHeight = size.height * 0.8;
|
|
21
|
+
const chartBottom = chartTop + chartHeight;
|
|
22
|
+
const spans = tasks.map((t) => ({ start: finite(t.startMonth), duration: finite(t.duration) }));
|
|
23
|
+
const maxMonth = Math.max(1, ...spans.map((s) => s.start + s.duration));
|
|
24
|
+
const rowHeight = Math.max(chartHeight / tasks.length, 1);
|
|
25
|
+
const barHeight = Math.max(rowHeight * 0.8, 1);
|
|
26
|
+
const months = new Set([0, maxMonth]);
|
|
27
|
+
if (maxMonth <= MAX_MONTH_TICKS) {
|
|
28
|
+
for (const s of spans)
|
|
29
|
+
for (let m = s.start; m <= s.start + s.duration; m++)
|
|
30
|
+
months.add(m);
|
|
31
|
+
}
|
|
32
|
+
const sorted = [...months].sort((a, b) => a - b);
|
|
33
|
+
const stride = Math.max(1, Math.ceil(sorted.length / Math.max(2, Math.trunc(chartWidth / 22))));
|
|
34
|
+
return (_jsxs(Chart, { ...frame, durationMs: durationMs, children: [_jsx("path", { d: `M${chartLeft},${chartTop}V${chartBottom}H${chartLeft + chartWidth}`, fill: "none", stroke: theme.label, strokeWidth: 1.5 }), tasks.map((task, i) => (_jsx(Label, { text: task.name.length > 9 ? `${task.name.substring(0, 8)}…` : task.name, x: chartLeft - 6, y: chartTop + i * rowHeight + rowHeight / 2, h: "end", v: "center" }, i))), sorted.map((month, i) => {
|
|
35
|
+
const isLast = i === sorted.length - 1;
|
|
36
|
+
if (i % stride !== 0 && !isLast)
|
|
37
|
+
return null;
|
|
38
|
+
return (_jsx(Label, { text: formatTick(month), x: Math.min(chartLeft + (month / maxMonth) * chartWidth, size.width - 6), y: chartBottom + 6, h: isLast ? 'end' : 'center' }, month));
|
|
39
|
+
}), _jsx("g", { className: "d-fade", children: tasks.map((task, i) => {
|
|
40
|
+
const span = spans[i] ?? { start: 0, duration: 0 };
|
|
41
|
+
return (_jsxs("g", { className: "d-mark", children: [_jsx("rect", { className: "d-grow-right", x: chartLeft + (span.start / maxMonth) * chartWidth, y: chartTop + i * rowHeight + rowHeight * 0.1, width: Math.max((span.duration / maxMonth) * chartWidth, 1), height: barHeight, rx: Math.min(6, barHeight / 2), fill: task.color ?? paletteAt(i) }), _jsx("title", { children: `${task.name}: month ${formatTick(span.start)}–${formatTick(span.start + span.duration)}` })] }, i));
|
|
42
|
+
}) })] }));
|
|
43
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type ChartFrameProps } from '../svg/Chart.js';
|
|
2
|
+
export interface GaugeChartProps extends ChartFrameProps {
|
|
3
|
+
readonly value: number;
|
|
4
|
+
readonly min?: number;
|
|
5
|
+
readonly max?: number;
|
|
6
|
+
/** Text drawn under the value. (`label` is the frame's accessible name.) */
|
|
7
|
+
readonly caption?: string;
|
|
8
|
+
/** Knob ring colour. */
|
|
9
|
+
readonly color?: string;
|
|
10
|
+
}
|
|
11
|
+
/** A 240° gauge: a palette-swept value arc over a faint track, with a riding knob. */
|
|
12
|
+
export declare function GaugeChart({ value, min, max, caption, color, ...frame }: GaugeChartProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { jsx as _jsx, Fragment as _Fragment, 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 { RadialLayout } from '../core/scales.js';
|
|
6
|
+
import { arcPath } from '../core/paths.js';
|
|
7
|
+
import { DrafterColors, drafterPalette, theme } from '../core/colors.js';
|
|
8
|
+
const REVEAL_MS = 900;
|
|
9
|
+
const DEG = Math.PI / 180;
|
|
10
|
+
/** 240° sweep, opening downward: from 150° clockwise to 390°. */
|
|
11
|
+
const START = 150 * DEG;
|
|
12
|
+
const SWEEP = 240 * DEG;
|
|
13
|
+
/** Angular size of one painted step of the palette sweep. */
|
|
14
|
+
const STEP = 4 * DEG;
|
|
15
|
+
function channels(hex) {
|
|
16
|
+
const v = parseInt(hex.slice(1), 16);
|
|
17
|
+
return [(v >> 16) & 0xff, (v >> 8) & 0xff, v & 0xff];
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The Drafter palette as a sweep gradient around the centre (0 = +x, clockwise),
|
|
21
|
+
* wrapping back to the first colour. SVG has no conic gradient, so the arc is
|
|
22
|
+
* painted in small steps of this function.
|
|
23
|
+
*/
|
|
24
|
+
function sweepColor(angle) {
|
|
25
|
+
const turn = (((angle / (Math.PI * 2)) % 1) + 1) % 1;
|
|
26
|
+
const scaled = turn * drafterPalette.length;
|
|
27
|
+
const i = Math.floor(scaled) % drafterPalette.length;
|
|
28
|
+
const a = channels(drafterPalette[i] ?? DrafterColors.blue);
|
|
29
|
+
const b = channels(drafterPalette[(i + 1) % drafterPalette.length] ?? DrafterColors.blue);
|
|
30
|
+
const t = scaled - Math.floor(scaled);
|
|
31
|
+
const mix = (k) => Math.round(a[k] + (b[k] - a[k]) * t);
|
|
32
|
+
return `rgb(${mix(0)},${mix(1)},${mix(2)})`;
|
|
33
|
+
}
|
|
34
|
+
function text(value) {
|
|
35
|
+
return Number.isFinite(value) ? formatTick(value) : '–';
|
|
36
|
+
}
|
|
37
|
+
/** A 240° gauge: a palette-swept value arc over a faint track, with a riding knob. */
|
|
38
|
+
export function GaugeChart({ value, min = 0, max = 100, caption = '', color = DrafterColors.teal, ...frame }) {
|
|
39
|
+
const maskId = useId();
|
|
40
|
+
const size = frameSize(frame);
|
|
41
|
+
const layout = new RadialLayout(size, 0.82);
|
|
42
|
+
const { x: cx, y: cy } = layout.center;
|
|
43
|
+
const radius = layout.radius;
|
|
44
|
+
const durationMs = frame.durationMs ?? REVEAL_MS;
|
|
45
|
+
if (!(radius > 0))
|
|
46
|
+
return _jsx(Chart, { ...frame, durationMs: durationMs });
|
|
47
|
+
const strokeWidth = radius * 0.16;
|
|
48
|
+
const arcRadius = radius - strokeWidth / 2;
|
|
49
|
+
const knobRadius = strokeWidth * 0.42;
|
|
50
|
+
const span = max - min;
|
|
51
|
+
const ratio = (value - min) / (span === 0 ? 1 : span);
|
|
52
|
+
const fraction = Number.isFinite(ratio) ? Math.min(Math.max(ratio, 0), 1) : 0;
|
|
53
|
+
const end = START + SWEEP * fraction;
|
|
54
|
+
const valuePath = fraction > 0 ? arcPath(cx, cy, arcRadius, START, end) : '';
|
|
55
|
+
const knob = layout.pointAt(end, arcRadius);
|
|
56
|
+
// Paint a little past both ends so the round-capped mask has colour under its caps.
|
|
57
|
+
const capAngle = strokeWidth / 2 / arcRadius;
|
|
58
|
+
const steps = [];
|
|
59
|
+
for (let a = START - capAngle; a < end + capAngle; a += STEP) {
|
|
60
|
+
steps.push({ from: a, to: Math.min(a + STEP, end + capAngle) + 0.5 * DEG });
|
|
61
|
+
}
|
|
62
|
+
const valueFontSize = Math.min(Math.max(radius * 0.04, 22), 44);
|
|
63
|
+
const valueH = valueFontSize * 1.2;
|
|
64
|
+
const captionH = caption ? 13 * 1.2 : 0;
|
|
65
|
+
const gap = 6;
|
|
66
|
+
const blockTop = cy - (valueH + (caption ? captionH + gap : 0)) / 2;
|
|
67
|
+
const endDistance = arcRadius + strokeWidth * 0.9;
|
|
68
|
+
const minPt = layout.pointAt(START, endDistance);
|
|
69
|
+
const maxPt = layout.pointAt(START + SWEEP, endDistance);
|
|
70
|
+
return (_jsxs(Chart, { ...frame, durationMs: durationMs, children: [_jsx("path", { d: arcPath(cx, cy, arcRadius, START, START + SWEEP), fill: "none", stroke: theme.grid, strokeWidth: strokeWidth, strokeLinecap: "round" }), valuePath ? (_jsxs(_Fragment, { children: [_jsx("mask", { id: maskId, maskUnits: "userSpaceOnUse", x: 0, y: 0, width: size.width, height: size.height, children: _jsx("path", { className: "d-trace", d: valuePath, pathLength: 1, fill: "none", stroke: "#fff", strokeWidth: strokeWidth, strokeLinecap: "round" }) }), _jsx("g", { mask: `url(#${maskId})`, fill: "none", strokeWidth: strokeWidth + 1, children: steps.map((step, i) => (_jsx("path", { d: arcPath(cx, cy, arcRadius, step.from, step.to), stroke: sweepColor((step.from + step.to) / 2) }, i))) }), _jsx("g", { className: "d-ride", style: { offsetPath: `path("${valuePath}")` }, children: _jsx("circle", { r: knobRadius, fill: "#fff", stroke: color, strokeWidth: 1.5 }) })] })) : (_jsx("circle", { cx: knob.x, cy: knob.y, r: knobRadius, fill: "#fff", stroke: color, strokeWidth: 1.5 })), _jsx(Label, { text: text(value), x: cx, y: blockTop + valueH / 2, color: "currentColor", fontSize: valueFontSize, weight: 700, h: "center", v: "center" }), caption ? (_jsx("g", { opacity: 0.65, children: _jsx(Label, { text: caption, x: cx, y: blockTop + valueH + gap + captionH / 2, color: "currentColor", fontSize: 13, h: "center", v: "center" }) })) : null, _jsx(Label, { text: text(min), x: minPt.x, y: minPt.y, fontSize: 12, h: "center", v: "center" }), _jsx(Label, { text: text(max), x: maxPt.x, y: maxPt.y, fontSize: 12, h: "center", v: "center" })] }));
|
|
71
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type ChartFrameProps } from '../svg/Chart.js';
|
|
2
|
+
/** Activity on one calendar day; several entries on the same day add up. */
|
|
3
|
+
export interface ContributionData {
|
|
4
|
+
readonly date: Date;
|
|
5
|
+
readonly count: number;
|
|
6
|
+
}
|
|
7
|
+
export interface HeatmapProps extends ChartFrameProps {
|
|
8
|
+
readonly contributions: readonly ContributionData[];
|
|
9
|
+
/** Colour of an active day; its opacity steps up with the count. */
|
|
10
|
+
readonly baseColor?: string;
|
|
11
|
+
/** Colour of a day with no activity. Defaults to the theme's grid colour. */
|
|
12
|
+
readonly backgroundSquareColor?: string;
|
|
13
|
+
}
|
|
14
|
+
/** A contribution calendar: 53 weeks of day cells ending at the latest date in the data. */
|
|
15
|
+
export declare function Heatmap({ contributions, baseColor, backgroundSquareColor, ...frame }: HeatmapProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { Chart, frameSize } from '../svg/Chart.js';
|
|
3
|
+
import { theme } from '../core/colors.js';
|
|
4
|
+
const DEFAULT_BASE_COLOR = '#40c463';
|
|
5
|
+
const CELL_SIZE = 8;
|
|
6
|
+
const CELL_PADDING = 2;
|
|
7
|
+
const WEEKS = 53;
|
|
8
|
+
function startOfDay(date) {
|
|
9
|
+
return new Date(date.getFullYear(), date.getMonth(), date.getDate());
|
|
10
|
+
}
|
|
11
|
+
function addDays(date, days) {
|
|
12
|
+
return new Date(date.getFullYear(), date.getMonth(), date.getDate() + days);
|
|
13
|
+
}
|
|
14
|
+
/** Four intensity steps of the base colour, like a contribution calendar. */
|
|
15
|
+
function intensity(count) {
|
|
16
|
+
if (count <= 3)
|
|
17
|
+
return 0.35;
|
|
18
|
+
if (count <= 6)
|
|
19
|
+
return 0.6;
|
|
20
|
+
if (count <= 9)
|
|
21
|
+
return 0.8;
|
|
22
|
+
return 1;
|
|
23
|
+
}
|
|
24
|
+
function isoDay(date) {
|
|
25
|
+
const pad = (v) => `${v}`.padStart(2, '0');
|
|
26
|
+
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
|
|
27
|
+
}
|
|
28
|
+
/** A contribution calendar: 53 weeks of day cells ending at the latest date in the data. */
|
|
29
|
+
export function Heatmap({ contributions, baseColor = DEFAULT_BASE_COLOR, backgroundSquareColor = theme.grid, ...frame }) {
|
|
30
|
+
const size = frameSize(frame);
|
|
31
|
+
const countsByDay = new Map();
|
|
32
|
+
for (const { date, count } of contributions) {
|
|
33
|
+
const day = startOfDay(date).getTime();
|
|
34
|
+
if (Number.isFinite(day) && Number.isFinite(count))
|
|
35
|
+
countsByDay.set(day, (countsByDay.get(day) ?? 0) + count);
|
|
36
|
+
}
|
|
37
|
+
const latest = countsByDay.size > 0 ? new Date(Math.max(...countsByDay.keys())) : startOfDay(new Date());
|
|
38
|
+
const rawStart = addDays(latest, -(WEEKS * 7 - 1));
|
|
39
|
+
const startDate = addDays(rawStart, -rawStart.getDay());
|
|
40
|
+
const step = Math.max(CELL_SIZE, Math.min(size.width / WEEKS, size.height / 7));
|
|
41
|
+
const cell = Math.max(2, step - CELL_PADDING);
|
|
42
|
+
const originX = (size.width - step * WEEKS) / 2;
|
|
43
|
+
const originY = (size.height - step * 7) / 2;
|
|
44
|
+
const cells = [];
|
|
45
|
+
for (let index = 0; index < WEEKS * 7; index++) {
|
|
46
|
+
const day = addDays(startDate, index);
|
|
47
|
+
if (day.getTime() > latest.getTime())
|
|
48
|
+
break;
|
|
49
|
+
const count = countsByDay.get(day.getTime()) ?? 0;
|
|
50
|
+
const active = count >= 1;
|
|
51
|
+
cells.push(_jsx("rect", { className: "d-mark", x: originX + Math.floor(index / 7) * step, y: originY + (index % 7) * step, width: cell, height: cell, rx: Math.min(2, cell * 0.25), fill: active ? baseColor : backgroundSquareColor, fillOpacity: active ? intensity(count) : undefined, children: _jsx("title", { children: `${isoDay(day)}: ${count}` }) }, index));
|
|
52
|
+
}
|
|
53
|
+
return (_jsx(Chart, { ...frame, children: _jsx("g", { className: "d-fade", children: cells }) }));
|
|
54
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type ChartFrameProps } from '../svg/Chart.js';
|
|
2
|
+
import { type ChartPoint, type ChartSeries } from '../core/types.js';
|
|
3
|
+
export interface LineChartProps extends ChartFrameProps {
|
|
4
|
+
readonly points: readonly ChartPoint[];
|
|
5
|
+
readonly color?: string;
|
|
6
|
+
}
|
|
7
|
+
/** A smooth single-series line chart with a soft gradient fill and reveal. */
|
|
8
|
+
export declare function LineChart({ points, color, ...frame }: LineChartProps): import("react").JSX.Element;
|
|
9
|
+
export interface GroupedLineChartProps extends ChartFrameProps {
|
|
10
|
+
readonly series: readonly ChartSeries[];
|
|
11
|
+
readonly categories?: readonly string[];
|
|
12
|
+
}
|
|
13
|
+
/** Overlaid multi-series smooth lines with vertex dots revealed as they trace. */
|
|
14
|
+
export declare function GroupedLineChart({ series, categories, ...frame }: GroupedLineChartProps): import("react").JSX.Element;
|
|
15
|
+
export interface StackedLineChartProps extends ChartFrameProps {
|
|
16
|
+
readonly series: readonly ChartSeries[];
|
|
17
|
+
readonly categories?: readonly string[];
|
|
18
|
+
}
|
|
19
|
+
/** Stacked filled areas that grow up from the baseline. */
|
|
20
|
+
export declare function StackedLineChart({ series, categories, ...frame }: StackedLineChartProps): import("react").JSX.Element;
|