Skip to content

Open-source personal finance manager. Self-hosted, privacy-first.

fix(reports): focus net worth trend scale (#1101)

eilandnl8826aa158140
diff --git a/frontend/src/lib/net-worth-chart-utils.test.ts b/frontend/src/lib/net-worth-chart-utils.test.ts
new file mode 100644
index 00000000..f4e8852a
--- /dev/null
+++ b/frontend/src/lib/net-worth-chart-utils.test.ts
@@ -0,0 +1,62 @@
+import { describe, expect, it } from 'vitest'
+import {
+ formatNetWorthAxisTick,
+ getNetWorthDomain,
+ shouldFillNetWorthArea,
+} from './net-worth-chart-utils'
+
+describe('getNetWorthDomain', () => {
+ it('rounds bounds to readable values for ordinary net worth ranges', () => {
+ expect(getNetWorthDomain([23_000, 203_000])).toEqual([0, 250_000])
+ })
+
+ it('keeps the scale close to a narrow range at a high balance', () => {
+ expect(getNetWorthDomain([999_990, 1_000_110])).toEqual([999_960, 1_000_140])
+ })
+
+ it('keeps zero in range when positive values are close to zero', () => {
+ expect(getNetWorthDomain([2, 100])).toEqual([0, 120])
+ })
+
+ it('adds padding around negative and positive values', () => {
+ expect(getNetWorthDomain([-100, 100])).toEqual([-150, 150])
+ })
+
+ it('pads a constant series so the chart still has a usable scale', () => {
+ expect(getNetWorthDomain([1_000_000, 1_000_000])).toEqual([950_000, 1_050_000])
+ })
+
+ it('falls back to a valid range when there are no finite values', () => {
+ expect(getNetWorthDomain([Number.NaN, Number.POSITIVE_INFINITY])).toEqual([0, 1])
+ })
+
+ it('fills only when the chart domain includes zero', () => {
+ expect(shouldFillNetWorthArea([0, 250_000])).toBe(true)
+ expect(shouldFillNetWorthArea([950_000, 1_050_000])).toBe(false)
+ expect(shouldFillNetWorthArea([-150, -50])).toBe(false)
+ })
+})
+
+describe('formatNetWorthAxisTick', () => {
+ it('keeps adjacent high-balance ticks distinguishable', () => {
+ const domain: [number, number] = [999_960, 1_000_120]
+ expect(formatNetWorthAxisTick(1_000_000, domain)).toBe('$1,000,000')
+ expect(formatNetWorthAxisTick(1_000_040, domain)).toBe('$1,000,040')
+ })
+
+ it('preserves cents when the tick interval is below one currency unit', () => {
+ const domain: [number, number] = [1.005, 1.045]
+ expect(formatNetWorthAxisTick(1.01, domain)).toBe('$1.01')
+ expect(formatNetWorthAxisTick(1.04, domain)).toBe('$1.04')
+ })
+
+ it('distinguishes irregular sub-cent ticks on a narrow currency scale', () => {
+ const domain: [number, number] = [1, 1.05]
+ expect(formatNetWorthAxisTick(1.045, domain)).toBe('$1.045')
+ expect(formatNetWorthAxisTick(1.05, domain)).toBe('$1.05')
+ })
+
+ it('uses compact currency labels when the scale supports them', () => {
+ expect(formatNetWorthAxisTick(46_500, [45_000, 52_000])).toBe('$46.5K')
+ })
+})
diff --git a/frontend/src/lib/net-worth-chart-utils.ts b/frontend/src/lib/net-worth-chart-utils.ts
new file mode 100644
index 00000000..fb890fcc
--- /dev/null
+++ b/frontend/src/lib/net-worth-chart-utils.ts
@@ -0,0 +1,66 @@
+function roundToNiceStep(value: number): number {
+ const power = 10 ** Math.floor(Math.log10(value))
+ const fraction = value / power
+ const niceFraction = fraction < 1.5 ? 1 : fraction < 3.5 ? 2 : fraction < 7.5 ? 5 : 10
+ return niceFraction * power
+}
+
+export function getNetWorthDomain(values: readonly number[]): [number, number] {
+ const finiteValues = values.filter(Number.isFinite)
+ if (finiteValues.length === 0) return [0, 1]
+
+ const min = Math.min(...finiteValues)
+ const max = Math.max(...finiteValues)
+ const range = max - min
+ const magnitude = Math.max(Math.abs(min), Math.abs(max))
+ const padding = range > 0 ? range * 0.1 : Math.max(magnitude * 0.005, 1)
+ const step = roundToNiceStep(range > 0 ? range / 4 : Math.max(padding, magnitude * 0.05))
+ const paddedMin = min - padding
+ const lower = min >= 0 && paddedMin < 0 ? 0 : paddedMin
+ const upper = max + padding
+
+ return [Math.floor(lower / step) * step, Math.ceil(upper / step) * step]
+}
+
+export function formatNetWorthAxisTick(
+ value: number,
+ domain: readonly [number, number],
+ currency = 'USD',
+ locale = 'en-US',
+): string {
+ if (value === 0) return '0'
+
+ const tickInterval = (domain[1] - domain[0]) / 4
+ const magnitude = Math.abs(value)
+ const compactUnit = magnitude >= 1_000_000 ? 1_000_000 : magnitude >= 1_000 ? 1_000 : 1
+ if (tickInterval < compactUnit / 10) {
+ const currencyFormat = new Intl.NumberFormat(locale, { style: 'currency', currency })
+ const currencyDigits = currencyFormat.resolvedOptions().maximumFractionDigits ?? 2
+ const intervalDigits = Math.max(
+ 0,
+ Math.ceil(-Math.log10(Math.max(tickInterval / 2, Number.EPSILON))),
+ )
+ const fractionDigits = Math.min(
+ 6,
+ tickInterval < 1 ? Math.max(currencyDigits, intervalDigits) : Math.min(currencyDigits, intervalDigits),
+ )
+ const minimumFractionDigits = currencyFormat.resolvedOptions().minimumFractionDigits ?? 0
+ return new Intl.NumberFormat(locale, {
+ style: 'currency',
+ currency,
+ minimumFractionDigits: Math.min(minimumFractionDigits, fractionDigits),
+ maximumFractionDigits: fractionDigits,
+ }).format(value)
+ }
+
+ return new Intl.NumberFormat(locale, {
+ style: 'currency',
+ currency,
+ notation: 'compact',
+ maximumFractionDigits: 1,
+ }).format(value)
+}
+
+export function shouldFillNetWorthArea(domain: readonly [number, number]): boolean {
+ return domain[0] <= 0 && domain[1] >= 0
+}
diff --git a/frontend/src/pages/reports.tsx b/frontend/src/pages/reports.tsx
index 8b050986..519fe627 100644
--- a/frontend/src/pages/reports.tsx
+++ b/frontend/src/pages/reports.tsx
@@ -34,6 +34,11 @@ import type { ReportResponse, CategoryTrendItem } from '@/types'
import { formatCurrency } from '@/lib/format'
import { localDateString, todayInTimezone } from '@/lib/date-utils'
import { useEffectiveTimezone } from '@/hooks/use-timezone'
+import {
+ formatNetWorthAxisTick,
+ getNetWorthDomain,
+ shouldFillNetWorthArea,
+} from '@/lib/net-worth-chart-utils'
// A small qualitative palette of well-separated hues for the composition
// detail ring. Capped to a handful of slices, distinct colours make each
@@ -341,6 +346,11 @@ export default function ReportsPage() {
...breakdowns,
} as Record<string, string | number | null>
})
+ const netWorthDomain = getNetWorthDomain(
+ chartData
+ .map((point) => point.value)
+ .filter((value): value is number => typeof value === 'number'),
+ )
// Cash-flow runway: the first projected day below zero and the lowest
// point of the horizon, both computed day by day on the backend.
@@ -1084,15 +1094,15 @@ export default function ReportsPage() {
interval="preserveStartEnd"
/>
<YAxis
+ domain={netWorthDomain}
tickFormatter={(v) => {
if (privacyMode) return ''
- if (v === 0) return '0'
- return formatCompact(v, userCurrency, locale)
+ return formatNetWorthAxisTick(v, netWorthDomain, userCurrency, locale)
}}
tick={{ fontSize: 10, fill: 'var(--muted-foreground)' }}
axisLine={false}
tickLine={false}
- width={64}
+ width={88}
tickCount={5}
/>
<Tooltip
@@ -1125,7 +1135,7 @@ export default function ReportsPage() {
dataKey="value"
stroke="#6366F1"
strokeWidth={2.5}
- fill="url(#netWorthGrad)"
+ fill={shouldFillNetWorthArea(netWorthDomain) ? 'url(#netWorthGrad)' : 'none'}
dot={false}
activeDot={{ r: 4, fill: '#6366F1' }}
/>