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 ` + const cid = 'ch' + (++CHART_SEQ); + CHARTS[cid] = { + W, H, unit: o.unit, + pts: (single ? [points[0]] : points).map(p => ({ x: X(p.t), y: Y(p.y), iso: p.d || tsToISO(p.t), v: p.y })), + top: P.t, bot: H - P.b, left: P.l, right: W - P.r + }; + return `
+ + ${axes} - ${fmtNum(last.y)} - ${fmtDate(new Date(first.t).toISOString().slice(0,10))} - ${fmtDate(new Date(last.t).toISOString().slice(0,10))} - high ${fmtNum(Math.max(...ys))} · low ${fmtNum(Math.min(...ys))} ${o.unit} - `; + + + + + + +
`; +} +/* finger/mouse hover: snap to nearest point, crosshair + date/value tooltip */ +function bindCharts(root) { + root.querySelectorAll('.chart-i').forEach(box => { + const d = CHARTS[box.dataset.cid]; + if (!d || !d.pts.length) return; + const svg = box.querySelector('svg'), tip = box.querySelector('.ctip'); + const cross = svg.querySelector('.cross'); + const vl = svg.querySelector('.cvl'), hl = svg.querySelector('.chl'), dot = svg.querySelector('.cdot'); + function show(clientX) { + const r = svg.getBoundingClientRect(); + const w = r.width || d.W; + const vx = (clientX - r.left) / w * d.W; + let best = d.pts[0]; + d.pts.forEach(p => { if (Math.abs(p.x - vx) < Math.abs(best.x - vx)) best = p; }); + vl.setAttribute('x1', best.x); vl.setAttribute('x2', best.x); + hl.setAttribute('y1', best.y); hl.setAttribute('y2', best.y); + dot.setAttribute('cx', best.x); dot.setAttribute('cy', best.y); + cross.setAttribute('visibility', 'visible'); + tip.style.display = 'block'; + tip.textContent = fmtDate(best.iso, true) + ' · ' + fmtNum(best.v) + (d.unit ? ' ' + d.unit : ''); + const px = best.x / d.W * w; + const tw = tip.offsetWidth || 110; + tip.style.left = Math.max(2, Math.min(w - tw - 2, px - tw / 2)) + 'px'; + } + const onMove = e => { const p = e.touches ? e.touches[0] : e; if (p && p.clientX !== undefined) show(p.clientX); }; + box.addEventListener('mousemove', onMove); + box.addEventListener('mousedown', onMove); + box.addEventListener('touchstart', onMove, { passive: true }); + box.addEventListener('touchmove', onMove, { passive: true }); + }); } /* ============================================================ @@ -519,7 +602,7 @@ function viewHome(app) { const plannedPerWeek = Object.keys(S.week).filter(k => S.week[k]).length; const lastW = S.workouts[S.workouts.length-1]; - const bwSpark = lineChart(S.bodyweight.slice(-30).map(b => ({t: b.t || new Date(b.d).getTime(), y: b.w})), {h: 90}); + const bwSpark = lineChart(S.bodyweight.slice(-30).map(b => ({t: b.t || new Date(b.d).getTime(), y: b.w, d: b.d})), {h: 90, axes: false, unit: S.unit}); app.innerHTML = `
@@ -609,6 +692,7 @@ function viewHome(app) { const bp = $('#btn-goplan'); if (bp) bp.onclick = () => nav('#plan'); const bh = $('#btn-history'); if (bh) bh.onclick = () => nav('#history'); bindWorkoutItems(app); + bindCharts(app); } function setsDoneActive() { let n = 0; if (S.active) S.active.entries.forEach(e => e.sets.forEach(s => { if (s.done) n++; })); @@ -1271,7 +1355,7 @@ function viewStats(app) { const now = Date.now(); const bwPts = S.bodyweight .filter(b => statsRange === 0 || (b.t || new Date(b.d).getTime()) > now - statsRange * 86400000) - .map(b => ({ t: b.t || new Date(b.d).getTime(), y: b.w })); + .map(b => ({ t: b.t || new Date(b.d).getTime(), y: b.w, d: b.d })); const bw30 = S.bodyweight.filter(b => (b.t || new Date(b.d).getTime()) > now - 30 * 86400000); const bwDelta30 = bw30.length > 1 ? bw30[bw30.length-1].w - bw30[0].w : null; const monthW = S.workouts.filter(w => w.d.slice(0, 7) === todayISO().slice(0, 7)).length; @@ -1290,7 +1374,7 @@ function viewStats(app) { if (mx > 0) pts.push({ t: w.start, y: mx, d: w.d, sets: en.sets.filter(s => s.done) }); } }); - exChart = lineChart(pts.map(p => ({ t: p.t, y: p.y })), { h: 150, unit: S.unit, color: 'var(--blue)' }); + exChart = lineChart(pts.map(p => ({ t: p.t, y: p.y, d: p.d })), { h: 150, unit: S.unit, color: 'var(--blue)' }); exList = pts.slice(-5).reverse().map(p => `
${fmtDate(p.d, true)} @@ -1352,6 +1436,7 @@ function viewStats(app) { const ha = $('#st-histall'); if (ha) ha.onclick = () => nav('#history'); bindWorkoutItems(app); bindHeatmap(app); + bindCharts(app); } function viewHistory(app) { diff --git a/app/index.html b/app/index.html index 5ff1849..1aeedbc 100644 --- a/app/index.html +++ b/app/index.html @@ -13,7 +13,7 @@ - +
🏋️
@@ -45,7 +45,7 @@
- - + + diff --git a/app/style.css b/app/style.css index 0160a60..fb9d6e6 100644 --- a/app/style.css +++ b/app/style.css @@ -178,6 +178,10 @@ a{color:var(--acc)} /* ---------- charts ---------- */ .chart{width:100%;overflow:hidden} .chart svg{display:block;width:100%;height:auto} +.chart-i{position:relative;touch-action:pan-y} +.ctip{position:absolute;top:-4px;background:var(--card2);border:1px solid var(--line); + border-radius:99px;padding:4px 12px;font-size:.76rem;font-weight:700;white-space:nowrap; + pointer-events:none;box-shadow:0 4px 14px rgba(0,0,0,.4)} /* activity heatmap (GitHub-style) */ .hm-wrap{overflow-x:auto;-webkit-overflow-scrolling:touch;scrollbar-width:none;padding-bottom:2px} diff --git a/app/sw.js b/app/sw.js index b4ab789..c4c8398 100644 --- a/app/sw.js +++ b/app/sw.js @@ -1,6 +1,6 @@ /* GymLog service worker — network-first for app shell, cache-first for media */ -const CACHE = 'gymlog-v5'; -const SHELL = ['./', 'index.html', 'style.css?v=5', 'app.js?v=5', 'data.js?v=5', 'manifest.json']; +const CACHE = 'gymlog-v6'; +const SHELL = ['./', 'index.html', 'style.css?v=6', 'app.js?v=6', 'data.js?v=6', 'manifest.json']; self.addEventListener('install', e => { e.waitUntil(caches.open(CACHE).then(c => c.addAll(SHELL)).then(() => self.skipWaiting()));