Add a trend line to the Sleep chart
This commit is contained in:
1 parent
50947d077f
commit
5a419b64dd
4 files changed
+77
-8
No files matched your search
+64
-7
@@ -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);
|
||||
}
|
||||
|
||||
// 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) {
|
||||
const svg = document.getElementById("chart-counts");
|
||||
const W = 320, H = 180;
|
||||
@@ -2150,14 +2205,16 @@
|
||||
// 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
|
||||
// implies it knows about the ones it skipped.
|
||||
// valueOf picks which figure to fit: the day total by default, or one
|
||||
// kind's share of it when the bars are split.
|
||||
function foodTrend(days, valueOf = (d) => d.grams) {
|
||||
// valueOf picks which figure to fit: one food kind's grams, or the day's
|
||||
// sleep hours. It may return null to leave a day out of the fit altogether.
|
||||
function dailyTrend(days, valueOf) {
|
||||
const pts = [];
|
||||
days.forEach((d, i) => {
|
||||
if (d.excluded) return;
|
||||
if (i === days.length - 1) return; // today, still being eaten
|
||||
pts.push({ x: i, y: valueOf(d) });
|
||||
if (i === days.length - 1) return; // today, still in progress
|
||||
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
|
||||
// that can show a direction rather than a coincidence.
|
||||
@@ -2340,7 +2397,7 @@
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -2452,7 +2509,7 @@
|
||||
|
||||
// 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
|
||||
// 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
|
||||
// scatter is not claimed.
|
||||
function foodSeriesSentences(series, windowDays) {
|
||||
|
||||
Reference in new issue
Block a user