Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

fix: add compact formatting to Y axis numbers in db charts (#43170)

Jordi Enric785625c92f06
diff --git a/apps/studio/components/ui/Charts/Charts.utils.tsx b/apps/studio/components/ui/Charts/Charts.utils.tsx
index 152fe2466c7..42cdbb3b97f 100644
--- a/apps/studio/components/ui/Charts/Charts.utils.tsx
+++ b/apps/studio/components/ui/Charts/Charts.utils.tsx
@@ -58,6 +58,21 @@ export const precisionFormatter = (num: number, precision: number): string => {
}
}
+/**
+ * Formats a number compactly for Y-axis ticks by abbreviating large values.
+ * Prevents long numbers like 1,000,000 from overflowing the Y-axis width.
+ *
+ * @example
+ * compactNumberFormatter(999) // "999"
+ * compactNumberFormatter(1000) // "1K"
+ * compactNumberFormatter(1500) // "1.5K"
+ * compactNumberFormatter(1000000) // "1M"
+ * compactNumberFormatter(2500000) // "2.5M"
+ */
+export const compactNumberFormatter = (num: number): string => {
+ return new Intl.NumberFormat('en', { notation: 'compact', maximumFractionDigits: 1 }).format(num)
+}
+
/**
* Formats a percentage, trimming decimals at 100.
*
diff --git a/apps/studio/data/reports/database-charts.ts b/apps/studio/data/reports/database-charts.ts
index 77e096470eb..0b51863d731 100644
--- a/apps/studio/data/reports/database-charts.ts
+++ b/apps/studio/data/reports/database-charts.ts
@@ -1,8 +1,9 @@
-import { numberFormatter } from 'components/ui/Charts/Charts.utils'
+import { compactNumberFormatter, numberFormatter } from 'components/ui/Charts/Charts.utils'
import { ReportAttributes } from 'components/ui/Charts/ComposedChart.utils'
import { DOCS_URL } from 'lib/constants'
import { formatBytes } from 'lib/helpers'
import type { Organization } from 'types'
+
import { DiskAttributesData } from '../config/disk-attributes-query'
import { MaxConnectionsData } from '../database/max-connections-query'
import { Project } from '../projects/project-detail-query'
@@ -148,8 +149,8 @@ export const getReportAttributesV2: (
showGrid: true,
showMaxValue: true,
YAxisProps: {
- width: 35,
- tickFormatter: (value: any) => numberFormatter(value, 0),
+ width: 50,
+ tickFormatter: (value: any) => compactNumberFormatter(value),
},
defaultChartStyle: 'bar',
attributes: [
diff --git a/apps/studio/tests/components/ui/Charts/Charts.utils.test.ts b/apps/studio/tests/components/ui/Charts/Charts.utils.test.ts
index 135d445f93b..f059ba53b24 100644
--- a/apps/studio/tests/components/ui/Charts/Charts.utils.test.ts
+++ b/apps/studio/tests/components/ui/Charts/Charts.utils.test.ts
@@ -1,5 +1,6 @@
import { renderHook } from '@testing-library/react'
import {
+ compactNumberFormatter,
formatPercentage,
isFloat,
numberFormatter,
@@ -105,6 +106,37 @@ describe('formatPercentage', () => {
})
})
+describe('compactNumberFormatter', () => {
+ it('returns the number as-is below 1000', () => {
+ expect(compactNumberFormatter(0)).toBe('0')
+ expect(compactNumberFormatter(1)).toBe('1')
+ expect(compactNumberFormatter(999)).toBe('999')
+ })
+
+ it('formats thousands with K suffix', () => {
+ expect(compactNumberFormatter(1000)).toBe('1K')
+ expect(compactNumberFormatter(1500)).toBe('1.5K')
+ expect(compactNumberFormatter(64000)).toBe('64K')
+ expect(compactNumberFormatter(999999)).toBe('1M') // rounds up
+ })
+
+ it('formats millions with M suffix', () => {
+ expect(compactNumberFormatter(1_000_000)).toBe('1M')
+ expect(compactNumberFormatter(1_500_000)).toBe('1.5M')
+ expect(compactNumberFormatter(2_500_000)).toBe('2.5M')
+ })
+
+ it('formats billions with B suffix', () => {
+ expect(compactNumberFormatter(1_000_000_000)).toBe('1B')
+ expect(compactNumberFormatter(2_500_000_000)).toBe('2.5B')
+ })
+
+ it('handles negative numbers', () => {
+ expect(compactNumberFormatter(-1000)).toBe('-1K')
+ expect(compactNumberFormatter(-1_500_000)).toBe('-1.5M')
+ })
+})
+
test('useStacked', () => {
const { result } = renderHook(() =>
useStacked({