@sctg/openclaw-coach 0.1.0 → 2.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/README.md +323 -66
- package/assets/fonts/Inter_400Regular.ttf +0 -0
- package/assets/fonts/Inter_600SemiBold.ttf +0 -0
- package/assets/fonts/LICENSE.txt +93 -0
- package/assets/icon.png +0 -0
- package/assets/logo.png +0 -0
- package/assets/logo.svg +62 -0
- package/dist/charts/build.d.ts +71 -0
- package/dist/charts/build.js +276 -0
- package/dist/charts/labels.d.ts +38 -0
- package/dist/charts/labels.js +73 -0
- package/dist/charts/render.d.ts +10 -0
- package/dist/charts/render.js +26 -0
- package/dist/charts/svg.d.ts +47 -0
- package/dist/charts/svg.js +92 -0
- package/dist/config.d.ts +25 -0
- package/dist/config.js +38 -0
- package/dist/dates.d.ts +22 -0
- package/dist/dates.js +71 -0
- package/dist/email.d.ts +58 -0
- package/dist/email.js +188 -0
- package/dist/garmin/gc.d.ts +25 -0
- package/dist/garmin/gc.js +81 -0
- package/dist/garmin/normalize.d.ts +20 -0
- package/dist/garmin/normalize.js +132 -0
- package/dist/index.js +207 -163
- package/dist/profile.d.ts +4 -0
- package/dist/profile.js +43 -0
- package/dist/science/associations.d.ts +56 -0
- package/dist/science/associations.js +102 -0
- package/dist/science/healthspan.d.ts +25 -0
- package/dist/science/healthspan.js +89 -0
- package/dist/science/hrv.d.ts +61 -0
- package/dist/science/hrv.js +119 -0
- package/dist/science/load.d.ts +107 -0
- package/dist/science/load.js +228 -0
- package/dist/science/performance.d.ts +93 -0
- package/dist/science/performance.js +191 -0
- package/dist/science/readiness.d.ts +47 -0
- package/dist/science/readiness.js +97 -0
- package/dist/science/recovery.d.ts +74 -0
- package/dist/science/recovery.js +173 -0
- package/dist/science/sleep.d.ts +79 -0
- package/dist/science/sleep.js +165 -0
- package/dist/science/stats.d.ts +50 -0
- package/dist/science/stats.js +201 -0
- package/dist/science/strength.d.ts +47 -0
- package/dist/science/strength.js +120 -0
- package/dist/store.d.ts +36 -0
- package/dist/store.js +78 -0
- package/dist/sync.d.ts +36 -0
- package/dist/sync.js +145 -0
- package/dist/tools.d.ts +463 -0
- package/dist/tools.js +792 -0
- package/dist/types.d.ts +143 -0
- package/dist/types.js +2 -0
- package/docs/SCIENCE.md +721 -0
- package/examples/ATHLETE-fr.md +72 -0
- package/examples/ATHLETE.md +74 -0
- package/examples/PLAN-fr.md +55 -0
- package/examples/PLAN.md +55 -0
- package/openclaw.plugin.json +158 -5
- package/package.json +14 -3
- package/skills/coach/SKILL.md +291 -208
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Chart builders: pure functions from already computed model results to SVG.
|
|
3
|
+
* Nothing here computes physiology; every number drawn comes from the models.
|
|
4
|
+
* Logical sizes are in CSS pixels; the PNG is rendered at twice that for sharp screens.
|
|
5
|
+
*/
|
|
6
|
+
import { type Lang } from "./labels.ts";
|
|
7
|
+
import { FONT, esc, type Theme } from "./svg.ts";
|
|
8
|
+
export type Chart = {
|
|
9
|
+
svg: string;
|
|
10
|
+
width: number;
|
|
11
|
+
height: number;
|
|
12
|
+
alt: string;
|
|
13
|
+
};
|
|
14
|
+
export type SignalRow = {
|
|
15
|
+
value: number | null;
|
|
16
|
+
baseline: number | null;
|
|
17
|
+
delta: number | null;
|
|
18
|
+
z: number | null;
|
|
19
|
+
};
|
|
20
|
+
export type SignalRows = Record<"hrv" | "restingHr" | "respiration" | "skinTemp" | "spo2", SignalRow>;
|
|
21
|
+
export type HrvPoint = {
|
|
22
|
+
date: string;
|
|
23
|
+
ms: number | null;
|
|
24
|
+
trendMs: number | null;
|
|
25
|
+
};
|
|
26
|
+
export type LoadPoint = {
|
|
27
|
+
date: string;
|
|
28
|
+
ctl: number;
|
|
29
|
+
atl: number;
|
|
30
|
+
tsb: number;
|
|
31
|
+
};
|
|
32
|
+
export type SleepPoint = {
|
|
33
|
+
date: string;
|
|
34
|
+
hours: number | null;
|
|
35
|
+
deepH: number | null;
|
|
36
|
+
lightH: number | null;
|
|
37
|
+
remH: number | null;
|
|
38
|
+
needH: number;
|
|
39
|
+
};
|
|
40
|
+
type Common = {
|
|
41
|
+
theme: Theme;
|
|
42
|
+
lang: Lang;
|
|
43
|
+
};
|
|
44
|
+
export declare function recoveryGauge(input: Common & {
|
|
45
|
+
score: number | null;
|
|
46
|
+
band: "low" | "moderate" | "high" | null;
|
|
47
|
+
}): Chart;
|
|
48
|
+
export declare function ceilingLadder(input: Common & {
|
|
49
|
+
ceiling: "rest" | "easy" | "moderate" | "hard";
|
|
50
|
+
suggestedLoad: {
|
|
51
|
+
min: number;
|
|
52
|
+
max: number;
|
|
53
|
+
} | null;
|
|
54
|
+
}): Chart;
|
|
55
|
+
export declare function signalsChart(input: Common & {
|
|
56
|
+
rows: SignalRows;
|
|
57
|
+
}): Chart;
|
|
58
|
+
export declare function hrvChart(input: Common & {
|
|
59
|
+
points: HrvPoint[];
|
|
60
|
+
range: {
|
|
61
|
+
lowMs: number;
|
|
62
|
+
highMs: number;
|
|
63
|
+
} | null;
|
|
64
|
+
}): Chart;
|
|
65
|
+
export declare function loadChart(input: Common & {
|
|
66
|
+
points: LoadPoint[];
|
|
67
|
+
}): Chart;
|
|
68
|
+
export declare function sleepChart(input: Common & {
|
|
69
|
+
nights: SleepPoint[];
|
|
70
|
+
}): Chart;
|
|
71
|
+
export { FONT, esc };
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Chart builders: pure functions from already computed model results to SVG.
|
|
3
|
+
* Nothing here computes physiology; every number drawn comes from the models.
|
|
4
|
+
* Logical sizes are in CSS pixels; the PNG is rendered at twice that for sharp screens.
|
|
5
|
+
*/
|
|
6
|
+
import { LABELS, shortDate } from "./labels.js";
|
|
7
|
+
import { FONT, esc, gaugeArc, linePath, n, niceTicks, polar, scale, svgDocument, text } from "./svg.js";
|
|
8
|
+
// --- Recovery gauge ---------------------------------------------------------------------------------
|
|
9
|
+
export function recoveryGauge(input) {
|
|
10
|
+
const { theme, lang, score, band } = input;
|
|
11
|
+
const t = LABELS[lang];
|
|
12
|
+
const width = 280;
|
|
13
|
+
const height = 210;
|
|
14
|
+
const cx = 140;
|
|
15
|
+
const cy = 146;
|
|
16
|
+
const radius = 92;
|
|
17
|
+
const stroke = 20;
|
|
18
|
+
const bands = [
|
|
19
|
+
[0, 33, theme.bad],
|
|
20
|
+
[34, 66, theme.warn],
|
|
21
|
+
[67, 100, theme.good],
|
|
22
|
+
];
|
|
23
|
+
let body = text(cx, 22, t.recovery, { size: 15, weight: 600, anchor: "middle", fill: theme.text });
|
|
24
|
+
for (const [from, to, color] of bands) {
|
|
25
|
+
body += `<path d="${gaugeArc(cx, cy, radius, from + 0.6, to - 0.6)}" fill="none" stroke="${score === null ? theme.track : color}" stroke-width="${stroke}" stroke-opacity="${score === null ? 1 : 0.28}"/>`;
|
|
26
|
+
}
|
|
27
|
+
if (score !== null && band !== null) {
|
|
28
|
+
const color = band === "high" ? theme.good : band === "moderate" ? theme.warn : theme.bad;
|
|
29
|
+
body += `<path d="${gaugeArc(cx, cy, radius, 0.6, Math.max(score, 1))}" fill="none" stroke="${color}" stroke-width="${stroke}" stroke-linecap="round"/>`;
|
|
30
|
+
const tip = polar(cx, cy, radius, Math.PI * (1 - Math.max(score, 1) / 100));
|
|
31
|
+
body += `<circle cx="${n(tip.x)}" cy="${n(tip.y)}" r="${stroke / 2 + 3}" fill="${theme.background}"/><circle cx="${n(tip.x)}" cy="${n(tip.y)}" r="${stroke / 2 - 3}" fill="${color}"/>`;
|
|
32
|
+
body += `<text x="${cx}" y="${cy - 8}" font-family="${FONT}" text-anchor="middle" fill="${theme.text}"><tspan font-size="46" font-weight="600">${score}</tspan><tspan font-size="13" fill="${theme.muted}" dx="4">${esc(t.of100)}</tspan></text>`;
|
|
33
|
+
body += text(cx, cy + 28, t.bands[band], { size: 15, weight: 600, anchor: "middle", fill: color });
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
body += text(cx, cy - 4, "n/a", { size: 38, weight: 600, anchor: "middle", fill: theme.muted });
|
|
37
|
+
body += text(cx, cy + 28, t.noScore, { size: 13, anchor: "middle", fill: theme.muted });
|
|
38
|
+
}
|
|
39
|
+
body += text(cx - radius, cy + 20, "0", { size: 11, anchor: "middle", fill: theme.muted });
|
|
40
|
+
body += text(cx + radius, cy + 20, "100", { size: 11, anchor: "middle", fill: theme.muted });
|
|
41
|
+
return { svg: svgDocument(width, height, theme, t.recovery, t.altGauge(score, band ? t.bands[band] : null), body), width, height, alt: t.altGauge(score, band ? t.bands[band] : null) };
|
|
42
|
+
}
|
|
43
|
+
// --- Ceiling ladder ---------------------------------------------------------------------------------
|
|
44
|
+
export function ceilingLadder(input) {
|
|
45
|
+
const { theme, lang, ceiling, suggestedLoad } = input;
|
|
46
|
+
const t = LABELS[lang];
|
|
47
|
+
const width = 280;
|
|
48
|
+
const height = 210;
|
|
49
|
+
const order = ["rest", "easy", "moderate", "hard"];
|
|
50
|
+
const level = order.indexOf(ceiling);
|
|
51
|
+
const color = theme.ceiling[ceiling];
|
|
52
|
+
let body = text(width / 2, 28, t.ceilingTitle, { size: 15, weight: 600, anchor: "middle", fill: theme.text });
|
|
53
|
+
const barWidth = 44;
|
|
54
|
+
const gap = 14;
|
|
55
|
+
const left = (width - (4 * barWidth + 3 * gap)) / 2;
|
|
56
|
+
const base = 156;
|
|
57
|
+
order.forEach((name, index) => {
|
|
58
|
+
const barHeight = 26 + index * 15;
|
|
59
|
+
const x = left + index * (barWidth + gap);
|
|
60
|
+
const active = index <= level;
|
|
61
|
+
body += `<rect x="${n(x)}" y="${n(base - barHeight)}" width="${barWidth}" height="${barHeight}" rx="6" fill="${active ? color : theme.track}" fill-opacity="${active ? (index === level ? 1 : 0.45) : 1}"/>`;
|
|
62
|
+
body += text(x + barWidth / 2, base + 16, t.ceilings[name], { size: 11, weight: index === level ? 600 : 400, anchor: "middle", fill: index === level ? theme.text : theme.muted });
|
|
63
|
+
});
|
|
64
|
+
body += text(width / 2, 74, t.ceilings[ceiling], { size: 30, weight: 600, anchor: "middle", fill: color });
|
|
65
|
+
if (suggestedLoad) {
|
|
66
|
+
body += text(width / 2, 196, `${t.suggestedLoad}: ${Math.round(suggestedLoad.min)}–${Math.round(suggestedLoad.max)}`, { size: 12, anchor: "middle", fill: theme.muted });
|
|
67
|
+
}
|
|
68
|
+
const alt = t.altCeiling(t.ceilings[ceiling]);
|
|
69
|
+
return { svg: svgDocument(width, height, theme, t.ceilingTitle, alt, body), width, height, alt };
|
|
70
|
+
}
|
|
71
|
+
// --- Overnight signals against the personal baseline -----------------------------------------------
|
|
72
|
+
const SIGNAL_ORDER = ["hrv", "restingHr", "respiration", "skinTemp", "spo2"];
|
|
73
|
+
/** Direction in which a deviation is unfavourable. */
|
|
74
|
+
const BAD_SIDE = { hrv: -1, restingHr: 1, respiration: 1, skinTemp: 1, spo2: -1 };
|
|
75
|
+
const UNITS = { hrv: "ms", restingHr: "bpm", respiration: "/min", skinTemp: "°C", spo2: "%" };
|
|
76
|
+
function formatSigned(value, digits = 1) {
|
|
77
|
+
const text = value.toFixed(digits).replace(/^-/, "−");
|
|
78
|
+
return value > 0 ? `+${text}` : text;
|
|
79
|
+
}
|
|
80
|
+
export function signalsChart(input) {
|
|
81
|
+
const { theme, lang, rows } = input;
|
|
82
|
+
const t = LABELS[lang];
|
|
83
|
+
const width = 600;
|
|
84
|
+
const rowHeight = 34;
|
|
85
|
+
const top = 62;
|
|
86
|
+
const height = top + SIGNAL_ORDER.length * rowHeight + 14;
|
|
87
|
+
const trackLeft = 118;
|
|
88
|
+
const trackRight = 440;
|
|
89
|
+
const x = scale(-3, 3, trackLeft, trackRight);
|
|
90
|
+
let body = text(20, 28, t.signalsTitle, { size: 15, weight: 600, fill: theme.text });
|
|
91
|
+
body += text(20, 46, t.signalsSubtitle, { size: 11, fill: theme.muted });
|
|
92
|
+
const described = [];
|
|
93
|
+
SIGNAL_ORDER.forEach((key, index) => {
|
|
94
|
+
const row = rows[key];
|
|
95
|
+
const cy = top + index * rowHeight + rowHeight / 2;
|
|
96
|
+
body += text(20, cy, t.signals[key], { size: 13, weight: 600, fill: theme.text, baseline: "middle" });
|
|
97
|
+
body += `<rect x="${trackLeft}" y="${cy - 6}" width="${trackRight - trackLeft}" height="12" rx="6" fill="${theme.track}"/>`;
|
|
98
|
+
body += `<rect x="${n(x(-2))}" y="${cy - 6}" width="${n(x(2) - x(-2))}" height="12" fill="${theme.grid}"/>`;
|
|
99
|
+
body += `<rect x="${n(x(-1))}" y="${cy - 6}" width="${n(x(1) - x(-1))}" height="12" fill="${theme.accent}" fill-opacity="0.22"/>`;
|
|
100
|
+
body += `<line x1="${n(x(0))}" y1="${cy - 9}" x2="${n(x(0))}" y2="${cy + 9}" stroke="${theme.muted}" stroke-width="1"/>`;
|
|
101
|
+
if (row.z === null || row.value === null) {
|
|
102
|
+
body += text(trackRight + 16, cy, t.noData, { size: 12, fill: theme.muted, baseline: "middle" });
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
const z = Math.max(-3, Math.min(3, row.z));
|
|
106
|
+
const unfavourable = row.z * BAD_SIDE[key] > 0 ? Math.abs(row.z) : 0;
|
|
107
|
+
const color = unfavourable >= 2 ? theme.bad : unfavourable >= 1 ? theme.warn : theme.good;
|
|
108
|
+
body += `<circle cx="${n(x(z))}" cy="${cy}" r="8" fill="${theme.background}"/><circle cx="${n(x(z))}" cy="${cy}" r="6" fill="${color}"/>`;
|
|
109
|
+
const digits = key === "skinTemp" || key === "respiration" ? 1 : 0;
|
|
110
|
+
const delta = row.delta === null ? "" : key === "hrv" ? ` (${formatSigned(row.delta, 0)} %)` : ` (${formatSigned(row.delta, digits === 0 ? 0 : 1)})`;
|
|
111
|
+
const shown = key === "skinTemp" ? formatSigned(row.value, 1) : row.value.toFixed(digits);
|
|
112
|
+
body += text(trackRight + 16, cy, `${shown} ${UNITS[key]}${delta}`, { size: 12, fill: theme.text, baseline: "middle" });
|
|
113
|
+
described.push(`${t.signals[key]} ${shown} ${UNITS[key]}`);
|
|
114
|
+
});
|
|
115
|
+
const alt = `${t.signalsTitle}: ${described.join(", ") || t.noData}.`;
|
|
116
|
+
return { svg: svgDocument(width, height, theme, t.signalsTitle, alt, body), width, height, alt };
|
|
117
|
+
}
|
|
118
|
+
// --- HRV trend ------------------------------------------------------------------------------------
|
|
119
|
+
export function hrvChart(input) {
|
|
120
|
+
const { theme, lang, points, range } = input;
|
|
121
|
+
const t = LABELS[lang];
|
|
122
|
+
const width = 600;
|
|
123
|
+
const height = 260;
|
|
124
|
+
const plot = { left: 48, right: 566, top: 64, bottom: 212 };
|
|
125
|
+
const values = points.flatMap((p) => [p.ms, p.trendMs]).filter((v) => v !== null);
|
|
126
|
+
if (range)
|
|
127
|
+
values.push(range.lowMs, range.highMs);
|
|
128
|
+
const lo = Math.min(...values);
|
|
129
|
+
const hi = Math.max(...values);
|
|
130
|
+
const pad = (hi - lo) * 0.12 || 5;
|
|
131
|
+
const y = scale(lo - pad, hi + pad, plot.bottom, plot.top);
|
|
132
|
+
const x = scale(0, Math.max(points.length - 1, 1), plot.left, plot.right);
|
|
133
|
+
let body = text(20, 28, t.hrvTitle, { size: 15, weight: 600, fill: theme.text });
|
|
134
|
+
body += text(20, 46, t.hrvSubtitle, { size: 11, fill: theme.muted });
|
|
135
|
+
for (const tick of niceTicks(lo - pad, hi + pad, 4)) {
|
|
136
|
+
body += `<line x1="${plot.left}" y1="${n(y(tick))}" x2="${plot.right}" y2="${n(y(tick))}" stroke="${theme.grid}" stroke-width="1"/>`;
|
|
137
|
+
body += text(plot.left - 8, y(tick), String(Math.round(tick)), { size: 11, anchor: "end", fill: theme.muted, baseline: "middle" });
|
|
138
|
+
}
|
|
139
|
+
if (range) {
|
|
140
|
+
body += `<rect x="${plot.left}" y="${n(y(range.highMs))}" width="${plot.right - plot.left}" height="${n(y(range.lowMs) - y(range.highMs))}" fill="${theme.good}" fill-opacity="0.16"/>`;
|
|
141
|
+
}
|
|
142
|
+
points.forEach((p, index) => {
|
|
143
|
+
if (p.ms !== null)
|
|
144
|
+
body += `<circle cx="${n(x(index))}" cy="${n(y(p.ms))}" r="3" fill="${theme.muted}" fill-opacity="0.7"/>`;
|
|
145
|
+
});
|
|
146
|
+
body += `<path d="${linePath(points.map((p, index) => ({ x: x(index), y: p.trendMs === null ? null : y(p.trendMs) })))}" fill="none" stroke="${theme.accent}" stroke-width="2.5" stroke-linejoin="round" stroke-linecap="round"/>`;
|
|
147
|
+
const last = [...points].reverse().findIndex((p) => p.trendMs !== null);
|
|
148
|
+
if (last !== -1) {
|
|
149
|
+
const index = points.length - 1 - last;
|
|
150
|
+
const p = points[index];
|
|
151
|
+
body += `<circle cx="${n(x(index))}" cy="${n(y(p.trendMs))}" r="5" fill="${theme.accent}"/>`;
|
|
152
|
+
body += text(Math.min(x(index), plot.right - 4), y(p.trendMs) - 12, `${Math.round(p.trendMs)} ms`, { size: 12, weight: 600, anchor: "end", fill: theme.accent, halo: theme.background });
|
|
153
|
+
}
|
|
154
|
+
const labelEvery = Math.max(1, Math.round(points.length / 5));
|
|
155
|
+
points.forEach((p, index) => {
|
|
156
|
+
if ((points.length - 1 - index) % labelEvery === 0)
|
|
157
|
+
body += text(x(index), plot.bottom + 18, shortDate(p.date, lang), { size: 11, anchor: "middle", fill: theme.muted });
|
|
158
|
+
});
|
|
159
|
+
// Legend.
|
|
160
|
+
const ly = height - 14;
|
|
161
|
+
body += `<circle cx="22" cy="${ly - 4}" r="3" fill="${theme.muted}" fill-opacity="0.7"/>` + text(31, ly, t.nightly, { size: 11, fill: theme.muted });
|
|
162
|
+
body += `<line x1="92" y1="${ly - 4}" x2="112" y2="${ly - 4}" stroke="${theme.accent}" stroke-width="2.5"/>` + text(119, ly, t.trend7, { size: 11, fill: theme.muted });
|
|
163
|
+
if (range)
|
|
164
|
+
body += `<rect x="212" y="${ly - 10}" width="18" height="10" fill="${theme.good}" fill-opacity="0.3"/>` + text(237, ly, `${t.normalRange} ${Math.round(range.lowMs)}–${Math.round(range.highMs)} ms`, { size: 11, fill: theme.muted });
|
|
165
|
+
const lastTrend = [...points].reverse().find((p) => p.trendMs !== null)?.trendMs;
|
|
166
|
+
const alt = `${t.hrvTitle}: ${lastTrend == null ? t.noData : `${Math.round(lastTrend)} ms (7 d)`}${range ? `, ${t.normalRange.toLowerCase()} ${Math.round(range.lowMs)}–${Math.round(range.highMs)} ms` : ""}.`;
|
|
167
|
+
return { svg: svgDocument(width, height, theme, t.hrvTitle, alt, body), width, height, alt };
|
|
168
|
+
}
|
|
169
|
+
// --- Fitness, fatigue, form ----------------------------------------------------------------------------
|
|
170
|
+
export function loadChart(input) {
|
|
171
|
+
const { theme, lang, points } = input;
|
|
172
|
+
const t = LABELS[lang];
|
|
173
|
+
const width = 600;
|
|
174
|
+
const height = 350;
|
|
175
|
+
const left = 48;
|
|
176
|
+
const right = 566;
|
|
177
|
+
const top = { top: 64, bottom: 198 };
|
|
178
|
+
const bottom = { top: 232, bottom: 292 };
|
|
179
|
+
const x = scale(0, Math.max(points.length - 1, 1), left, right);
|
|
180
|
+
const loadMax = Math.max(...points.flatMap((p) => [p.ctl, p.atl]), 10);
|
|
181
|
+
const yLoad = scale(0, loadMax * 1.1, top.bottom, top.top);
|
|
182
|
+
const tsbAbs = Math.max(...points.map((p) => Math.abs(p.tsb)), 10);
|
|
183
|
+
const yTsb = scale(-tsbAbs, tsbAbs, bottom.bottom, bottom.top);
|
|
184
|
+
let body = text(20, 28, t.loadTitle, { size: 15, weight: 600, fill: theme.text });
|
|
185
|
+
body += text(20, 46, "TRIMP", { size: 11, fill: theme.muted });
|
|
186
|
+
for (const tick of niceTicks(0, loadMax * 1.1, 4)) {
|
|
187
|
+
body += `<line x1="${left}" y1="${n(yLoad(tick))}" x2="${right}" y2="${n(yLoad(tick))}" stroke="${theme.grid}" stroke-width="1"/>`;
|
|
188
|
+
body += text(left - 8, yLoad(tick), String(Math.round(tick)), { size: 11, anchor: "end", fill: theme.muted, baseline: "middle" });
|
|
189
|
+
}
|
|
190
|
+
const area = `${linePath(points.map((p, i) => ({ x: x(i), y: yLoad(p.ctl) })))}L${n(right)} ${n(top.bottom)}L${n(left)} ${n(top.bottom)}Z`;
|
|
191
|
+
body += `<path d="${area}" fill="${theme.accent}" fill-opacity="0.12"/>`;
|
|
192
|
+
body += `<path d="${linePath(points.map((p, i) => ({ x: x(i), y: yLoad(p.ctl) })))}" fill="none" stroke="${theme.accent}" stroke-width="2.5" stroke-linejoin="round"/>`;
|
|
193
|
+
body += `<path d="${linePath(points.map((p, i) => ({ x: x(i), y: yLoad(p.atl) })))}" fill="none" stroke="${theme.accent2}" stroke-width="2" stroke-linejoin="round"/>`;
|
|
194
|
+
// Form: bars around zero, favourable above.
|
|
195
|
+
body += text(20, bottom.top - 8, t.form, { size: 11, fill: theme.muted });
|
|
196
|
+
body += `<line x1="${left}" y1="${n(yTsb(0))}" x2="${right}" y2="${n(yTsb(0))}" stroke="${theme.muted}" stroke-width="1"/>`;
|
|
197
|
+
const barWidth = Math.max(1.5, ((right - left) / points.length) * 0.7);
|
|
198
|
+
points.forEach((p, i) => {
|
|
199
|
+
const y0 = yTsb(0);
|
|
200
|
+
const y1 = yTsb(p.tsb);
|
|
201
|
+
body += `<rect x="${n(x(i) - barWidth / 2)}" y="${n(Math.min(y0, y1))}" width="${n(barWidth)}" height="${n(Math.abs(y1 - y0))}" fill="${p.tsb >= 0 ? theme.good : theme.warn}" fill-opacity="0.8"/>`;
|
|
202
|
+
});
|
|
203
|
+
body += text(left - 8, yTsb(tsbAbs), `+${Math.round(tsbAbs)}`, { size: 10, anchor: "end", fill: theme.muted, baseline: "middle" });
|
|
204
|
+
body += text(left - 8, yTsb(-tsbAbs), `−${Math.round(tsbAbs)}`, { size: 10, anchor: "end", fill: theme.muted, baseline: "middle" });
|
|
205
|
+
const labelEvery = Math.max(1, Math.round(points.length / 6));
|
|
206
|
+
points.forEach((p, i) => {
|
|
207
|
+
if ((points.length - 1 - i) % labelEvery === 0)
|
|
208
|
+
body += text(x(i), bottom.bottom + 16, shortDate(p.date, lang), { size: 11, anchor: "middle", fill: theme.muted });
|
|
209
|
+
});
|
|
210
|
+
const lastPoint = points[points.length - 1];
|
|
211
|
+
body += `<circle cx="${n(x(points.length - 1))}" cy="${n(yLoad(lastPoint.ctl))}" r="4" fill="${theme.accent}"/><circle cx="${n(x(points.length - 1))}" cy="${n(yLoad(lastPoint.atl))}" r="4" fill="${theme.accent2}"/>`;
|
|
212
|
+
const ly = height - 12;
|
|
213
|
+
body += `<line x1="22" y1="${ly - 4}" x2="42" y2="${ly - 4}" stroke="${theme.accent}" stroke-width="2.5"/>` + text(49, ly, `${t.fitness} ${lastPoint.ctl.toFixed(0)}`, { size: 11, fill: theme.muted });
|
|
214
|
+
body += `<line x1="190" y1="${ly - 4}" x2="210" y2="${ly - 4}" stroke="${theme.accent2}" stroke-width="2"/>` + text(217, ly, `${t.fatigue} ${lastPoint.atl.toFixed(0)}`, { size: 11, fill: theme.muted });
|
|
215
|
+
body += `<rect x="358" y="${ly - 10}" width="12" height="10" fill="${lastPoint.tsb >= 0 ? theme.good : theme.warn}"/>` + text(377, ly, `${t.form} ${lastPoint.tsb >= 0 ? "+" : "−"}${Math.abs(lastPoint.tsb).toFixed(0)}`, { size: 11, fill: theme.muted });
|
|
216
|
+
const alt = `${t.loadTitle}: ${t.fitness} ${lastPoint.ctl.toFixed(0)}, ${t.fatigue} ${lastPoint.atl.toFixed(0)}, ${t.form} ${lastPoint.tsb.toFixed(0)}.`;
|
|
217
|
+
return { svg: svgDocument(width, height, theme, t.loadTitle, alt, body), width, height, alt };
|
|
218
|
+
}
|
|
219
|
+
// --- Sleep ----------------------------------------------------------------------------------------
|
|
220
|
+
export function sleepChart(input) {
|
|
221
|
+
const { theme, lang, nights } = input;
|
|
222
|
+
const t = LABELS[lang];
|
|
223
|
+
const width = 600;
|
|
224
|
+
const height = 260;
|
|
225
|
+
const plot = { left: 48, right: 580, top: 56, bottom: 200 };
|
|
226
|
+
const need = nights.length ? nights[nights.length - 1].needH : 8;
|
|
227
|
+
const maxHours = Math.max(...nights.map((night) => night.hours ?? 0), need, 6) * 1.1;
|
|
228
|
+
const y = scale(0, maxHours, plot.bottom, plot.top);
|
|
229
|
+
const slot = (plot.right - plot.left) / Math.max(nights.length, 1);
|
|
230
|
+
const barWidth = Math.min(slot * 0.62, 30);
|
|
231
|
+
let body = text(20, 28, t.sleepTitle, { size: 15, weight: 600, fill: theme.text });
|
|
232
|
+
const stagesKnown = nights.some((night) => night.deepH !== null);
|
|
233
|
+
for (let tick = 0; tick <= maxHours; tick += 2) {
|
|
234
|
+
body += `<line x1="${plot.left}" y1="${n(y(tick))}" x2="${plot.right}" y2="${n(y(tick))}" stroke="${theme.grid}" stroke-width="1"/>`;
|
|
235
|
+
body += text(plot.left - 8, y(tick), `${Math.round(tick)}`, { size: 11, anchor: "end", fill: theme.muted, baseline: "middle" });
|
|
236
|
+
}
|
|
237
|
+
nights.forEach((night, index) => {
|
|
238
|
+
const cx = plot.left + slot * (index + 0.5);
|
|
239
|
+
if (night.hours === null)
|
|
240
|
+
return;
|
|
241
|
+
const segments = stagesKnown && night.deepH !== null && night.lightH !== null && night.remH !== null
|
|
242
|
+
? [[night.deepH, theme.accent], [night.remH, theme.good], [night.lightH, theme.accent2]]
|
|
243
|
+
: [[night.hours, theme.accent]];
|
|
244
|
+
let acc = 0;
|
|
245
|
+
// Stages stack from the bottom: deep, REM, light.
|
|
246
|
+
for (const [hours, color] of segments) {
|
|
247
|
+
if (hours === null)
|
|
248
|
+
continue;
|
|
249
|
+
const y1 = y(acc + hours);
|
|
250
|
+
const y0 = y(acc);
|
|
251
|
+
body += `<rect x="${n(cx - barWidth / 2)}" y="${n(y1)}" width="${n(barWidth)}" height="${n(Math.max(y0 - y1, 0))}" fill="${color}" fill-opacity="${night.hours < night.needH * 0.75 ? 0.55 : 0.9}"/>`;
|
|
252
|
+
acc += hours;
|
|
253
|
+
}
|
|
254
|
+
body += text(cx, y(night.hours) - 6, night.hours.toFixed(1), { size: 10, weight: 600, anchor: "middle", fill: theme.text, halo: theme.background });
|
|
255
|
+
const weekday = new Date(`${night.date}T12:00:00Z`).getUTCDay();
|
|
256
|
+
body += text(cx, plot.bottom + 16, t.weekdays[weekday], { size: 11, anchor: "middle", fill: theme.muted });
|
|
257
|
+
if (index === 0 || index === nights.length - 1)
|
|
258
|
+
body += text(cx, plot.bottom + 30, shortDate(night.date, lang), { size: 10, anchor: "middle", fill: theme.muted });
|
|
259
|
+
});
|
|
260
|
+
body += `<line x1="${plot.left}" y1="${n(y(need))}" x2="${plot.right}" y2="${n(y(need))}" stroke="${theme.text}" stroke-width="1.2" stroke-dasharray="5 4"/>`;
|
|
261
|
+
const ly = height - 12;
|
|
262
|
+
body += `<line x1="370" y1="${ly - 4}" x2="392" y2="${ly - 4}" stroke="${theme.text}" stroke-width="1.2" stroke-dasharray="5 4"/>` + text(399, ly, `${t.need} ${need.toFixed(1)} ${t.hours}`, { size: 11, fill: theme.muted });
|
|
263
|
+
if (stagesKnown) {
|
|
264
|
+
body += `<rect x="22" y="${ly - 9}" width="10" height="10" rx="2" fill="${theme.accent}"/>` + text(37, ly, t.deep, { size: 11, fill: theme.muted });
|
|
265
|
+
body += `<rect x="92" y="${ly - 9}" width="10" height="10" rx="2" fill="${theme.good}"/>` + text(107, ly, t.rem, { size: 11, fill: theme.muted });
|
|
266
|
+
body += `<rect x="172" y="${ly - 9}" width="10" height="10" rx="2" fill="${theme.accent2}"/>` + text(187, ly, t.light, { size: 11, fill: theme.muted });
|
|
267
|
+
}
|
|
268
|
+
else {
|
|
269
|
+
body += `<rect x="22" y="${ly - 9}" width="10" height="10" rx="2" fill="${theme.accent}"/>` + text(37, ly, t.unknownStages, { size: 11, fill: theme.muted });
|
|
270
|
+
}
|
|
271
|
+
const avg = nights.filter((night) => night.hours !== null);
|
|
272
|
+
const mean = avg.length ? avg.reduce((total, night) => total + night.hours, 0) / avg.length : null;
|
|
273
|
+
const alt = `${t.sleepTitle}: ${avg.length} ${lang === "fr" ? "nuits" : "nights"}${mean === null ? "" : `, ${mean.toFixed(1)} ${t.hours}`} ${lang === "fr" ? "en moyenne" : "on average"}, ${t.need.toLowerCase()} ${need.toFixed(1)} ${t.hours}.`;
|
|
274
|
+
return { svg: svgDocument(width, height, theme, t.sleepTitle, alt, body), width, height, alt };
|
|
275
|
+
}
|
|
276
|
+
export { FONT, esc };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export type Lang = "en" | "fr";
|
|
2
|
+
type Strings = {
|
|
3
|
+
recovery: string;
|
|
4
|
+
of100: string;
|
|
5
|
+
bands: Record<"low" | "moderate" | "high", string>;
|
|
6
|
+
noScore: string;
|
|
7
|
+
ceilingTitle: string;
|
|
8
|
+
ceilings: Record<"rest" | "easy" | "moderate" | "hard", string>;
|
|
9
|
+
suggestedLoad: string;
|
|
10
|
+
signalsTitle: string;
|
|
11
|
+
signalsSubtitle: string;
|
|
12
|
+
signals: Record<"hrv" | "restingHr" | "respiration" | "skinTemp" | "spo2", string>;
|
|
13
|
+
noData: string;
|
|
14
|
+
hrvTitle: string;
|
|
15
|
+
hrvSubtitle: string;
|
|
16
|
+
nightly: string;
|
|
17
|
+
trend7: string;
|
|
18
|
+
normalRange: string;
|
|
19
|
+
loadTitle: string;
|
|
20
|
+
fitness: string;
|
|
21
|
+
fatigue: string;
|
|
22
|
+
form: string;
|
|
23
|
+
sleepTitle: string;
|
|
24
|
+
need: string;
|
|
25
|
+
deep: string;
|
|
26
|
+
light: string;
|
|
27
|
+
rem: string;
|
|
28
|
+
unknownStages: string;
|
|
29
|
+
hours: string;
|
|
30
|
+
months: string[];
|
|
31
|
+
weekdays: string[];
|
|
32
|
+
/** Alt texts. */
|
|
33
|
+
altGauge: (score: number | null, band: string | null) => string;
|
|
34
|
+
altCeiling: (ceiling: string) => string;
|
|
35
|
+
};
|
|
36
|
+
export declare const LABELS: Record<Lang, Strings>;
|
|
37
|
+
export declare function shortDate(date: string, lang: Lang): string;
|
|
38
|
+
export {};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
export const LABELS = {
|
|
2
|
+
en: {
|
|
3
|
+
recovery: "Recovery",
|
|
4
|
+
of100: "/100",
|
|
5
|
+
bands: { low: "Low", moderate: "Moderate", high: "High" },
|
|
6
|
+
noScore: "Not enough data",
|
|
7
|
+
ceilingTitle: "Today's ceiling",
|
|
8
|
+
ceilings: { rest: "Rest", easy: "Easy", moderate: "Moderate", hard: "Hard" },
|
|
9
|
+
suggestedLoad: "Suggested load",
|
|
10
|
+
signalsTitle: "Overnight signals vs your normal",
|
|
11
|
+
signalsSubtitle: "Dot = last night; blue band = usual range (±1 SD), grey = ±2 SD",
|
|
12
|
+
signals: { hrv: "HRV", restingHr: "Resting HR", respiration: "Respiration", skinTemp: "Skin temp.", spo2: "SpO2" },
|
|
13
|
+
noData: "no data",
|
|
14
|
+
hrvTitle: "Heart rate variability",
|
|
15
|
+
hrvSubtitle: "Overnight rMSSD (ms), 7-day trend and personal normal range",
|
|
16
|
+
nightly: "Night",
|
|
17
|
+
trend7: "7-day mean",
|
|
18
|
+
normalRange: "Normal range",
|
|
19
|
+
loadTitle: "Fitness, fatigue and form",
|
|
20
|
+
fitness: "Fitness (CTL)",
|
|
21
|
+
fatigue: "Fatigue (ATL)",
|
|
22
|
+
form: "Form (TSB)",
|
|
23
|
+
sleepTitle: "Sleep",
|
|
24
|
+
need: "Need",
|
|
25
|
+
deep: "Deep",
|
|
26
|
+
light: "Light",
|
|
27
|
+
rem: "REM",
|
|
28
|
+
unknownStages: "Total",
|
|
29
|
+
hours: "h",
|
|
30
|
+
months: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
|
|
31
|
+
weekdays: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"],
|
|
32
|
+
altGauge: (score, band) => (score === null ? "Recovery index: not enough data." : `Recovery index ${score} out of 100 (${band}).`),
|
|
33
|
+
altCeiling: (ceiling) => `Training ceiling for today: ${ceiling}.`,
|
|
34
|
+
},
|
|
35
|
+
fr: {
|
|
36
|
+
recovery: "Récupération",
|
|
37
|
+
of100: "/100",
|
|
38
|
+
bands: { low: "Basse", moderate: "Moyenne", high: "Haute" },
|
|
39
|
+
noScore: "Données insuffisantes",
|
|
40
|
+
ceilingTitle: "Plafond du jour",
|
|
41
|
+
ceilings: { rest: "Repos", easy: "Facile", moderate: "Modéré", hard: "Intense" },
|
|
42
|
+
suggestedLoad: "Charge suggérée",
|
|
43
|
+
signalsTitle: "Signaux de la nuit vs votre normale",
|
|
44
|
+
signalsSubtitle: "Point = nuit dernière ; bande bleue = plage habituelle (±1 É.T.), grise = ±2 É.T.",
|
|
45
|
+
signals: { hrv: "VFC", restingHr: "FC repos", respiration: "Respiration", skinTemp: "Temp. peau", spo2: "SpO2" },
|
|
46
|
+
noData: "pas de données",
|
|
47
|
+
hrvTitle: "Variabilité de la fréquence cardiaque",
|
|
48
|
+
hrvSubtitle: "rMSSD nocturne (ms), tendance sur 7 jours et plage normale personnelle",
|
|
49
|
+
nightly: "Nuit",
|
|
50
|
+
trend7: "Moyenne 7 j",
|
|
51
|
+
normalRange: "Plage normale",
|
|
52
|
+
loadTitle: "Forme physique, fatigue et fraîcheur",
|
|
53
|
+
fitness: "Forme (CTL)",
|
|
54
|
+
fatigue: "Fatigue (ATL)",
|
|
55
|
+
form: "Fraîcheur (TSB)",
|
|
56
|
+
sleepTitle: "Sommeil",
|
|
57
|
+
need: "Besoin",
|
|
58
|
+
deep: "Profond",
|
|
59
|
+
light: "Léger",
|
|
60
|
+
rem: "Paradoxal",
|
|
61
|
+
unknownStages: "Total",
|
|
62
|
+
hours: "h",
|
|
63
|
+
months: ["janv.", "févr.", "mars", "avr.", "mai", "juin", "juil.", "août", "sept.", "oct.", "nov.", "déc."],
|
|
64
|
+
weekdays: ["D", "L", "M", "M", "J", "V", "S"],
|
|
65
|
+
altGauge: (score, band) => (score === null ? "Indice de récupération : données insuffisantes." : `Indice de récupération ${score} sur 100 (${band}).`),
|
|
66
|
+
altCeiling: (ceiling) => `Plafond d'entraînement du jour : ${ceiling}.`,
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
export function shortDate(date, lang) {
|
|
70
|
+
const month = LABELS[lang].months[Number(date.slice(5, 7)) - 1] ?? "";
|
|
71
|
+
const day = Number(date.slice(8, 10));
|
|
72
|
+
return lang === "fr" ? `${day} ${month}` : `${month} ${day}`;
|
|
73
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** SVG to PNG with resvg and the bundled Inter font: output does not depend on the fonts of the host. */
|
|
2
|
+
export declare const RENDER_SCALE = 2;
|
|
3
|
+
export declare class ChartsUnavailableError extends Error {
|
|
4
|
+
}
|
|
5
|
+
export declare function renderPng(svg: string): Promise<{
|
|
6
|
+
png: Buffer;
|
|
7
|
+
width: number;
|
|
8
|
+
height: number;
|
|
9
|
+
}>;
|
|
10
|
+
export declare const FONT_DIRECTORY: string;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** SVG to PNG with resvg and the bundled Inter font: output does not depend on the fonts of the host. */
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
export const RENDER_SCALE = 2;
|
|
5
|
+
const FONT_FILES = ["Inter_400Regular.ttf", "Inter_600SemiBold.ttf"].map((name) => fileURLToPath(new URL(`../../assets/fonts/${name}`, import.meta.url)));
|
|
6
|
+
export class ChartsUnavailableError extends Error {
|
|
7
|
+
}
|
|
8
|
+
let loaded;
|
|
9
|
+
function resvg() {
|
|
10
|
+
loaded ??= import("@resvg/resvg-js").catch((error) => {
|
|
11
|
+
loaded = undefined;
|
|
12
|
+
throw new ChartsUnavailableError(`The PNG renderer (@resvg/resvg-js) is not installed for this platform: ${error instanceof Error ? error.message : "unknown error"}.`);
|
|
13
|
+
});
|
|
14
|
+
return loaded;
|
|
15
|
+
}
|
|
16
|
+
export async function renderPng(svg) {
|
|
17
|
+
const { Resvg } = await resvg();
|
|
18
|
+
const rendered = new Resvg(svg, {
|
|
19
|
+
font: { fontFiles: FONT_FILES, loadSystemFonts: false, defaultFontFamily: "Inter" },
|
|
20
|
+
fitTo: { mode: "zoom", value: RENDER_SCALE },
|
|
21
|
+
shapeRendering: 2,
|
|
22
|
+
textRendering: 1,
|
|
23
|
+
}).render();
|
|
24
|
+
return { png: rendered.asPng(), width: rendered.width, height: rendered.height };
|
|
25
|
+
}
|
|
26
|
+
export const FONT_DIRECTORY = path.dirname(FONT_FILES[0]);
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/** Minimal SVG helpers: no dependency, deterministic output (same input, same bytes). */
|
|
2
|
+
export type ThemeName = "light" | "dark";
|
|
3
|
+
export type Theme = {
|
|
4
|
+
name: ThemeName;
|
|
5
|
+
background: string;
|
|
6
|
+
text: string;
|
|
7
|
+
muted: string;
|
|
8
|
+
grid: string;
|
|
9
|
+
track: string;
|
|
10
|
+
accent: string;
|
|
11
|
+
accent2: string;
|
|
12
|
+
good: string;
|
|
13
|
+
warn: string;
|
|
14
|
+
bad: string;
|
|
15
|
+
/** Ceiling colours: restriction is shown in neutral/amber tones, never in alarm red. */
|
|
16
|
+
ceiling: Record<"rest" | "easy" | "moderate" | "hard", string>;
|
|
17
|
+
};
|
|
18
|
+
export declare const THEMES: Record<ThemeName, Theme>;
|
|
19
|
+
export declare const FONT = "Inter, sans-serif";
|
|
20
|
+
export declare function esc(value: string): string;
|
|
21
|
+
/** Number to at most two decimals, never "-0". */
|
|
22
|
+
export declare function n(value: number): string;
|
|
23
|
+
export type TextOptions = {
|
|
24
|
+
size?: number;
|
|
25
|
+
weight?: 400 | 600;
|
|
26
|
+
anchor?: "start" | "middle" | "end";
|
|
27
|
+
fill: string;
|
|
28
|
+
baseline?: "auto" | "middle" | "hanging";
|
|
29
|
+
halo?: string;
|
|
30
|
+
};
|
|
31
|
+
export declare function text(x: number, y: number, content: string, options: TextOptions): string;
|
|
32
|
+
export declare function svgDocument(width: number, height: number, theme: Theme, title: string, description: string, body: string): string;
|
|
33
|
+
export declare function scale(domainMin: number, domainMax: number, rangeMin: number, rangeMax: number): (value: number) => number;
|
|
34
|
+
/** Round tick values covering [min, max] (about `count` of them). */
|
|
35
|
+
export declare function niceTicks(min: number, max: number, count?: number): number[];
|
|
36
|
+
/** Polyline path through points, breaking the line where a value is missing. */
|
|
37
|
+
export declare function linePath(points: ReadonlyArray<{
|
|
38
|
+
x: number;
|
|
39
|
+
y: number | null;
|
|
40
|
+
}>): string;
|
|
41
|
+
/** Point on a circle; angle 0 = right, pi = left, counter-clockwise on screen. */
|
|
42
|
+
export declare function polar(cx: number, cy: number, radius: number, angle: number): {
|
|
43
|
+
x: number;
|
|
44
|
+
y: number;
|
|
45
|
+
};
|
|
46
|
+
/** Arc along the top half-circle from value `from` to `to` (0-100), left to right. */
|
|
47
|
+
export declare function gaugeArc(cx: number, cy: number, radius: number, from: number, to: number): string;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/** Minimal SVG helpers: no dependency, deterministic output (same input, same bytes). */
|
|
2
|
+
export const THEMES = {
|
|
3
|
+
light: {
|
|
4
|
+
name: "light",
|
|
5
|
+
background: "#ffffff",
|
|
6
|
+
text: "#111827",
|
|
7
|
+
muted: "#6b7280",
|
|
8
|
+
grid: "#e5e7eb",
|
|
9
|
+
track: "#f3f4f6",
|
|
10
|
+
accent: "#2563eb",
|
|
11
|
+
accent2: "#ea580c",
|
|
12
|
+
good: "#16a34a",
|
|
13
|
+
warn: "#d97706",
|
|
14
|
+
bad: "#dc2626",
|
|
15
|
+
ceiling: { rest: "#64748b", easy: "#d97706", moderate: "#2563eb", hard: "#16a34a" },
|
|
16
|
+
},
|
|
17
|
+
dark: {
|
|
18
|
+
name: "dark",
|
|
19
|
+
background: "#111827",
|
|
20
|
+
text: "#f3f4f6",
|
|
21
|
+
muted: "#9ca3af",
|
|
22
|
+
grid: "#374151",
|
|
23
|
+
track: "#1f2937",
|
|
24
|
+
accent: "#60a5fa",
|
|
25
|
+
accent2: "#fb923c",
|
|
26
|
+
good: "#4ade80",
|
|
27
|
+
warn: "#fbbf24",
|
|
28
|
+
bad: "#f87171",
|
|
29
|
+
ceiling: { rest: "#94a3b8", easy: "#fbbf24", moderate: "#60a5fa", hard: "#4ade80" },
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
export const FONT = "Inter, sans-serif";
|
|
33
|
+
export function esc(value) {
|
|
34
|
+
return value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
35
|
+
}
|
|
36
|
+
/** Number to at most two decimals, never "-0". */
|
|
37
|
+
export function n(value) {
|
|
38
|
+
const rounded = Math.round(value * 100) / 100;
|
|
39
|
+
return String(Object.is(rounded, -0) ? 0 : rounded);
|
|
40
|
+
}
|
|
41
|
+
export function text(x, y, content, options) {
|
|
42
|
+
const { size = 12, weight = 400, anchor = "start", fill, baseline = "auto", halo } = options;
|
|
43
|
+
const outline = halo ? ` stroke="${halo}" stroke-width="3.5" stroke-linejoin="round" paint-order="stroke"` : "";
|
|
44
|
+
return `<text x="${n(x)}" y="${n(y)}" font-family="${FONT}" font-size="${size}" font-weight="${weight}" text-anchor="${anchor}" dominant-baseline="${baseline}" fill="${fill}"${outline}>${esc(content)}</text>`;
|
|
45
|
+
}
|
|
46
|
+
export function svgDocument(width, height, theme, title, description, body) {
|
|
47
|
+
return (`<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}" viewBox="0 0 ${width} ${height}" role="img">` +
|
|
48
|
+
`<title>${esc(title)}</title><desc>${esc(description)}</desc>` +
|
|
49
|
+
`<rect width="${width}" height="${height}" rx="12" fill="${theme.background}"/>` +
|
|
50
|
+
body +
|
|
51
|
+
`</svg>`);
|
|
52
|
+
}
|
|
53
|
+
export function scale(domainMin, domainMax, rangeMin, rangeMax) {
|
|
54
|
+
const span = domainMax - domainMin || 1;
|
|
55
|
+
return (value) => rangeMin + ((value - domainMin) / span) * (rangeMax - rangeMin);
|
|
56
|
+
}
|
|
57
|
+
/** Round tick values covering [min, max] (about `count` of them). */
|
|
58
|
+
export function niceTicks(min, max, count = 4) {
|
|
59
|
+
if (!(max > min))
|
|
60
|
+
return [min];
|
|
61
|
+
const rawStep = (max - min) / count;
|
|
62
|
+
const magnitude = 10 ** Math.floor(Math.log10(rawStep));
|
|
63
|
+
const step = [1, 2, 5, 10].map((m) => m * magnitude).find((candidate) => candidate >= rawStep) ?? 10 * magnitude;
|
|
64
|
+
const ticks = [];
|
|
65
|
+
for (let value = Math.ceil(min / step) * step; value <= max + step * 1e-9; value += step)
|
|
66
|
+
ticks.push(Math.round(value * 1e6) / 1e6);
|
|
67
|
+
return ticks;
|
|
68
|
+
}
|
|
69
|
+
/** Polyline path through points, breaking the line where a value is missing. */
|
|
70
|
+
export function linePath(points) {
|
|
71
|
+
let out = "";
|
|
72
|
+
let open = false;
|
|
73
|
+
for (const point of points) {
|
|
74
|
+
if (point.y === null) {
|
|
75
|
+
open = false;
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
out += `${open ? "L" : "M"}${n(point.x)} ${n(point.y)}`;
|
|
79
|
+
open = true;
|
|
80
|
+
}
|
|
81
|
+
return out;
|
|
82
|
+
}
|
|
83
|
+
/** Point on a circle; angle 0 = right, pi = left, counter-clockwise on screen. */
|
|
84
|
+
export function polar(cx, cy, radius, angle) {
|
|
85
|
+
return { x: cx + radius * Math.cos(angle), y: cy - radius * Math.sin(angle) };
|
|
86
|
+
}
|
|
87
|
+
/** Arc along the top half-circle from value `from` to `to` (0-100), left to right. */
|
|
88
|
+
export function gaugeArc(cx, cy, radius, from, to) {
|
|
89
|
+
const start = polar(cx, cy, radius, Math.PI * (1 - from / 100));
|
|
90
|
+
const end = polar(cx, cy, radius, Math.PI * (1 - to / 100));
|
|
91
|
+
return `M${n(start.x)} ${n(start.y)}A${n(radius)} ${n(radius)} 0 0 1 ${n(end.x)} ${n(end.y)}`;
|
|
92
|
+
}
|