Add a trend line to the Sleep chart

This commit is contained in:
Alexander Heldt committed 2026-09-29 20:51:04 +00:00
1 parent 50947d077f
commit 5a419b64dd
4 files changed
+77 -8

No files matched your search

+64 -7
View File
@@ -2068,9 +2068,64 @@
} }
}); });
// The same fit the food chart draws, for "is he sleeping less as he
// grows?". A day with no sleep at all is left out as well as today and the
// marked ones: a puppy never sleeps 0h, so it is a day nobody logged, and
// fitting it as a real zero would drag the line through the floor.
const trend = dailyTrend(days, d => d.sleepHours > 0 ? d.sleepHours : null);
if (trend) {
const cx = (i) => ML + i * (barW + gap) + barW / 2;
const cy = (h) => MT + innerH * (1 - Math.min(Math.max(h, 0), yMax) / yMax);
parts.push(
`<line class="sleep-trend" ` +
`x1="${cx(trend.first).toFixed(1)}" y1="${cy(trend.at(trend.first)).toFixed(1)}" ` +
`x2="${cx(trend.last).toFixed(1)}" y2="${cy(trend.at(trend.last)).toFixed(1)}"/>`
);
}
renderSleepTrendNote(days, trend);
setChartSVG(svg, parts); setChartSVG(svg, parts);
} }
// The food trend's rules in hours: figures rounded to the half hour (the
// fitted ends are the line's guess, not a reading), and no direction claimed
// unless the move outruns the day-to-day scatter and is worth acting on.
function sleepTrendSentence(trend, windowDays) {
const window = `the last ${windowDays} days`;
if (!trend) {
return `Not enough complete days in ${window} to draw a trend — it needs four, and today doesn't count until it's over.`;
}
const half = (v) => Math.round(Math.max(0, v) * 2) / 2;
const hours = (v) => `${half(v)}h`;
const slight = Math.abs(trend.change) < 0.25 || Math.abs(trend.change) < trend.mean * 0.05;
if (!trend.clear || slight) {
return `Over ${window}, daily sleep is roughly steady, averaging about ${hours(trend.mean)} a day` +
` — day-to-day variation is larger than any trend.`;
}
// From the rounded ends, so the two figures on screen subtract to the one quoted.
const from = half(trend.at(trend.first));
const to = half(trend.at(trend.last));
if (from === to) {
return `Over ${window}, daily sleep is roughly steady at about ${hours(from)} a day.`;
}
return `Over ${window}, daily sleep is ${to > from ? "up" : "down"} about ${hours(Math.abs(to - from))}` +
` — from roughly ${hours(from)} a day to ${hours(to)}.`;
}
function renderSleepTrendNote(days, trend) {
const note = document.getElementById("sleep-note");
if (!note) return;
const lines = [sleepTrendSentence(trend, chartDays())];
// Days before logging began (or a gap in it) read as empty bars; say they
// were skipped so the line isn't taken to have ignored real data.
const blank = days.filter((d, i) => i < days.length - 1 && !d.excluded && !(d.sleepHours > 0)).length;
if (blank > 0) {
lines.push(`${blank} ${blank === 1 ? "day" : "days"} with no sleep logged ${blank === 1 ? "is" : "are"} left out.`);
}
note.textContent = lines.join(" ");
note.hidden = false;
}
function drawCountsChart(days) { function drawCountsChart(days) {
const svg = document.getElementById("chart-counts"); const svg = document.getElementById("chart-counts");
const W = 320, H = 180; const W = 320, H = 180;
@@ -2150,14 +2205,16 @@
// up over the day — a moving line that reflects the clock rather than the // up over the day — a moving line that reflects the clock rather than the
// puppy. The line is drawn only across the days it was fitted on, so it never // puppy. The line is drawn only across the days it was fitted on, so it never
// implies it knows about the ones it skipped. // implies it knows about the ones it skipped.
// valueOf picks which figure to fit: the day total by default, or one // valueOf picks which figure to fit: one food kind's grams, or the day's
// kind's share of it when the bars are split. // sleep hours. It may return null to leave a day out of the fit altogether.
function foodTrend(days, valueOf = (d) => d.grams) { function dailyTrend(days, valueOf) {
const pts = []; const pts = [];
days.forEach((d, i) => { days.forEach((d, i) => {
if (d.excluded) return; if (d.excluded) return;
if (i === days.length - 1) return; // today, still being eaten if (i === days.length - 1) return; // today, still in progress
pts.push({ x: i, y: valueOf(d) }); const y = valueOf(d);
if (y == null) return;
pts.push({ x: i, y });
}); });
// Two points always fit a line perfectly and say nothing; four is the least // Two points always fit a line perfectly and say nothing; four is the least
// that can show a direction rather than a coincidence. // that can show a direction rather than a coincidence.
@@ -2340,7 +2397,7 @@
of: (d) => d.gramsByKind?.[NO_KIND] || 0, of: (d) => d.gramsByKind?.[NO_KIND] || 0,
}); });
} }
for (const s of out) s.trend = foodTrend(days, s.of); for (const s of out) s.trend = dailyTrend(days, s.of);
return out; return out;
} }
@@ -2452,7 +2509,7 @@
// One sentence per kind would grow with the list and bury the answer, so // One sentence per kind would grow with the list and bury the answer, so
// only kinds with something to report get a sentence of their own and the // only kinds with something to report get a sentence of their own and the
// rest are folded into a clause. Both rules are foodTrend's, not new ones: // rest are folded into a clause. Both rules are dailyTrend's, not new ones:
// a kind with too few days has no fit, and one whose move is under its own // a kind with too few days has no fit, and one whose move is under its own
// scatter is not claimed. // scatter is not claimed.
function foodSeriesSentences(series, windowDays) { function foodSeriesSentences(series, windowDays) {
+1
View File
@@ -1,4 +1,5 @@
[ [
{ "date": "2026-09-29", "text": "The Sleep chart on the Sleep tab has a trend line through it, like the Food (grams) chart, so you can see whether he is sleeping less as he grows — the day-to-day bars vary enough to hide a steady change. A line under the chart puts it in figures: “daily sleep is down about 1.5h — from roughly 17h a day to 15.5h”, or, when the days scatter more than they move, that it is roughly steady and what the average is. Today is left out until it's over, as are days marked “not counted” and days with no sleep logged at all, which the sentence mentions so an empty bar isn't mistaken for a night without sleep. It follows the 7 / 14 / 30 day picker like the rest of the charts" },
{ "date": "2026-09-29", "text": "The Timing panel no longer counts the night. The gap from each day's last pee, poo or meal to the next morning's first was being measured like any other, so every window held several eight-hour-plus “gaps” that were really just bedtime — the longest figure was a sleep figure and the typical gap was pulled up with it. Only gaps within a single day are measured now, so the bars show how often he needs to go out while he's up and about. The “since the last one” marker is unchanged, so first thing in the morning it still reads as overdue — which, before the first trip out, it is" }, { "date": "2026-09-29", "text": "The Timing panel no longer counts the night. The gap from each day's last pee, poo or meal to the next morning's first was being measured like any other, so every window held several eight-hour-plus “gaps” that were really just bedtime — the longest figure was a sleep figure and the typical gap was pulled up with it. Only gaps within a single day are measured now, so the bars show how often he needs to go out while he's up and about. The “since the last one” marker is unchanged, so first thing in the morning it still reads as overdue — which, before the first trip out, it is" },
{ "date": "2026-09-26", "text": "Tapping the tab you are already on scrolls back to the top of the page, the way it does in most phone apps — handy after scrolling a long way down Habits or the history. Switching to a different tab still leaves you where you were, as before" }, { "date": "2026-09-26", "text": "Tapping the tab you are already on scrolls back to the top of the page, the way it does in most phone apps — handy after scrolling a long way down Habits or the history. Switching to a different tab still leaves you where you were, as before" },
{ "date": "2026-09-24", "text": "Kinds of food are now created in one place only — Settings → Food kinds. The Ate dialog no longer has a box for inventing one on the spot; it still lets you pick from the kinds you have, exactly as before. Making them mid-meal put a bit of list-keeping in the middle of logging, where the thing you actually came to do is record that the dog ate, and a kind typed in a hurry is easy to get slightly wrong — nothing stops two near-identical kinds existing, and clearing one up afterwards means relabelling every meal logged under it first" }, { "date": "2026-09-24", "text": "Kinds of food are now created in one place only — Settings → Food kinds. The Ate dialog no longer has a box for inventing one on the spot; it still lets you pick from the kinds you have, exactly as before. Making them mid-meal put a bit of list-keeping in the middle of logging, where the thing you actually came to do is record that the dog ate, and a kind typed in a hurry is easy to get slightly wrong — nothing stops two near-identical kinds existing, and clearing one up afterwards means relabelling every meal logged under it first" },
+3 -1
View File
@@ -301,7 +301,9 @@
<h2>Sleep <span class="muted-note" data-chart-days-label>(last 7 days)</span></h2> <h2>Sleep <span class="muted-note" data-chart-days-label>(last 7 days)</span></h2>
<div class="chart"> <div class="chart">
<div class="chart-title">Hours per day</div> <div class="chart-title">Hours per day</div>
<svg id="chart-sleep" class="chart-svg" viewBox="0 0 320 160" role="img" aria-label="Sleep hours per day"></svg> <svg id="chart-sleep" class="chart-svg" viewBox="0 0 320 160" role="img" aria-label="Sleep hours per day, with a trend line"></svg>
<!-- What the trend line says — see renderSleepTrendNote. -->
<p id="sleep-note" class="muted-note" hidden></p>
</div> </div>
</section> </section>
+9
View File
@@ -1369,6 +1369,15 @@ input.switch:checked::after { transform: translateX(18px); }
stroke-linecap: round; stroke-linecap: round;
fill: none; fill: none;
} }
/* The sleep bars are --sleep, so the fit through them takes the ink shade —
darker in light mode, lighter in dark — to stay visible where it crosses one. */
.chart-svg .sleep-trend {
stroke: var(--sleep-ink);
stroke-width: 2;
stroke-dasharray: 5 3;
stroke-linecap: round;
fill: none;
}
.legend .sw.food-sw { background: var(--food-color, var(--muted)); } .legend .sw.food-sw { background: var(--food-color, var(--muted)); }
.kind-field { margin-bottom: 10px; } .kind-field { margin-bottom: 10px; }