Skip to content

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

fix(charts): keep the sign on a balance chart's axis labels (#1322)

Tássio Noronha5bfe2aec9a14
diff --git a/frontend/src/lib/format.test.ts b/frontend/src/lib/format.test.ts
index a2ae452e..4220def4 100644
--- a/frontend/src/lib/format.test.ts
+++ b/frontend/src/lib/format.test.ts
@@ -2,6 +2,7 @@ import { describe, expect, it } from 'vitest'
import {
formatAmountInput,
+ formatCompactCurrency,
formatCurrency,
formatSignedAmount,
parseAmountInput,
@@ -261,3 +262,27 @@ describe('formatAmountInput', () => {
expect(formatAmountInput(1.5, 'not a locale')).toBe('1.50')
})
})
+
+describe('formatCompactCurrency', () => {
+ it('keeps a six-figure amount short enough for a chart tick', () => {
+ // The full form is "-R$340,000.00": far past the width an axis tick gets,
+ // which is what clipped the minus sign off a loan's balance chart.
+ const compact = normalize(formatCompactCurrency(-340000, 'BRL', 'en-US'))
+ expect(compact.length).toBeLessThan('-R$340,000.00'.length)
+ expect(compact.startsWith('-')).toBe(true)
+ })
+
+ it('keeps the sign on every magnitude', () => {
+ for (const value of [-1_500_000, -340_000, -1_200, -5]) {
+ expect(normalize(formatCompactCurrency(value, 'USD', 'en-US')).startsWith('-')).toBe(true)
+ }
+ })
+
+ it('renders a null value as the empty-value glyph', () => {
+ expect(formatCompactCurrency(null)).toBe('\u2014')
+ })
+
+ it('falls back to a dollar format on a malformed locale', () => {
+ expect(normalize(formatCompactCurrency(1500, 'USD', 'not a locale'))).toBe('$1.5K')
+ })
+})
diff --git a/frontend/src/lib/format.ts b/frontend/src/lib/format.ts
index 5ed00f80..7a1649b5 100644
--- a/frontend/src/lib/format.ts
+++ b/frontend/src/lib/format.ts
@@ -175,6 +175,20 @@ function currencyFormatter(currency: string, locale: string): Intl.NumberFormat
}
}
+function compactCurrencyFormatter(currency: string, locale: string): Intl.NumberFormat {
+ const options: Intl.NumberFormatOptions = {
+ style: 'currency',
+ currency: currency || 'USD',
+ notation: 'compact',
+ maximumFractionDigits: 1,
+ }
+ try {
+ return new Intl.NumberFormat(locale, options)
+ } catch {
+ return new Intl.NumberFormat('en-US', { ...options, currency: 'USD' })
+ }
+}
+
/**
* Format a numeric value as currency using Intl.NumberFormat.
*
@@ -195,6 +209,24 @@ export function formatCurrency(
return formatter.format(Math.round(value * factor) === 0 ? 0 : value)
}
+/**
+ * Format a value as currency in compact notation ("-R$340 mil", "$1.2M").
+ *
+ * Chart axes give a tick a fixed width, and a full amount outgrows it as soon
+ * as the balance reaches six figures: the label then renders past the axis and
+ * the browser clips its leading characters, which costs a negative balance its
+ * minus sign. Compact notation keeps the tick inside its width at any
+ * magnitude, and the tooltip still carries the exact amount.
+ */
+export function formatCompactCurrency(
+ value: number | null | undefined,
+ currency = 'USD',
+ locale = 'en-US',
+): string {
+ if (value == null) return '\u2014'
+ return compactCurrencyFormatter(currency, locale).format(value)
+}
+
/**
* Format a transaction amount with its direction: `+` for a credit, `-` for
* a debit, in front of the magnitude. Amounts that round to zero at the
diff --git a/frontend/src/pages/account-detail-running-balance.test.tsx b/frontend/src/pages/account-detail-running-balance.test.tsx
index 1e4d3ef5..7b4e499c 100644
--- a/frontend/src/pages/account-detail-running-balance.test.tsx
+++ b/frontend/src/pages/account-detail-running-balance.test.tsx
@@ -177,7 +177,8 @@ describe('a non-credit-card account with more rows than one page', () => {
await renderPage()
- expect(await screen.findByText('R$20,000')).toBeInTheDocument()
+ // The axis ticks are compact, so the series shows up as "R$20.0K".
+ expect(await screen.findByText('R$20.0K')).toBeInTheDocument()
expect(api.accounts.runningBalance).toHaveBeenCalledWith('acc-1', '2026-09-01', '2026-09-30')
})
})
diff --git a/frontend/src/pages/account-detail.tsx b/frontend/src/pages/account-detail.tsx
index b489e67a..d556dda5 100644
--- a/frontend/src/pages/account-detail.tsx
+++ b/frontend/src/pages/account-detail.tsx
@@ -35,7 +35,7 @@ import { useAuth } from '@/contexts/auth-context'
import { useWorkspace } from '@/contexts/workspace-context'
import { useCreateTransaction } from '@/hooks/use-create-transaction'
import { resolveDateFnsLocale } from '@/lib/date-fns-locale'
-import { formatCurrency } from '@/lib/format'
+import { formatCompactCurrency, formatCurrency } from '@/lib/format'
import { LoanScheduleCard } from '@/components/loan-schedule-card'
import {
AreaChart,
@@ -1548,12 +1548,12 @@ export default function AccountDetailPage() {
tickFormatter={(v) => {
if (privacyMode) return ''
if (v === 0) return '0'
- return formatCurrency(v, displayCurrency, locale).replace(/,00$/, '').replace(/\.00$/, '')
+ return formatCompactCurrency(v, displayCurrency, locale)
}}
tick={{ fontSize: 10, fill: 'var(--muted-foreground)' }}
axisLine={false}
tickLine={false}
- width={56}
+ width={64}
tickCount={5}
domain={[
(dataMin: number) => dataMin < 0 ? Math.floor(dataMin / 100) * 100 : 0,
diff --git a/frontend/src/pages/dashboard.tsx b/frontend/src/pages/dashboard.tsx
index 8a61f580..59aca1d8 100644
--- a/frontend/src/pages/dashboard.tsx
+++ b/frontend/src/pages/dashboard.tsx
@@ -57,7 +57,7 @@ import { useAuth } from '@/contexts/auth-context'
import { useCollectionFilter } from '@/contexts/collection-filter-context'
import { resolveDateFnsLocale } from '@/lib/date-fns-locale'
import type { Rule, Transaction } from '@/types'
-import { formatCurrency } from '@/lib/format'
+import { formatCompactCurrency, formatCurrency } from '@/lib/format'
import { shouldShowPendingBadge } from '@/lib/transaction-status'
function formatDate(dateStr: string, locale = 'pt-BR') {
@@ -1270,12 +1270,12 @@ export default function DashboardPage() {
tickFormatter={(v) => {
if (privacyMode) return ''
if (v === 0) return '0'
- return formatCurrency(v, userCurrency, locale).replace(/,00$/, '').replace(/\.00$/, '')
+ return formatCompactCurrency(v, userCurrency, locale)
}}
tick={{ fontSize: 10, fill: 'var(--muted-foreground)' }}
axisLine={false}
tickLine={false}
- width={56}
+ width={64}
tickCount={5}
domain={[
(dataMin: number) => Math.min(0, Math.floor(dataMin / 100) * 100),
diff --git a/frontend/src/pages/reports.tsx b/frontend/src/pages/reports.tsx
index 39672a73..938e9d4a 100644
--- a/frontend/src/pages/reports.tsx
+++ b/frontend/src/pages/reports.tsx
@@ -31,7 +31,7 @@ import { usePrivacyMode } from '@/hooks/use-privacy-mode'
import { useAuth } from '@/contexts/auth-context'
import { useCollectionFilter } from '@/contexts/collection-filter-context'
import type { ReportResponse, CategoryTrendItem } from '@/types'
-import { formatCurrency } from '@/lib/format'
+import { formatCompactCurrency, formatCurrency } from '@/lib/format'
import { localDateString, todayInTimezone } from '@/lib/date-utils'
import { monthLabel } from '@/lib/month-utils'
import { periodWindow, windowMonths, type PeriodPreset } from '@/lib/report-period'
@@ -63,15 +63,6 @@ const SLICE_COLORS = [
]
const OTHER_SLICE_COLOR = '#9CA3AF'
-function formatCompact(value: number, currency = 'USD', locale = 'en-US') {
- return new Intl.NumberFormat(locale, {
- style: 'currency',
- currency,
- notation: 'compact',
- maximumFractionDigits: 1,
- }).format(value)
-}
-
/** "Apr 2026" from a YYYY-MM-DD date, in the date locale. */
function formatShortMonth(isoDate: string, locale: string) {
const [y, m] = isoDate.split('-').map(Number)
@@ -1108,7 +1099,7 @@ export default function ReportsPage() {
tickFormatter={(v) => {
if (privacyMode) return ''
if (v === 0) return '0'
- return formatCompact(v, userCurrency, locale)
+ return formatCompactCurrency(v, userCurrency, locale)
}}
tick={{ fontSize: 10, fill: 'var(--muted-foreground)' }}
axisLine={false}
@@ -1208,7 +1199,7 @@ export default function ReportsPage() {
tickFormatter={(v) => {
if (privacyMode) return ''
if (v === 0) return '0'
- return formatCompact(v, userCurrency, locale)
+ return formatCompactCurrency(v, userCurrency, locale)
}}
tick={{ fontSize: 10, fill: 'var(--muted-foreground)' }}
axisLine={false}
@@ -1459,7 +1450,7 @@ export default function ReportsPage() {
: t(currentTab.labelKey)}
</span>
<span className="text-base font-bold text-foreground tabular-nums">
- {mask(formatCompact(
+ {mask(formatCompactCurrency(
snapshotTrendPoint
? compositionView === 'netWorth' || compositionView === 'net' || !compositionView
? snapshotTrendPoint.value
@@ -1668,7 +1659,7 @@ export default function ReportsPage() {
</span>
</div>
<p className="text-xs font-bold tabular-nums text-foreground mb-1">
- {mask(formatCompact(item.total, userCurrency, locale))}
+ {mask(formatCompactCurrency(item.total, userCurrency, locale))}
</p>
<div style={{ height: 48 }}>
<ResponsiveContainer width="100%" height="100%">
@@ -1739,7 +1730,7 @@ export default function ReportsPage() {
tickFormatter={(v) => {
if (privacyMode) return ''
if (v === 0) return '0'
- return formatCompact(v, userCurrency, locale)
+ return formatCompactCurrency(v, userCurrency, locale)
}}
tick={{ fontSize: 10, fill: 'var(--muted-foreground)' }}
axisLine={false}