From 5c975e2e76012294a9f1a7d0d398e02d07e14a5d Mon Sep 17 00:00:00 2001 From: Raphael Fakhri <153192858+RaphaelFakhri@users.noreply.github.com> Date: Tue, 3 Nov 2026 13:06:00 +0000 Subject: [PATCH] fix(trading): keep price chart y-axis labels distinct --- .../src/__tests__/formatAxisPrice-test.ts | 28 +++++++++++++++++++ packages/sdk/src/index.ts | 2 +- packages/sdk/src/utils/formatPrice.ts | 14 ++++++++++ .../src/screens/AssetDetailsScreen.tsx | 12 ++------ 4 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 packages/host/src/__tests__/formatAxisPrice-test.ts diff --git a/packages/host/src/__tests__/formatAxisPrice-test.ts b/packages/host/src/__tests__/formatAxisPrice-test.ts new file mode 100644 index 00000000..73c2d45e --- /dev/null +++ b/packages/host/src/__tests__/formatAxisPrice-test.ts @@ -0,0 +1,28 @@ +import {formatAxisPrice} from 'super-app-showcase-sdk'; + +describe('formatAxisPrice', () => { + it.each([ + [ + [3865, 3870, 3875, 3880], + ['$3.865k', '$3.87k', '$3.875k', '$3.88k'], + ], + [ + [109000, 109100, 109200], + ['$109k', '$109.1k', '$109.2k'], + ], + [ + [176.5, 177, 177.5, 178], + ['$176.5', '$177', '$177.5', '$178'], + ], + [ + [2.31, 2.32, 2.33], + ['$2.31', '$2.32', '$2.33'], + ], + [ + [0.652, 0.654, 0.656], + ['$0.652', '$0.654', '$0.656'], + ], + ])('gives each tick of %j its own label', (ticks, expected) => { + expect(ticks.map(formatAxisPrice)).toEqual(expected); + }); +}); diff --git a/packages/sdk/src/index.ts b/packages/sdk/src/index.ts index 3adcc7e3..7f5423d5 100644 --- a/packages/sdk/src/index.ts +++ b/packages/sdk/src/index.ts @@ -8,7 +8,7 @@ export {useHistoricalPrices} from './hooks/useHistoricalPrices'; export {useFlashAnimation} from './hooks/useFlashAnimation'; export {ASSETS, ASSET_MAP, KRAKEN_PAIR_TO_SYMBOL} from './constants'; export type {Asset, AssetSymbol, PriceUpdate, PriceMap} from './types'; -export {formatPrice, formatValue} from './utils/formatPrice'; +export {formatPrice, formatValue, formatAxisPrice} from './utils/formatPrice'; export {getAssetIconUri} from './utils/assetIcon'; export {colors} from './theme'; export {default as ConnectionBanner} from './components/ConnectionBanner'; diff --git a/packages/sdk/src/utils/formatPrice.ts b/packages/sdk/src/utils/formatPrice.ts index e1372955..bb39f86e 100644 --- a/packages/sdk/src/utils/formatPrice.ts +++ b/packages/sdk/src/utils/formatPrice.ts @@ -17,3 +17,17 @@ export const formatValue = (value: number): string => maximumFractionDigits: 2, }) : '—'; + +// Chart ticks can sit closer together than a fixed number of decimals shows +// (e.g. $3,865 / $3,870 / $3,875), so keep every significant digit of the +// tick instead of rounding them all to the same label. +export const formatAxisPrice = (value: number): string => { + if (value >= 1000) { + return `$${(value / 1000).toLocaleString('en-US', { + maximumFractionDigits: 3, + })}k`; + } + return `$${value.toLocaleString('en-US', { + maximumFractionDigits: value >= 1 ? 4 : 6, + })}`; +}; diff --git a/packages/trading/src/screens/AssetDetailsScreen.tsx b/packages/trading/src/screens/AssetDetailsScreen.tsx index b010f4e4..d5bf85d1 100644 --- a/packages/trading/src/screens/AssetDetailsScreen.tsx +++ b/packages/trading/src/screens/AssetDetailsScreen.tsx @@ -21,6 +21,7 @@ import { useAssetPrice, useHistoricalPrices, formatPrice, + formatAxisPrice, colors, } from 'super-app-showcase-sdk'; import type {TradingStackParamList} from '../navigation/MainNavigator'; @@ -186,16 +187,7 @@ const AssetDetailsScreen = ({route, navigation}: Props) => { tickCount: 4, labelColor: colors.secondary, axisSide: 'right', - formatYLabel: value => { - const n = Number(value); - if (n >= 1000) { - return `$${(n / 1000).toFixed(1)}k`; - } - if (n >= 1) { - return `$${n.toFixed(0)}`; - } - return `$${n.toFixed(3)}`; - }, + formatYLabel: value => formatAxisPrice(Number(value)), }, ]}> {({points}) => (