From 03fdc2ae1019157b6aa90044f6599906ecdf4faf Mon Sep 17 00:00:00 2001 From: saml33 Date: Fri, 30 Jun 2023 13:05:41 +1000 Subject: [PATCH] render basic chart to component --- components/trade/DepthChart.tsx | 80 +++++++++++++++++++++++++ components/trade/OrderbookAndTrades.tsx | 9 +++ styles/globals.css | 21 +++++++ 3 files changed, 110 insertions(+) create mode 100644 components/trade/DepthChart.tsx diff --git a/components/trade/DepthChart.tsx b/components/trade/DepthChart.tsx new file mode 100644 index 00000000..1ad34ae3 --- /dev/null +++ b/components/trade/DepthChart.tsx @@ -0,0 +1,80 @@ +import mangoStore from '@store/mangoStore' +import React, { useEffect, useState } from 'react' +import { + LineChart, + Line, + XAxis, + YAxis, + Tooltip, + Legend, + ResponsiveContainer, +} from 'recharts' + +type DepthData = { + price: number + [key: string]: number + cumulative: number +} + +function DepthChart() { + const orderbook = mangoStore((s) => s.selectedMarket.orderbook) + const [chartData, setChartData] = useState([]) + console.log(orderbook) + useEffect(() => { + const bidsCumulative = calculateCumulative(orderbook.bids, 'bids') + const asksCumulative = calculateCumulative(orderbook.asks, 'asks') + + const mergedData = mergeCumulativeData(bidsCumulative, asksCumulative) + setChartData(mergedData) + }, [orderbook]) + + const calculateCumulative = (levels: number[][], type: 'bids' | 'asks') => { + let cumulative = 0 + return levels.map((level) => { + cumulative += level[1] + return { price: level[0], [type]: cumulative, cumulative: cumulative } + }) + } + + const mergeCumulativeData = (bids: DepthData[], asks: DepthData[]) => { + return [...bids, ...asks].sort((a, b) => a.price - b.price) + } + return ( + + + + + + + + + + + + + + + + + + + + + ) +} + +export default DepthChart diff --git a/components/trade/OrderbookAndTrades.tsx b/components/trade/OrderbookAndTrades.tsx index fc69ac9c..6435932d 100644 --- a/components/trade/OrderbookAndTrades.tsx +++ b/components/trade/OrderbookAndTrades.tsx @@ -2,9 +2,11 @@ import TabButtons from '@components/shared/TabButtons' import { useState } from 'react' import Orderbook from './Orderbook' import RecentTrades from './RecentTrades' +import DepthChart from './DepthChart' export const TABS: [string, number][] = [ ['trade:book', 0], + ['trade:depth', 0], ['trade:trades', 0], ] @@ -27,6 +29,13 @@ const OrderbookAndTrades = () => { > +
+ +