Make the by-hour heatmap blocks tappable

The count behind a block was only ever reachable through its <title> tooltip,
which needs a pointer — on a phone there was no way to find out whether a dark
block meant two meals or five. Each cell now carries a transparent hit rect
that names it in a caption under the chart ("3 meals between 07:00 and
08:00"), the same string the tooltip shows, so hover and tap agree. The
focused cell takes an accent ring and tapping it again clears it.

The hit rect claims the 1px spacing between cells and half the gap to the
neighbouring rows, which takes the target from ~13x31 to ~13x38 CSS px on a
360px-wide screen; the rows still tile without overlapping and stay clear of
the hour labels. Width is capped by fitting 24 hours across the chart, so a
mis-tap lands on a neighbouring hour — the caption names the range it hit,
which makes that self-correcting.

The focused cell is held outside the render so a background sync can't wipe
what is being read; its count is recomputed each pass, so the caption stays
current. The caption is aria-live, which also gives screen readers a route to
the numbers that role="img" on the svg otherwise closes off.
This commit is contained in:
Alexander Heldt
2026-08-24 12:56:11 +00:00
parent d81c20ac9b
commit 2a14c34010
4 changed files with 57 additions and 9 deletions
+46 -8
View File
@@ -1349,18 +1349,25 @@
setChartSVG(svg, parts); // wires the .bar[data-day] click → select that day setChartSVG(svg, parts); // wires the .bar[data-day] click → select that day
} }
// Which heatmap block is focused, remembered across re-renders so a background
// sync doesn't wipe the block the user just tapped. The counts behind it are
// recomputed every render, so the readout stays current.
let hourCellSel = null; // `${type}-${hour}`, or null for nothing focused
const HOUR_CELL_HINT = "Tap a block to see how many it counts.";
// Hour-of-day heatmap: one row per event type, 24 cells shaded by how often // Hour-of-day heatmap: one row per event type, 24 cells shaded by how often
// that event lands in each hour across the window. Reveals daily rhythm that // that event lands in each hour across the window. Reveals daily rhythm that
// the median gap can't show (e.g. "always poos ~7am and ~6pm"). // the median gap can't show (e.g. "always poos ~7am and ~6pm").
function renderHourHeatmap(events) { function renderHourHeatmap(events) {
const svg = document.getElementById("chart-hour-heatmap"); const svg = document.getElementById("chart-hour-heatmap");
if (!svg) return; if (!svg) return;
const info = document.getElementById("hour-heatmap-info");
const from = startOfDay(new Date(Date.now() - (chartDays() - 1) * 86_400_000)).getTime(); const from = startOfDay(new Date(Date.now() - (chartDays() - 1) * 86_400_000)).getTime();
const series = [ const series = [
{ type: "pee", label: "Pees", cls: "hm-pee" }, { type: "pee", label: "Pees", cls: "hm-pee", one: "pee", many: "pees" },
{ type: "poo", label: "Poos", cls: "hm-poo" }, { type: "poo", label: "Poos", cls: "hm-poo", one: "poo", many: "poos" },
{ type: "eat", label: "Meals", cls: "hm-eat" }, { type: "eat", label: "Meals", cls: "hm-eat", one: "meal", many: "meals" },
]; ];
const counts = {}; const counts = {};
for (const s of series) counts[s.type] = new Array(24).fill(0); for (const s of series) counts[s.type] = new Array(24).fill(0);
@@ -1376,7 +1383,15 @@
const rowH = (H - MT - MB - (series.length - 1) * rowGap) / series.length; const rowH = (H - MT - MB - (series.length - 1) * rowGap) / series.length;
const cellW = innerW / 24; const cellW = innerW / 24;
// "3 meals between 07:00 and 08:00" — self-describing, so the same string
// serves as the pointer tooltip and as the tap readout under the chart.
const cellText = (s, h, c) =>
`${c === 0 ? "No" : c} ${c === 1 ? s.one : s.many} between ` +
`${pad2(h)}:00 and ${pad2((h + 1) % 24)}:00`;
const parts = []; const parts = [];
const hits = []; // appended last so they sit above every row
const details = {}; // cell key → readout text
series.forEach((s, r) => { series.forEach((s, r) => {
const y = MT + r * (rowH + rowGap); const y = MT + r * (rowH + rowGap);
const max = Math.max(1, ...counts[s.type]); const max = Math.max(1, ...counts[s.type]);
@@ -1384,11 +1399,18 @@
const c = counts[s.type][h]; const c = counts[s.type][h];
const op = c === 0 ? 0.06 : 0.2 + 0.8 * (c / max); const op = c === 0 ? 0.06 : 0.2 + 0.8 * (c / max);
const x = ML + h * cellW; const x = ML + h * cellW;
const range = `${pad2(h)}:00${pad2((h + 1) % 24)}:00`; const key = `${s.type}-${h}`;
details[key] = cellText(s, h, c);
parts.push( parts.push(
`<rect class="hm-cell ${s.cls}" x="${x.toFixed(1)}" y="${y.toFixed(1)}" ` + `<rect class="hm-cell ${s.cls}" data-cell="${key}" x="${x.toFixed(1)}" y="${y.toFixed(1)}" ` +
`width="${(cellW - 1).toFixed(1)}" height="${rowH.toFixed(1)}" rx="1.5" fill-opacity="${op.toFixed(2)}">` + `width="${(cellW - 1).toFixed(1)}" height="${rowH.toFixed(1)}" rx="1.5" fill-opacity="${op.toFixed(2)}"/>`
`<title>${escapeText(`${s.label} · ${range}: ${c}`)}</title></rect>` );
// A cell is only ~12px wide on a phone, so the tap target claims the
// spacing between cells and half the gap to the neighbouring rows.
hits.push(
`<rect class="hm-hit" data-cell="${key}" x="${x.toFixed(1)}" y="${(y - rowGap / 2).toFixed(1)}" ` +
`width="${cellW.toFixed(1)}" height="${(rowH + rowGap).toFixed(1)}">` +
`<title>${escapeText(details[key])}</title></rect>`
); );
} }
parts.push(`<text x="${ML - 6}" y="${(y + rowH / 2 + 3).toFixed(1)}" text-anchor="end">${s.label}</text>`); parts.push(`<text x="${ML - 6}" y="${(y + rowH / 2 + 3).toFixed(1)}" text-anchor="end">${s.label}</text>`);
@@ -1401,7 +1423,23 @@
} }
parts.push(`<text x="${(ML + innerW).toFixed(1)}" y="${yAxis}" text-anchor="end">24h</text>`); parts.push(`<text x="${(ML + innerW).toFixed(1)}" y="${yAxis}" text-anchor="end">24h</text>`);
svg.innerHTML = parts.join(""); svg.innerHTML = parts.concat(hits).join("");
// <title> tooltips only ever show on a pointer, which left phones with no
// way to read a block's count. Tap/hover names the block under the chart;
// tapping the focused block again clears it.
const cells = svg.querySelectorAll(".hm-cell[data-cell]");
const focusCell = (key) => {
hourCellSel = details[key] ? key : null;
cells.forEach(c => c.classList.toggle("hm-active", c.dataset.cell === hourCellSel));
if (info) info.textContent = hourCellSel ? details[hourCellSel] : HOUR_CELL_HINT;
};
svg.querySelectorAll(".hm-hit").forEach(hit => {
const key = hit.dataset.cell;
hit.addEventListener("click", () => focusCell(hourCellSel === key ? null : key));
hit.addEventListener("mouseenter", () => focusCell(key));
});
focusCell(hourCellSel);
} }
// ---------- sleep trend ---------- // ---------- sleep trend ----------
+1
View File
@@ -1,4 +1,5 @@
[ [
{ "date": "2026-08-24", "text": "The “By hour of day” chart is now tappable: tap any block to read what it counts (“3 meals between 07:00 and 08:00”) just under the chart — until now that number only showed as a hover tooltip, which phones never get. The block you tapped gets a ring; tap it again to clear it" },
{ "date": "2026-08-21", "text": "The sleep timer pill in the day bar — the one that appears once you've scrolled past the big timer — is now tappable: tap it while the puppy is asleep to log the wake-up, or while awake to log a sleep start, without scrolling back up to the buttons" }, { "date": "2026-08-21", "text": "The sleep timer pill in the day bar — the one that appears once you've scrolled past the big timer — is now tappable: tap it while the puppy is asleep to log the wake-up, or while awake to log a sleep start, without scrolling back up to the buttons" },
{ "date": "2026-08-20", "text": "Added reminders: turn them on in Settings and your phone gets a notification when it's time to sleep (\"Awake for 45 min\") or when there's been no pee, poo or meal for a while. Each one has its own interval, they arrive even with the app closed, and they stay quiet while the puppy is logged as asleep so you're not nagged all night. On iPhone, add Puppy Tracker to your Home Screen first — iOS only allows notifications for installed apps" }, { "date": "2026-08-20", "text": "Added reminders: turn them on in Settings and your phone gets a notification when it's time to sleep (\"Awake for 45 min\") or when there's been no pee, poo or meal for a while. Each one has its own interval, they arrive even with the app closed, and they stay quiet while the puppy is logged as asleep so you're not nagged all night. On iPhone, add Puppy Tracker to your Home Screen first — iOS only allows notifications for installed apps" },
{ "date": "2026-08-18", "text": "The sleep button that would just repeat the last one is now disabled: while asleep you can only tap ⏰ Sleep end, and while awake only 😴 Sleep start — no more accidental double taps creating zero-length sleep windows. If you did miss a boundary, you can still add it at the right time from the event log" }, { "date": "2026-08-18", "text": "The sleep button that would just repeat the last one is now disabled: while asleep you can only tap ⏰ Sleep end, and while awake only 😴 Sleep start — no more accidental double taps creating zero-length sleep windows. If you did miss a boundary, you can still add it at the right time from the event log" },
+1
View File
@@ -240,6 +240,7 @@
<section class="patterns" data-panel="hour-heatmap"> <section class="patterns" data-panel="hour-heatmap">
<h2>By hour of day <span class="muted-note" data-chart-days-label>(last 7 days)</span></h2> <h2>By hour of day <span class="muted-note" data-chart-days-label>(last 7 days)</span></h2>
<svg id="chart-hour-heatmap" class="chart-svg" viewBox="0 0 320 120" role="img" aria-label="Pee, poo and meal frequency by hour of day"></svg> <svg id="chart-hour-heatmap" class="chart-svg" viewBox="0 0 320 120" role="img" aria-label="Pee, poo and meal frequency by hour of day"></svg>
<p id="hour-heatmap-info" class="muted-note hour-point-info" aria-live="polite"></p>
<p class="muted-note">Darker = happens more often at that hour.</p> <p class="muted-note">Darker = happens more often at that hour.</p>
</section> </section>
+9 -1
View File
@@ -662,7 +662,8 @@ dialog menu {
stroke-width: 1.5; stroke-width: 1.5;
} }
.weight-point-info { .weight-point-info,
.hour-point-info {
margin: 6px 4px 0; margin: 6px 4px 0;
min-height: 1.2em; min-height: 1.2em;
text-align: center; text-align: center;
@@ -1013,6 +1014,13 @@ input.switch:checked::after { transform: translateX(18px); }
.lg[hidden] { display: none; } .lg[hidden] { display: none; }
.chart-svg .hm-cell { stroke: none; } .chart-svg .hm-cell { stroke: none; }
/* Focused block: an accent ring, drawn at full opacity so it stays visible on
the palest (zero-count) cells. */
.chart-svg .hm-cell.hm-active {
stroke: var(--accent);
stroke-width: 1.2;
}
.chart-svg .hm-hit { fill: transparent; cursor: pointer; }
.chart-svg .hm-pee { fill: var(--pee); } .chart-svg .hm-pee { fill: var(--pee); }
.chart-svg .hm-poo { fill: var(--poo); } .chart-svg .hm-poo { fill: var(--poo); }
.chart-svg .hm-eat { fill: var(--eat); } .chart-svg .hm-eat { fill: var(--eat); }