diff --git a/components/trade/DepthChart.tsx b/components/trade/DepthChart.tsx index 43b02803..de2f3015 100644 --- a/components/trade/DepthChart.tsx +++ b/components/trade/DepthChart.tsx @@ -17,7 +17,22 @@ import { } from 'recharts' import { CategoricalChartFunc } from 'recharts/types/chart/generateCategoricalChart' import { COLORS } from 'styles/colors' -import { getDecimalCount } from 'utils/numbers' +import { floorToDecimal, getDecimalCount } from 'utils/numbers' + +type LabelPosition = + | 'left' + | 'right' + | 'center' + | 'bottom' + | 'insideLeft' + | 'insideRight' + | 'insideTop' + | 'insideBottom' + | 'insideTopLeft' + | 'insideTopRight' + | 'insideBottomLeft' + | 'insideBottomRight' + | 'top' const DepthChart = ({ grouping }: { grouping: number }) => { const { theme } = useTheme() @@ -36,6 +51,7 @@ const DepthChart = ({ grouping }: { grouping: number }) => { return [...bidsWithSide, ...asksWithSide].sort((a, b) => a.price - b.price) } + // filter elements with the same size const chartData = useMemo(() => { if (!orderbook) return [] return mergeCumulativeData(orderbook.bids, orderbook.asks) @@ -87,7 +103,8 @@ const DepthChart = ({ grouping }: { grouping: number }) => { }, [chartData, markPrice, priceRangePercent]) const yTickFormatter = useCallback( - (tick: number) => { + (tick: number | undefined) => { + if (!tick) return if (!serumOrPerpMarket) return tick.toFixed(1) const tickDecimals = getDecimalCount(serumOrPerpMarket.tickSize) if (tickDecimals >= 7) { @@ -97,6 +114,55 @@ const DepthChart = ({ grouping }: { grouping: number }) => { [serumOrPerpMarket] ) + const getChartReferenceColor = (price: number | undefined) => { + if (!price || !markPrice) return 'var(--fgd-2)' + return price > markPrice ? 'var(--down)' : 'var(--up)' + } + + const getPercentFromMarkPrice = (price: number | undefined) => { + if (!price || !markPrice) return + const percentDif = ((price - markPrice) / markPrice) * 100 + return `${percentDif.toFixed(2)}%` + } + + const getSizeFromMouseData = useCallback( + (size: number | undefined) => { + if (!size || !serumOrPerpMarket) return + return floorToDecimal( + size, + getDecimalCount(serumOrPerpMarket.tickSize) + ).toString() + }, + [serumOrPerpMarket] + ) + + const getSizeLabelPosition = useCallback( + (size: number | undefined, price: number | undefined) => { + if (!xMax || !size || !price || !markPrice) return `insideRight` + const yPosition = price > markPrice ? 'Top' : 'Bottom' + const midPoint = xMax / 2 + const xPosition = size > midPoint ? 'Left' : 'Right' + return `inside${yPosition}${xPosition}` as LabelPosition + }, + [xMax, markPrice] + ) + + const getPercentLabelPosition = useCallback( + (price: number | undefined) => { + if (!markPrice || !price || !yMax || !yMin) return 'bottom' + const upperMidPoint = (markPrice + yMax) / 2 + const lowerMidPoint = (markPrice + yMin) / 2 + return price > markPrice + ? price > upperMidPoint + ? 'bottom' + : 'top' + : price > lowerMidPoint + ? 'bottom' + : 'top' + }, + [markPrice, yMax, yMin] + ) + const handleMouseMove: CategoricalChartFunc = (coords) => { if (coords?.activePayload) { setMouseData(coords.activePayload[0].payload) @@ -156,13 +222,13 @@ const DepthChart = ({ grouping }: { grouping: number }) => { /> -