Skip to content

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

minor percentage formatting fixes (#41923)

Jordi Enriceb85515acbea
diff --git a/apps/studio/components/ui/Charts/ChartHeader.tsx b/apps/studio/components/ui/Charts/ChartHeader.tsx
index 4910b8d1711..e38f28358f8 100644
--- a/apps/studio/components/ui/Charts/ChartHeader.tsx
+++ b/apps/studio/components/ui/Charts/ChartHeader.tsx
@@ -14,7 +14,7 @@ import { cn } from 'ui'
import { useParams } from 'common'
import { ButtonTooltip } from 'components/ui/ButtonTooltip'
import { formatBytes } from 'lib/helpers'
-import { numberFormatter } from './Charts.utils'
+import { formatPercentage, numberFormatter } from './Charts.utils'
import { useChartHoverState } from './useChartHoverState'
import { InfoTooltip } from 'ui-patterns/info-tooltip'
import { Badge } from 'ui'
@@ -102,6 +102,10 @@ export const ChartHeader = ({
return formatBytes(bytesValue, valuePrecision)
}
+ if (format === '%') {
+ return formatPercentage(value, valuePrecision)
+ }
+
const formattedValue = numberFormatter(value, valuePrecision)
if (typeof format === 'string' && format) {
diff --git a/apps/studio/components/ui/Charts/Charts.utils.tsx b/apps/studio/components/ui/Charts/Charts.utils.tsx
index ae0a84b78f7..152fe2466c7 100644
--- a/apps/studio/components/ui/Charts/Charts.utils.tsx
+++ b/apps/studio/components/ui/Charts/Charts.utils.tsx
@@ -13,8 +13,9 @@ import type { CommonChartProps, StackedChartProps } from './Charts.types'
* numberFormatter(123.123) // "123.12"
* numberFormatter(123, 2) // "123.00"
*/
-export const numberFormatter = (num: number, precision = 2) =>
- isFloat(num) ? precisionFormatter(num, precision) : num.toLocaleString()
+export const numberFormatter = (num: number, precision = 2) => {
+ return isFloat(num) ? precisionFormatter(num, precision) : num.toLocaleString()
+}
/**
* Tests if a number is a float.
@@ -57,6 +58,28 @@ export const precisionFormatter = (num: number, precision: number): string => {
}
}
+/**
+ * Formats a percentage, trimming decimals at 100.
+ *
+ * @example
+ * formatPercentage(100, 2) // "100%"
+ * formatPercentage(99.99, 2) // "99.99%"
+ */
+export const formatPercentage = (value: number, precision = 2) => {
+ const isHundred = Math.abs(value - 100) < 1e-6
+ if (isHundred) return '100%'
+ if (Number.isInteger(value)) return `${value}%`
+ const formatted = precisionFormatter(value, precision)
+ if (formatted.startsWith('<') || formatted.startsWith('>')) {
+ return `${formatted}%`
+ }
+ if (formatted.includes('.')) {
+ const [head, tail = ''] = formatted.split('.')
+ return `${head}.${tail.padEnd(precision, '0')}%`
+ }
+ return `${formatted}%`
+}
+
/**
* Formats a timestamp.
* Optionally formats the string to UTC
diff --git a/apps/studio/components/ui/Charts/ComposedChart.tsx b/apps/studio/components/ui/Charts/ComposedChart.tsx
index b61528ee5a6..10483b9705f 100644
--- a/apps/studio/components/ui/Charts/ComposedChart.tsx
+++ b/apps/studio/components/ui/Charts/ComposedChart.tsx
@@ -29,12 +29,12 @@ import {
updateStackedChartColors,
} from './Charts.constants'
import { CommonChartProps, Datum } from './Charts.types'
-import { numberFormatter, useChartSize } from './Charts.utils'
+import { formatPercentage, numberFormatter, useChartSize } from './Charts.utils'
import {
- calculateTotalChartAggregate,
CustomLabel,
CustomTooltip,
MultiAttribute,
+ calculateTotalChartAggregate,
} from './ComposedChart.utils'
import NoDataPlaceholder from './NoDataPlaceholder'
import { ChartHighlight } from './useChartHighlight'
@@ -191,8 +191,29 @@ export function ComposedChart({
)
}
+ function formatHighlightedValue(value: any) {
+ if (typeof value !== 'number') {
+ return value
+ }
+
+ if (shouldFormatBytes) {
+ const bytesValue = isNetworkChart ? Math.abs(value) : value
+ const formatted = formatBytes(bytesValue, valuePrecision)
+ return format === 'bytes-per-second' ? `${formatted}/s` : formatted
+ }
+
+ if (format === '%') {
+ return formatPercentage(value, valuePrecision)
+ }
+
+ if (valuePrecision === 0 && value > 0 && value < 1) {
+ return '<1'
+ }
+
+ return numberFormatter(value, valuePrecision)
+ }
+
function computeHighlightedValue() {
- const maxAttribute = attributes.find((a) => a.isMaxValue)
const referenceLines = attributes.filter(
(attribute) => attribute?.provider === 'reference-line'
)
@@ -233,24 +254,6 @@ export function ComposedChart({
return highlightedValue
}
- function formatHighlightedValue(value: any) {
- if (typeof value !== 'number') {
- return value
- }
-
- if (shouldFormatBytes) {
- const bytesValue = isNetworkChart ? Math.abs(value) : value
- const formatted = formatBytes(bytesValue, valuePrecision)
- return format === 'bytes-per-second' ? `${formatted}/s` : formatted
- }
-
- if (valuePrecision === 0 && value > 0 && value < 1) {
- return '<1'
- }
-
- return numberFormatter(value, valuePrecision)
- }
-
const maxAttribute = attributes.find((a) => a.isMaxValue)
const maxAttributeData = {
name: maxAttribute?.attribute,
@@ -307,7 +310,9 @@ export function ComposedChart({
const attribute = attributes.find((attr) => attr.attribute === att.name)
return !attribute?.isMaxValue
})
+
const visibleAttributes = stackedAttributes.filter((att) => !hiddenAttributes.has(att.name))
+
const isPercentage = format === '%'
const isRamChart =
!chartData?.some((att: any) => att.name.toLowerCase() === 'ram_usage') &&
diff --git a/apps/studio/components/ui/Charts/ComposedChart.utils.tsx b/apps/studio/components/ui/Charts/ComposedChart.utils.tsx
index 746b61fef05..df848d2d32e 100644
--- a/apps/studio/components/ui/Charts/ComposedChart.utils.tsx
+++ b/apps/studio/components/ui/Charts/ComposedChart.utils.tsx
@@ -5,7 +5,7 @@ import { formatBytes } from 'lib/helpers'
import { useState } from 'react'
import { cn, Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from 'ui'
import { CHART_COLORS, DateTimeFormats } from './Charts.constants'
-import { numberFormatter } from './Charts.utils'
+import { formatPercentage, numberFormatter } from './Charts.utils'
export interface ReportAttributes {
id?: string
@@ -236,9 +236,10 @@ export const CustomTooltip = ({
<span className="flex-grow text-foreground-lighter">Total</span>
<div className="flex items-end gap-1">
<span className="text-base">
- {formatNumeric(total as number) +
- (!isPercentage && format !== 'ms' ? byteUnitSuffix : '')}
- {isPercentage ? '%' : ''}
+ {isPercentage
+ ? formatPercentage(total as number, valuePrecision)
+ : formatNumeric(total as number) +
+ (!isPercentage && format !== 'ms' ? byteUnitSuffix : '')}
{format === 'ms' ? 'ms' : ''}
</span>
{maxValueAttribute &&
diff --git a/apps/studio/data/reports/database-charts.ts b/apps/studio/data/reports/database-charts.ts
index ce4df40e06b..77e096470eb 100644
--- a/apps/studio/data/reports/database-charts.ts
+++ b/apps/studio/data/reports/database-charts.ts
@@ -76,7 +76,11 @@ export const getReportAttributesV2: (
showGrid: true,
YAxisProps: {
width: 45,
- tickFormatter: (value: any) => `${numberFormatter(value, 2)}%`,
+ tickFormatter: (value: any) => {
+ // avoid displaying 100.00%
+ if (value === 100) return '100%'
+ return `${numberFormatter(value, 2)}%`
+ },
},
hideChartType: false,
defaultChartStyle: 'bar',
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 c4b9c0344c5..135d445f93b 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 {
+ formatPercentage,
isFloat,
numberFormatter,
precisionFormatter,
@@ -87,6 +88,23 @@ describe('precisionFormatter', () => {
})
})
+describe('formatPercentage', () => {
+ it('should format 100 without decimals', () => {
+ expect(formatPercentage(100, 2)).toBe('100%')
+ expect(formatPercentage(100, 0)).toBe('100%')
+ })
+
+ it('should keep decimals for non-100 values', () => {
+ expect(formatPercentage(99.99, 2)).toBe('99.99%')
+ expect(formatPercentage(0.5, 2)).toBe('0.50%')
+ })
+
+ it('should use numberFormatter for integers below 100', () => {
+ expect(formatPercentage(50, 2)).toBe('50%')
+ expect(formatPercentage(1, 2)).toBe('1%')
+ })
+})
+
test('useStacked', () => {
const { result } = renderHook(() =>
useStacked({