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.

Show calendar year in multi-year chart tooltips

Manuel4a614eb8ca5d
diff --git a/frontend/src/components/LineChart.jsx b/frontend/src/components/LineChart.jsx
index c236464e4..e8e59dbaa 100644
--- a/frontend/src/components/LineChart.jsx
+++ b/frontend/src/components/LineChart.jsx
@@ -91,7 +91,10 @@ export default function LineChart({ points, h = 150, unit = '', color = 'var(--a
const poly = pts.map(p => X(p.t).toFixed(1) + ',' + Y(p.y).toFixed(1)).join(' ')
const last = pts[pts.length - 1]
const gid = 'g' + Math.round(t0 % 1e7) + '_' + H
- const hoverPts = (single ? [points[0]] : points).map(p => ({ x: X(p.t), y: Y(p.y), iso: p.d || isoOf(new Date(p.t)), v: p.y, note: p.note }))
+ const hoverSource = single ? [points[0]] : points
+ const hoverDates = hoverSource.map(p => p.d || isoOf(new Date(p.t)))
+ const showYear = new Set(hoverDates.map(iso => new Date(iso + 'T12:00:00').getFullYear())).size > 1
+ const hoverPts = hoverSource.map((p, i) => ({ x: X(p.t), y: Y(p.y), iso: hoverDates[i], v: p.y, note: p.note }))
const marked = points.some(p => p.m != null)
const onMove = e => {
@@ -132,8 +135,8 @@ export default function LineChart({ points, h = 150, unit = '', color = 'var(--a
</g>}
</svg>
{hover && <div className="ctip" ref={tipRef}>
- {fmtDate(hover.iso, true)} · {fmtNum(hover.v)}{unit ? ' ' + unit : ''}{hover.note ? ' · ' + hover.note : ''}
+ {fmtDate(hover.iso, true, showYear)} · {fmtNum(hover.v)}{unit ? ' ' + unit : ''}{hover.note ? ' · ' + hover.note : ''}
</div>}
</div>
)
-}
+}
\ No newline at end of file
diff --git a/frontend/src/components/LineChart.test.jsx b/frontend/src/components/LineChart.test.jsx
new file mode 100644
index 000000000..fe4181004
--- /dev/null
+++ b/frontend/src/components/LineChart.test.jsx
@@ -0,0 +1,74 @@
+// @vitest-environment happy-dom
+import React, { act } from 'react'
+import { createRoot } from 'react-dom/client'
+import { afterEach, beforeEach, describe, expect, it } from 'vitest'
+import LineChart from './LineChart.jsx'
+import { fmtDate, isoOf } from '../lib/format.js'
+
+globalThis.IS_REACT_ACT_ENVIRONMENT = true
+
+let container
+let root
+
+beforeEach(() => {
+ container = document.createElement('div')
+ document.body.appendChild(container)
+ root = createRoot(container)
+})
+
+afterEach(() => {
+ act(() => root.unmount())
+ container.remove()
+})
+
+const point = (year, month, day, y) => ({
+ d: `${year}-${String(month).padStart(2, '0')}-${String(day).padStart(2, '0')}`,
+ t: new Date(year, month - 1, day, 12).getTime(),
+ y
+})
+
+function renderChart(points) {
+ act(() => root.render(<LineChart points={points} axes={false} unit="kg" />))
+}
+
+function hoverAt(clientX) {
+ act(() => {
+ container.querySelector('.chart-i').dispatchEvent(new MouseEvent('mousemove', { bubbles: true, clientX }))
+ })
+ return container.querySelector('.ctip').textContent
+}
+
+describe('LineChart hover date', () => {
+ it('keeps same-year points in the compact format', () => {
+ const first = point(2026, 1, 15, 70)
+ renderChart([first, point(2026, 2, 15, 71)])
+
+ expect(hoverAt(0)).toBe(`${fmtDate(first.d, true)} · 70 kg`)
+ })
+
+ it('includes the calendar year at a short cross-year boundary', () => {
+ const first = point(2025, 11, 30, 70)
+ const last = point(2026, 2, 1, 71)
+ renderChart([first, last])
+
+ expect(hoverAt(0)).toBe(`${fmtDate(first.d, true, true)} · 70 kg`)
+ expect(hoverAt(340)).toBe(`${fmtDate(last.d, true, true)} · 71 kg`)
+ })
+
+ it('keeps a single point compact', () => {
+ const only = point(2026, 7, 4, 70)
+ renderChart([only])
+
+ expect(hoverAt(170)).toBe(`${fmtDate(only.d, true)} · 70 kg`)
+ })
+
+ it('uses timestamp-only points when deciding whether to show the year', () => {
+ const first = point(2025, 12, 31, 70)
+ const last = point(2026, 1, 1, 71)
+ const timestampOnly = [{ t: first.t, y: first.y }, { t: last.t, y: last.y }]
+ renderChart(timestampOnly)
+
+ const lastIso = isoOf(new Date(last.t))
+ expect(hoverAt(340)).toBe(`${fmtDate(lastIso, true, true)} · 71 kg`)
+ })
+})
\ No newline at end of file
diff --git a/frontend/src/lib/format.js b/frontend/src/lib/format.js
index e8eadeba9..cd11e2b12 100644
--- a/frontend/src/lib/format.js
+++ b/frontend/src/lib/format.js
@@ -12,9 +12,11 @@ export const DAYS = ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa']
export const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
export const MONTHS_LONG = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
-export function fmtDate(iso, long) {
+export function fmtDate(iso, long, withYear = false) {
const d = new Date(iso + 'T12:00:00')
- return d.toLocaleDateString(dateLocale(), long ? { weekday: 'short', day: 'numeric', month: 'short' } : { day: 'numeric', month: 'short' })
+ const options = long ? { weekday: 'short', day: 'numeric', month: 'short' } : { day: 'numeric', month: 'short' }
+ if (withYear) options.year = 'numeric'
+ return d.toLocaleDateString(dateLocale(), options)
}
export function fmtDur(ms) {
const m = Math.floor(ms / 60000)
@@ -44,4 +46,4 @@ export function weekKey(d) {
export const localTZ = () => { try { return Intl.DateTimeFormat().resolvedOptions().timeZone || 'UTC' } catch { return 'UTC' } }
export const uid = () => Date.now().toString(36) + Math.random().toString(36).slice(2, 7)
-export const ACCENTS = { lime: '#30d158', sky: '#0a84ff', orange: '#ff9f0a', violet: '#bf5af2', pink: '#ff375f', red: '#ff453a', teal: '#40c8e0', gold: '#ffd60a' }
+export const ACCENTS = { lime: '#30d158', sky: '#0a84ff', orange: '#ff9f0a', violet: '#bf5af2', pink: '#ff375f', red: '#ff453a', teal: '#40c8e0', gold: '#ffd60a' }
\ No newline at end of file