Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

fix(charts): x-axis labels never repeat a date or a month

lodepav75689185aeaf
diff --git a/frontend/src/components/LineChart.jsx b/frontend/src/components/LineChart.jsx
index 546b4e418..2e9096875 100644
--- a/frontend/src/components/LineChart.jsx
+++ b/frontend/src/components/LineChart.jsx
@@ -85,10 +85,20 @@ export default function LineChart({ points, h = 150, unit = '', color = 'var(--a
let m = new Date(d0.getFullYear(), d0.getMonth() + 1, 1)
while (m <= d1) { ticks.push({ t: +m, txt: t(MONTHS[m.getMonth()]) }); m = new Date(m.getFullYear(), m.getMonth() + 1, 1) }
if (ticks.length === 0 && !single) {
+ // Start, middle and end. A span of a day or two gives the same date twice ("16 Oct | 17 Oct |
+ // 17 Oct"), so each date is written once: the end kept over a middle that reads the same,
+ // and a span inside one day gets one date, in the middle.
for (let i = 0; i <= 2; i++) {
const tv = t0 + (t1 - t0) * i / 2, dd = new Date(tv)
- ticks.push({ t: tv, txt: dd.getDate() + ' ' + t(MONTHS[dd.getMonth()]), anchor: i === 0 ? 'start' : i === 2 ? 'end' : 'middle' })
+ const tk = { t: tv, txt: dd.getDate() + ' ' + t(MONTHS[dd.getMonth()]), anchor: i === 0 ? 'start' : i === 2 ? 'end' : 'middle' }
+ if (ticks.length && ticks[ticks.length - 1].txt === tk.txt) { if (i === 2) ticks[ticks.length - 1] = tk; continue }
+ ticks.push(tk)
}
+ if (ticks.length === 1) ticks[0] = { ...ticks[0], t: (t0 + t1) / 2, anchor: 'middle' }
+ } else if (t1 - t0 > 366 * 864e5) {
+ // Over more than a year a month name comes round again ("Apr | Jul | Oct | Jan | Apr"), so
+ // each month says its year: "Apr '25".
+ for (const tk of ticks) tk.txt += " '" + String(new Date(tk.t).getFullYear()).slice(2)
}
const every = Math.max(1, Math.ceil(ticks.length / 7))
ticks.forEach((tk, i) => {
diff --git a/frontend/src/components/LineChart.test.jsx b/frontend/src/components/LineChart.test.jsx
index 4df9b4453..a3e82256a 100644
--- a/frontend/src/components/LineChart.test.jsx
+++ b/frontend/src/components/LineChart.test.jsx
@@ -111,3 +111,32 @@ describe('LineChart y axis', () => {
expect(labels.some(l => /^[-\u2212]0$/.test(l))).toBe(false)
})
})
+
+describe('LineChart x axis', () => {
+ // The date labels under the plot, left to right (the y labels sit at the left edge, end-anchored).
+ const xLabels = (points, h = 150) => {
+ act(() => root.render(<LineChart points={points} h={h} axes unit="kg" />))
+ return [...container.querySelectorAll('text')].filter(n => n.getAttribute('y') === String(h - 7)).map(n => n.textContent)
+ }
+ const at = (year, month, day, hour, y) => ({ t: new Date(year, month - 1, day, hour).getTime(), y })
+
+ it('writes each date once over a span of two days', () => {
+ expect(xLabels([at(2026, 10, 16, 10, 50), at(2026, 10, 17, 18, 60)])).toEqual(['16 Oct', '17 Oct'])
+ })
+
+ it('writes one date for a span inside one day', () => {
+ expect(xLabels([at(2026, 10, 16, 9, 50), at(2026, 10, 16, 19, 60)])).toEqual(['16 Oct'])
+ })
+
+ it('keeps three dates when they differ', () => {
+ expect(xLabels([at(2026, 10, 1, 12, 50), at(2026, 10, 15, 12, 60)])).toEqual(['1 Oct', '8 Oct', '15 Oct'])
+ })
+
+ it('gives the months their year over more than a year, and not within one', () => {
+ const long = xLabels([at(2025, 3, 18, 12, 50), at(2026, 10, 17, 12, 60)])
+ expect(long[0]).toBe("Apr '25")
+ expect(long.at(-1)).toBe("Oct '26")
+ expect(new Set(long).size).toBe(long.length)
+ expect(xLabels([at(2025, 10, 20, 12, 50), at(2026, 9, 30, 12, 60)]).every(l => !l.includes("'"))).toBe(true)
+ })
+})