mango-v4-ui/components/stats/PerpVolumeChart.tsx

118 lines
3.3 KiB
TypeScript
Raw Normal View History

2023-05-31 20:03:56 -07:00
import { useMemo, useState } from 'react'
2023-06-05 04:06:37 -07:00
import { GroupedDataItem, PerpStatsItem } from 'types'
2023-05-31 20:03:56 -07:00
import DetailedAreaOrBarChart from '@components/shared/DetailedAreaOrBarChart'
import { useTranslation } from 'next-i18next'
import { formatYAxis } from 'utils/formatting'
const PerpVolumeChart = ({
loading,
marketStats,
}: {
loading: boolean
marketStats: PerpStatsItem[]
}) => {
const { t } = useTranslation(['common', 'stats', 'trade'])
const [daysToShow, setDaysToShow] = useState('30')
2023-06-05 04:06:37 -07:00
const groupArrayByHours = (data: PerpStatsItem[], hours: number) => {
2023-05-31 20:03:56 -07:00
const groupedData = []
2023-06-05 04:06:37 -07:00
let currentGroup: GroupedDataItem[] = []
2023-05-31 20:03:56 -07:00
for (let i = 0; i < data.length; i++) {
const obj = data[i]
const date = new Date(obj.date_hour)
2023-06-05 04:06:37 -07:00
if (hours === 24) {
const day = date.getDate()
const month = date.getMonth()
if (
currentGroup.length === 0 ||
(currentGroup[0].day === day && currentGroup[0].month === month)
) {
currentGroup.push({ ...obj, day: day, month: month })
} else {
groupedData.push(currentGroup)
currentGroup = [{ ...obj, day: day, month: month }]
2023-05-31 20:03:56 -07:00
}
} else {
2023-06-05 04:06:37 -07:00
const intervalMillis = hours * 60 * 60 * 1000
const timestamp = date.getTime()
if (
currentGroup.length === 0 ||
timestamp - currentGroup[0].timestamp <= intervalMillis
) {
currentGroup.push({ ...obj, timestamp: timestamp })
} else {
groupedData.push(currentGroup)
currentGroup = [{ ...obj, timestamp: timestamp }]
}
2023-05-31 20:03:56 -07:00
}
}
2023-06-05 04:06:37 -07:00
if (currentGroup.length > 0) {
groupedData.push(currentGroup)
}
2023-05-31 20:03:56 -07:00
return groupedData
}
2023-06-05 04:06:37 -07:00
const interval = useMemo(() => {
2023-05-31 20:03:56 -07:00
if (daysToShow === '30') {
2023-06-05 04:06:37 -07:00
return 24
2023-05-31 20:03:56 -07:00
} else if (daysToShow === '7') {
2023-06-05 04:06:37 -07:00
return 6
2023-05-31 20:03:56 -07:00
} else {
2023-06-05 04:06:37 -07:00
return 1
2023-05-31 20:03:56 -07:00
}
}, [daysToShow])
const chartData = useMemo(() => {
if (!marketStats) return []
2023-06-05 04:06:37 -07:00
const chartData = []
if (interval !== 1) {
const groupedData = groupArrayByHours(marketStats, interval)
for (let i = 0; i < groupedData.length; i++) {
const volume =
groupedData[i][groupedData[i].length - 1].cumulative_quote_volume -
groupedData[i][0].cumulative_quote_volume
chartData.push({
date_hour: groupedData[i][groupedData[i].length - 1].date_hour,
volume: volume,
})
}
} else {
for (let i = 0; i < marketStats.length; i++) {
const volume =
marketStats[i].cumulative_quote_volume -
(marketStats[i - 1] ? marketStats[i - 1].cumulative_quote_volume : 0)
chartData.push({
date_hour: marketStats[i].date_hour,
volume: volume,
})
}
}
return chartData
2023-05-31 20:03:56 -07:00
}, [daysToShow, interval, marketStats])
return (
<DetailedAreaOrBarChart
data={chartData}
daysToShow={daysToShow}
setDaysToShow={setDaysToShow}
heightClass="h-64"
loading={loading}
loaderHeightClass="h-[350px]"
prefix="$"
tickFormat={(x) => formatYAxis(x)}
2023-06-05 04:06:37 -07:00
title={t('stats:volume')}
2023-05-31 20:03:56 -07:00
xKey="date_hour"
2023-06-05 04:06:37 -07:00
yKey="volume"
2023-05-31 20:03:56 -07:00
// yDecimals={5}
chartType="bar"
tooltipDateFormat={daysToShow === '30' ? 'DD MMM YY' : ''}
/>
)
}
export default PerpVolumeChart