Dashboard: blood pressure, haemoglobin and ferritin in one chart

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Riaan Doorduin
2026-11-15 13:45:34 +02:00
parent b5521d6731
commit 03dabb2a3a

View File

@@ -135,11 +135,8 @@ h3.sub { margin: 8px 0 0; font: 600 16px var(--body) }
<div id="bpBlock" hidden>
<h3 class="sub" id="bpHead">Blood pressure and haemoglobin (entered by hand)</h3>
<div class="grid2" id="bpGrid">
<div class="card" style="align-content:start"><div class="legend" id="hBpLeg"></div><div class="chartbox"><canvas id="cBp" role="img" aria-label="Blood pressure and haemoglobin readings"></canvas></div></div>
<div class="scroll"><table id="tBp"></table></div>
</div>
<div id="labGrid" hidden style="margin-top:16px">
<div class="card" style="align-content:start"><h3 id="labHead">Ferritin</h3><p class="note" id="labNote"></p><div class="legend" id="hLabLeg"></div><div class="chartbox"><canvas id="cLab" role="img" aria-label="Blood test results over time"></canvas></div></div>
<div class="card" style="align-content:start"><p class="note" id="bpNote" hidden></p><div class="legend" id="hBpLeg"></div><div class="chartbox" style="height:420px"><canvas id="cBp" role="img" aria-label="Blood pressure, haemoglobin and blood test results over time"></canvas></div></div>
<div class="scroll" id="bpTable"><table id="tBp"></table></div>
</div>
</div>
</section>
@@ -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 = '<thead><tr><th class="l">Date</th><th>Blood pressure</th><th>Pulse</th><th>Hb g/dL</th><th class="l">Note</th></tr></thead><tbody>' +
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 = '<thead><tr><th class="l">Date</th><th>Blood pressure</th><th>Pulse</th><th>Hb g/dL</th><th class="l">Note</th></tr></thead><tbody>' +
[...B].reverse().map(b => `<tr><td>${b.d}</td><td>${b.sys}/${b.dia}</td><td>${b.pulse ?? ''}</td><td>${b.hb ?? ''}</td><td class="l dim">${(b.note || '').replace(/[<>&]/g, '')}</td></tr>`).join('') + '</tbody>';
}
})();