diff --git a/app/app.js b/app/app.js
index e5b156e..d355663 100644
--- a/app/app.js
+++ b/app/app.js
@@ -343,36 +343,119 @@ function renderTimer() {
/* ============================================================
CHARTS (hand-rolled SVG)
============================================================ */
+const tsToISO = t => {
+ const d = new Date(t);
+ return d.getFullYear() + '-' + String(d.getMonth()+1).padStart(2,'0') + '-' + String(d.getDate()).padStart(2,'0');
+};
+let CHART_SEQ = 0;
+const CHARTS = {}; // cid -> {pts:[{x,y,iso,v}], W, H, unit}
function lineChart(points, opts) {
- // points: [{t: ms, y: num, lbl}] sorted by t
- const o = Object.assign({ h: 150, unit: '', color: 'var(--acc)' }, opts);
- const W = 340, H = o.h, P = { l: 8, r: 40, t: 14, b: 20 };
+ // points: [{t: ms, y: num, d?: iso}] sorted by t
+ const o = Object.assign({ h: 150, unit: '', color: 'var(--acc)', axes: true }, opts);
+ const W = 340, H = o.h;
+ const P = { l: o.axes ? 34 : 8, r: 12, t: 10, b: o.axes ? 22 : 8 };
if (points.length === 0) return `
No data yet
`;
- if (points.length === 1) points = [points[0], points[0]];
+ const single = points.length === 1;
+ if (single) points = [points[0], points[0]];
const ys = points.map(p => p.y);
let ymin = Math.min(...ys), ymax = Math.max(...ys);
if (ymin === ymax) { ymin -= 1; ymax += 1; }
const pad = (ymax - ymin) * 0.12; ymin -= pad; ymax += pad;
const t0 = points[0].t, t1 = points[points.length-1].t || t0 + 1;
- const X = t => t1 === t0 ? P.l : P.l + (t - t0) / (t1 - t0) * (W - P.l - P.r);
+ const X = t => t1 === t0 ? (P.l + W - P.r) / 2 : P.l + (t - t0) / (t1 - t0) * (W - P.l - P.r);
const Y = y => P.t + (1 - (y - ymin) / (ymax - ymin)) * (H - P.t - P.b);
+
+ // y-axis: nice tick steps + gridlines + kg labels
+ let axes = '';
+ if (o.axes) {
+ const range = ymax - ymin, raw = range / 3;
+ const pow = Math.pow(10, Math.floor(Math.log10(raw)));
+ let step = 10 * pow;
+ for (const m of [1, 2, 2.5, 5, 10]) if (raw <= m * pow) { step = m * pow; break; }
+ for (let v = Math.ceil(ymin / step) * step; v <= ymax + 1e-9; v += step) {
+ const y = Y(v).toFixed(1);
+ axes += `
+ ${fmtNum(v)}`;
+ }
+ // x-axis: month boundaries in between (or 3 date labels inside one month)
+ const d0 = new Date(t0), d1 = new Date(t1);
+ const MONTHS = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
+ const ticks = [];
+ let m = new Date(d0.getFullYear(), d0.getMonth() + 1, 1);
+ while (m <= d1) { ticks.push({ t: +m, txt: MONTHS[m.getMonth()] }); m = new Date(m.getFullYear(), m.getMonth() + 1, 1); }
+ if (ticks.length === 0 && !single) {
+ for (let i = 0; i <= 2; i++) {
+ const t = t0 + (t1 - t0) * i / 2;
+ const dd = new Date(t);
+ ticks.push({ t, txt: dd.getDate() + ' ' + MONTHS[dd.getMonth()], anchor: i === 0 ? 'start' : i === 2 ? 'end' : 'middle' });
+ }
+ }
+ const every = Math.max(1, Math.ceil(ticks.length / 7));
+ ticks.forEach((tk, i) => {
+ if (i % every) return;
+ const x = X(tk.t).toFixed(1);
+ axes += `
+ ${tk.txt}`;
+ });
+ }
+
const pts = points.map(p => X(p.t).toFixed(1) + ',' + Y(p.y).toFixed(1)).join(' ');
const last = points[points.length-1];
- const first = points[0];
const gid = 'g' + Math.random().toString(36).slice(2, 7);
- return `