// Petites courbes en SVG, sans bibliothèque : poids, charge, répétitions, // assiduité. Une seule série par courbe (jamais deux échelles), un trait plein // à angles vifs, des repères carrés. Les valeurs sont aussi dans une liste ou // un tableau à côté : la courbe n'est jamais la seule source. // La géométrie (pure, testée) est séparée du dessin (DOM). const NS = "http://www.w3.org/2000/svg"; /** * @typedef {object} Geometry * @property {string} path « M x,y L x,y … » * @property {{x: number, y: number}[]} dots * @property {{y: number, value: number}[]} ticks trois repères horizontaux * @property {(x: number, value: number) => {x: number, y: number}} at */ /** * Place des points (x quelconque, croissant ; y en unité) dans un cadre. * @param {{x: number, y: number}[]} points * @param {{width: number, height: number, left?: number, right?: number, top?: number, bottom?: number, minSpan?: number}} box * @returns {Geometry} */ export function lineGeometry(points, { width, height, left = 36, right = 8, top = 10, bottom = 20, minSpan = 1 }) { const xs = points.map((p) => p.x), ys = points.map((p) => p.y); const x0 = Math.min(...xs), x1 = Math.max(...xs); let y0 = Math.min(...ys), y1 = Math.max(...ys); const margin = Math.max(minSpan / 2, (y1 - y0) * 0.15); y0 -= margin; y1 += margin; const px = (/** @type {number} */ x) => left + (width - left - right) * (x1 === x0 ? 0.5 : (x - x0) / (x1 - x0)); const py = (/** @type {number} */ y) => top + (height - top - bottom) * (1 - (y - y0) / (y1 - y0)); const round = (/** @type {number} */ n) => Math.round(n * 10) / 10; const dots = points.map((p) => ({ x: round(px(p.x)), y: round(py(p.y)) })); return { path: dots.map((d, i) => `${i ? "L" : "M"}${d.x},${d.y}`).join(" "), dots, ticks: [0, 0.5, 1].map((f) => ({ value: y0 + (y1 - y0) * f, y: round(py(y0 + (y1 - y0) * f)) })), at: (x, value) => ({ x: round(px(x)), y: round(py(value)) }), }; } /** * @param {string} tag * @param {Record} attrs * @param {string} [text] */ function el(tag, attrs, text) { const e = document.createElementNS(NS, tag); for (const [k, v] of Object.entries(attrs)) e.setAttribute(k, String(v)); if (text !== undefined) e.textContent = text; return e; } /** * Courbe d'une série dans le temps. * @param {{points: {x: number, y: number}[], label: string, format: (v: number) => string, xLabels: [string, string], trend?: {from: number, to: number}|null, minSpan?: number}} o * x : jours depuis une origine ; trend : valeurs de la tendance au premier et au dernier x * @returns {SVGSVGElement} */ export function lineChart({ points, label, format, xLabels, trend = null, minSpan = 1 }) { const W = 340, H = 160; const g = lineGeometry(points, { width: W, height: H, minSpan }); const svg = /** @type {SVGSVGElement} */ (el("svg", { viewBox: `0 0 ${W} ${H}`, role: "img", "aria-label": label })); for (const t of g.ticks) { svg.append(el("line", { class: "chart__axis", x1: 36, x2: W - 8, y1: t.y, y2: t.y })); svg.append(el("text", { class: "chart__text", x: 30, y: t.y + 4, "text-anchor": "end" }, format(t.value))); } if (trend && points.length > 2) { const a = g.at(points[0].x, trend.from), b = g.at(points[points.length - 1].x, trend.to); svg.append(el("line", { class: "chart__trend", x1: a.x, y1: a.y, x2: b.x, y2: b.y })); } svg.append(el("path", { class: "chart__line", d: g.path })); for (const d of g.dots) svg.append(el("rect", { class: "chart__dot", x: d.x - 3, y: d.y - 3, width: 6, height: 6 })); svg.append(el("text", { class: "chart__text", x: 36, y: H - 4 }, xLabels[0])); svg.append(el("text", { class: "chart__text", x: W - 8, y: H - 4, "text-anchor": "end" }, xLabels[1])); return svg; } /** * Barres verticales (une par semaine) : des blocs, comme des disques empilés. * @param {{values: number[], label: string, labels: [string, string], highlightLast?: boolean}} o * @returns {SVGSVGElement} */ export function barChart({ values, label, labels, highlightLast = true }) { const W = 340, H = 130, top = 8, bottom = 20, left = 4, right = 4; const max = Math.max(1, ...values); const step = (W - left - right) / values.length; const svg = /** @type {SVGSVGElement} */ (el("svg", { viewBox: `0 0 ${W} ${H}`, role: "img", "aria-label": label })); svg.append(el("line", { class: "chart__axis", x1: left, x2: W - right, y1: H - bottom, y2: H - bottom })); values.forEach((v, i) => { const h = ((H - top - bottom) * v) / max; if (v > 0) svg.append(el("rect", { class: `chart__bar${highlightLast && i === values.length - 1 ? " chart__bar--now" : ""}`, x: Math.round(left + i * step + 2), y: Math.round(H - bottom - h), width: Math.max(2, Math.round(step - 4)), height: Math.round(h), })); }); svg.append(el("text", { class: "chart__text", x: left, y: H - 4 }, labels[0])); svg.append(el("text", { class: "chart__text", x: W - right, y: H - 4, "text-anchor": "end" }, labels[1])); return svg; }