Use 1-hour gridlines on the daily Sleep bar chart

niceAxisSleepHours drops from 2-hour to 1-hour steps and is shared with
the trend chart again (the trend-only helper it duplicated is gone).
Labels thin to every other gridline past 12 steps — this chart's plot
area is shorter than the trend's.
This commit is contained in:
Alexander Heldt
2026-07-15 10:56:16 +00:00
parent ba6e2c5e7d
commit 83af641a12
2 changed files with 12 additions and 14 deletions
+10 -13
View File
@@ -941,12 +941,12 @@
return { yMax, steps: Math.max(1, Math.round(yMax / step)) }; return { yMax, steps: Math.max(1, Math.round(yMax / step)) };
} }
// Sleep-specific axis: always 2-hour granularity, capped at 24h/day, // Sleep-specific axis: 1-hour granularity, capped at 24h/day. Shared by the
// for a more readable picture of typical 1018 h puppy sleep. // sleep bars and the trend chart; both thin out labels when steps get dense.
function niceAxisSleepHours(rawMax) { function niceAxisSleepHours(rawMax) {
if (!(rawMax > 0)) return { yMax: 2, steps: 2 }; if (!(rawMax > 0)) return { yMax: 2, steps: 2 };
const m = Math.min(24, Math.max(2, Math.ceil(rawMax / 2) * 2)); const m = Math.min(24, Math.max(2, Math.ceil(rawMax)));
return { yMax: m, steps: m / 2 }; return { yMax: m, steps: m };
} }
function escapeText(s) { function escapeText(s) {
@@ -980,14 +980,19 @@
const gap = days.length > 14 ? 2 : 4; const gap = days.length > 14 ? 2 : 4;
const barW = (innerW - (days.length - 1) * gap) / days.length; const barW = (innerW - (days.length - 1) * gap) / days.length;
// Every gridline gets a label unless that would crowd the 9px text
// (this chart's plot area is shorter than the trend chart's).
const labelEvery = ySteps > 12 ? 2 : 1;
const parts = []; const parts = [];
for (let i = 0; i <= ySteps; i++) { for (let i = 0; i <= ySteps; i++) {
const y = MT + innerH * (1 - i / ySteps); const y = MT + innerH * (1 - i / ySteps);
const v = Math.round(yMax * i / ySteps * 10) / 10; const v = Math.round(yMax * i / ySteps * 10) / 10;
const vText = v % 1 === 0 ? v : v.toFixed(1); const vText = v % 1 === 0 ? v : v.toFixed(1);
parts.push(`<line class="grid" x1="${ML}" y1="${y}" x2="${W - MR}" y2="${y}"/>`); parts.push(`<line class="grid" x1="${ML}" y1="${y}" x2="${W - MR}" y2="${y}"/>`);
if (i % labelEvery === 0) {
parts.push(`<text x="${ML - 4}" y="${y + 3}" text-anchor="end">${vText}h</text>`); parts.push(`<text x="${ML - 4}" y="${y + 3}" text-anchor="end">${vText}h</text>`);
} }
}
const selYmd = ymd(selectedDay()); const selYmd = ymd(selectedDay());
days.forEach((d, i) => { days.forEach((d, i) => {
@@ -1341,14 +1346,6 @@
return { today, yesterday, avg, avgDays, projected }; return { today, yesterday, avg, avgDays, projected };
} }
// Trend-specific y-axis: 1-hour gridlines (vs the bar charts' 2-hour steps)
// so nearby curves separate visually; still capped at 24h.
function niceAxisTrendHours(rawMax) {
if (!(rawMax > 0)) return { yMax: 2, steps: 2 };
const m = Math.min(24, Math.max(2, Math.ceil(rawMax)));
return { yMax: m, steps: m };
}
function drawSleepTrendChart(curves) { function drawSleepTrendChart(curves) {
const svg = document.getElementById("chart-sleep-trend"); const svg = document.getElementById("chart-sleep-trend");
if (!svg) return; if (!svg) return;
@@ -1366,7 +1363,7 @@
].filter(s => s.pts && s.pts.length > 1); ].filter(s => s.pts && s.pts.length > 1);
const rawMax = Math.max(...series.flatMap(s => s.pts.map(p => p.y))); const rawMax = Math.max(...series.flatMap(s => s.pts.map(p => p.y)));
const { yMax, steps: ySteps } = niceAxisTrendHours(rawMax); const { yMax, steps: ySteps } = niceAxisSleepHours(rawMax);
const xOf = (hour) => ML + (hour / 24) * innerW; const xOf = (hour) => ML + (hour / 24) * innerW;
const yOf = (v) => MT + innerH * (1 - v / yMax); const yOf = (v) => MT + innerH * (1 - v / yMax);
+1
View File
@@ -1,4 +1,5 @@
[ [
{ "date": "2026-07-15", "text": "The Sleep chart in the last-N-days card now has 1-hour gridlines too" },
{ "date": "2026-07-15", "text": "The Sleep trend chart is taller with 1-hour gridlines, so nearby lines are easier to tell apart" }, { "date": "2026-07-15", "text": "The Sleep trend chart is taller with 1-hour gridlines, so nearby lines are easier to tell apart" },
{ "date": "2026-07-15", "text": "The Sleep trend legend shows the hours for each line — Today, Yesterday and the average, next to the projection" }, { "date": "2026-07-15", "text": "The Sleep trend legend shows the hours for each line — Today, Yesterday and the average, next to the projection" },
{ "date": "2026-07-15", "text": "The Sleep trend chart projects where today will land by midnight — a dashed tail continues today's line following the average day's rhythm" }, { "date": "2026-07-15", "text": "The Sleep trend chart projects where today will land by midnight — a dashed tail continues today's line following the average day's rhythm" },