diff --git a/src/mygarmin/report/dashboard.html b/src/mygarmin/report/dashboard.html index 123b9ac..64321c7 100644 --- a/src/mygarmin/report/dashboard.html +++ b/src/mygarmin/report/dashboard.html @@ -135,11 +135,8 @@ h3.sub { margin: 8px 0 0; font: 600 16px var(--body) } @@ -426,31 +423,32 @@ if (hd) (function () { if (HEALTH.bp.length || LM.length) $('bpBlock').hidden = false; $('bpHead').textContent = [HEALTH.bp.length && 'Blood pressure', (HEALTH.bp.some(b => b.hb) || LABS.some(r => r.m === 'hb')) && 'haemoglobin', LM.length && lm !== 'hb' && LNAME[lm].toLowerCase()] .filter(Boolean).join(', ').replace(/, ([^,]*)$/, ' and $1').replace(/^./, c => c.toUpperCase()) + ' (entered by hand)'; - $('bpGrid').hidden = !HEALTH.bp.length; - if (LM.length) { - $('labGrid').hidden = false; - const labs = [...new Set(LM.map(r => r.lab || ''))].sort((a, b) => LM.filter(r => (r.lab || '') === b).length - LM.filter(r => (r.lab || '') === a).length), P = LM.map(r => ({ ...r, t: Date.parse(r.d) })); - $('labHead').textContent = `${LNAME[lm]} (${LM[0].unit})`; + if (HEALTH.bp.length || LM.length) { // one chart: blood pressure (left axis), haemoglobin and the main blood test (right axes) + const B = HEALTH.bp.map(b => ({ ...b, t: Date.parse(b.d) })), P = LM.map(r => ({ ...r, t: Date.parse(r.d) })); + const HB = [...B.filter(b => b.hb).map(b => ({ x: b.t, y: b.hb })), ...(lm !== 'hb' ? LABS.filter(r => r.m === 'hb').map(r => ({ x: Date.parse(r.d), y: r.v })) : [])].sort((a, b) => a.x - b.x); + const labs = [...new Set(LM.map(r => r.lab || ''))].sort((a, b) => LM.filter(r => (r.lab || '') === b).length - LM.filter(r => (r.lab || '') === a).length); + const tgt = P.filter(r => r.target), cols = [1, 3, 6], unit = LM[0]?.unit, name = LNAME[lm]; const refs = labs.map(l => [l, LM.find(r => (r.lab || '') === l && r.ref)?.ref]).filter(([, r]) => r); - $('labNote').textContent = (refs.length ? 'Lab reference range: ' + refs.map(([l, r]) => `${l ? l + ' ' : ''}${r[0]}–${r[1]}`).join(', ') + '. ' : '') + - (P.some(r => r.target) ? 'Dashed lines: your own target range. ' : '') + (lm === 'ferritin' ? 'Log scale, so low values stay readable.' : ''); - const tgt = P.filter(r => r.target), cols = [1, 0, 3, 6, 2]; - leg('hLabLeg', [...labs.map((l, i) => [l || LNAME[lm], color(cols[i % 5])]), ...(tgt.length ? [['Your target', css('--muted'), true]] : [])]); - new Chart($('cLab'), { type: 'line', data: { datasets: [ - ...labs.map((l, i) => line(P.filter(r => (r.lab || '') === l).map(r => ({ x: r.t, y: r.v })), color(cols[i % 5]), { label: l || LNAME[lm], pointRadius: 3, showLine: l === labs[0] })), - ...(tgt.length ? [0, 1].map(k => line(tgt.map(r => ({ x: r.t, y: r.target[k] })), css('--muted'), { label: k ? 'Target max' : 'Target min', stepped: true, borderDash: [5, 4], borderWidth: 1.5, pointHoverRadius: 0 })) : [])] }, - options: { maintainAspectRatio: false, parsing: false, plugins: { legend: { display: false }, tooltip: { ...tip, callbacks: { title: i => fmtD(i[0].raw.x), label: i => `${i.dataset.label}: ${i.raw.y} ${LM[0].unit}` } } }, - scales: { x: { ...xAxis, min: P[0].t - 15 * 864e5, max: P.at(-1).t + 15 * 864e5 }, y: { type: lm === 'ferritin' ? 'logarithmic' : 'linear', grid, ...(lm === 'ferritin' ? { afterBuildTicks: ax => { ax.ticks = [5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000].filter(v => v >= ax.min && v <= ax.max).map(value => ({ value })) } } : {}) } } } }); - } - if (HEALTH.bp.length) { - const B = HEALTH.bp.map(b => ({ ...b, t: Date.parse(b.d) })); - leg('hBpLeg', [['Systolic', color(4)], ['Diastolic', color(0)], ...(B.some(b => b.hb) ? [['Haemoglobin (right axis)', color(2)]] : [])]); - new Chart($('cBp'), { type: 'line', data: { datasets: [ - line(B.map(b => ({ x: b.t, y: b.sys })), color(4), { label: 'Systolic', pointRadius: 3 }), line(B.map(b => ({ x: b.t, y: b.dia })), color(0), { label: 'Diastolic', pointRadius: 3 }), - dots(B.filter(b => b.hb).map(b => ({ x: b.t, y: b.hb })), color(2), { label: 'Hb', pointRadius: 4, yAxisID: 'y2' })] }, - options: { maintainAspectRatio: false, parsing: false, plugins: { legend: { display: false }, tooltip: { ...tip, callbacks: { title: i => fmtD(i[0].raw.x), label: i => `${i.dataset.label}: ${i.raw.y}${i.dataset.label === 'Hb' ? ' g/dL' : ' mmHg'}` } } }, - scales: { x: { ...xAxis, min: undefined }, y: { grid, suggestedMin: 50, suggestedMax: 140 }, y2: { display: B.some(b => b.hb), position: 'right', grid: { display: false }, suggestedMin: 12, suggestedMax: 17 } } } }); - $('tBp').innerHTML = 'DateBlood pressurePulseHb g/dLNote' + + if (LM.length) { $('bpNote').hidden = false; + $('bpNote').textContent = `${name} on the outer right axis` + (lm === 'ferritin' ? ' (log scale, so low values stay readable)' : '') + '. ' + + (refs.length ? 'Lab reference range: ' + refs.map(([l, r]) => `${l ? l + ' ' : ''}${r[0]}–${r[1]}`).join(', ') + ` ${unit}. ` : '') + (tgt.length ? 'Dashed: your own target range.' : '') } + leg('hBpLeg', [...(B.length ? [['Systolic', color(4)], ['Diastolic', color(0)]] : []), ...(HB.length ? [['Haemoglobin', color(2)]] : []), + ...labs.map((l, i) => [`${name}${l ? ' ' + l : ''}`, color(cols[i % 3])]), ...(tgt.length ? [[`${name} target`, css('--muted'), true]] : [])]); + const ds = [ + ...(B.length ? [line(B.map(b => ({ x: b.t, y: b.sys })), color(4), { label: 'Systolic', unit: 'mmHg', pointRadius: 3 }), line(B.map(b => ({ x: b.t, y: b.dia })), color(0), { label: 'Diastolic', unit: 'mmHg', pointRadius: 3 })] : []), + ...(HB.length ? [dots(HB, color(2), { label: 'Hb', unit: 'g/dL', pointRadius: 4, yAxisID: 'y2' })] : []), + ...labs.map((l, i) => line(P.filter(r => (r.lab || '') === l).map(r => ({ x: r.t, y: r.v })), color(cols[i % 3]), { label: `${name}${l ? ' ' + l : ''}`, unit, pointRadius: 3, showLine: l === labs[0], yAxisID: 'y3' })), + ...(tgt.length ? [0, 1].map(k => line(tgt.map(r => ({ x: r.t, y: r.target[k] })), css('--muted'), { label: `Target ${k ? 'max' : 'min'}`, unit, stepped: true, borderDash: [5, 4], borderWidth: 1.5, pointHoverRadius: 0, yAxisID: 'y3' })) : [])]; + const ts = [...B.map(b => b.t), ...HB.map(h => h.x), ...P.map(r => r.t)], ttl = text => ({ display: true, text, color: css('--muted') }); + new Chart($('cBp'), { type: 'line', data: { datasets: ds }, + options: { maintainAspectRatio: false, parsing: false, plugins: { legend: { display: false }, tooltip: { ...tip, callbacks: { title: i => fmtD(i[0].raw.x), label: i => `${i.dataset.label}: ${i.raw.y} ${i.dataset.unit}` } } }, + scales: { x: { ...xAxis, min: Math.min(...ts) - 15 * 864e5, max: Math.max(...ts) + 15 * 864e5 }, + y: { display: B.length > 0, grid, suggestedMin: 50, suggestedMax: 140, title: ttl('mmHg') }, + y2: { display: HB.length > 0, position: 'right', grid: { display: false }, suggestedMin: 12, suggestedMax: 17, title: ttl('Hb g/dL') }, + y3: { display: LM.length > 0, position: 'right', grid: { display: B.length === 0 }, title: ttl(`${name} ${unit}`), type: lm === 'ferritin' ? 'logarithmic' : 'linear', + ...(lm === 'ferritin' ? { afterBuildTicks: ax => { ax.ticks = [5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000].filter(v => v >= ax.min && v <= ax.max).map(value => ({ value })) } } : {}) } } } }); + $('bpTable').hidden = !B.length; + if (B.length) $('tBp').innerHTML = 'DateBlood pressurePulseHb g/dLNote' + [...B].reverse().map(b => `${b.d}${b.sys}/${b.dia}${b.pulse ?? ''}${b.hb ?? ''}${(b.note || '').replace(/[<>&]/g, '')}`).join('') + ''; } })();