mango-v4-ui/components/token/PriceChart.tsx

90 lines
2.7 KiB
TypeScript
Raw Normal View History

import { formatDateAxis } from '@components/shared/DetailedAreaOrBarChart'
2023-04-12 17:31:22 -07:00
import dayjs from 'dayjs'
import { BirdeyePriceResponse } from 'hooks/useBirdeyeMarketPrices'
2023-07-21 11:50:06 -07:00
import useThemeWrapper from 'hooks/useThemeWrapper'
2022-10-23 18:43:07 -07:00
import { useMemo } from 'react'
import { Area, AreaChart, ResponsiveContainer, XAxis, YAxis } from 'recharts'
import { COLORS } from 'styles/colors'
2023-02-13 16:23:19 -08:00
import { formatCurrencyValue } from 'utils/numbers'
2022-10-23 18:43:07 -07:00
const PriceChart = ({
prices,
daysToShow,
}: {
prices: BirdeyePriceResponse[]
2022-10-23 18:43:07 -07:00
daysToShow: number
}) => {
2023-07-21 11:50:06 -07:00
const { theme } = useThemeWrapper()
2022-10-23 18:43:07 -07:00
const change = useMemo(() => {
return prices[prices.length - 1].value - prices[0].value
2022-10-23 18:43:07 -07:00
}, [prices])
return (
2023-02-13 16:23:19 -08:00
<div className="relative -mt-1 h-72 w-auto md:h-96">
<div className="mt-6 h-full">
2022-10-23 18:43:07 -07:00
<ResponsiveContainer width="100%" height="100%">
<AreaChart data={prices}>
<defs>
<linearGradient id="gradientArea" x1="0" y1="0" x2="0" y2="1">
<stop
offset="0%"
stopColor={
2022-11-30 19:32:32 -08:00
change >= 0 ? COLORS.UP[theme] : COLORS.DOWN[theme]
2022-10-23 18:43:07 -07:00
}
stopOpacity={0.15}
/>
<stop
offset="99%"
stopColor={
2022-11-30 19:32:32 -08:00
change >= 0 ? COLORS.UP[theme] : COLORS.DOWN[theme]
2022-10-23 18:43:07 -07:00
}
stopOpacity={0}
/>
</linearGradient>
</defs>
<Area
isAnimationActive={false}
type="monotone"
dataKey="value"
2022-11-30 19:32:32 -08:00
stroke={change >= 0 ? COLORS.UP[theme] : COLORS.DOWN[theme]}
2022-10-23 18:43:07 -07:00
strokeWidth={1.5}
fill="url(#gradientArea)"
/>
<XAxis
axisLine={false}
dataKey="unixTime"
2022-12-06 16:22:32 -08:00
minTickGap={20}
2022-10-23 18:43:07 -07:00
padding={{ left: 20, right: 20 }}
tick={{
fill: 'var(--fgd-4)',
2022-10-23 18:43:07 -07:00
fontSize: 10,
}}
tickLine={false}
2023-04-12 17:31:22 -07:00
tickFormatter={(d) =>
formatDateAxis(dayjs(d * 1000).toISOString(), daysToShow)
}
2022-10-23 18:43:07 -07:00
/>
<YAxis
axisLine={false}
dataKey="value"
2022-10-23 18:43:07 -07:00
type="number"
domain={['dataMin', 'dataMax']}
padding={{ top: 20, bottom: 20 }}
tick={{
fill: 'var(--fgd-4)',
2022-10-23 18:43:07 -07:00
fontSize: 10,
}}
2023-02-13 16:23:19 -08:00
tickFormatter={(x) => formatCurrencyValue(x)}
2022-10-23 18:43:07 -07:00
tickLine={false}
width={prices[0].value < 0.00001 ? 100 : 60}
2022-10-23 18:43:07 -07:00
/>
</AreaChart>
</ResponsiveContainer>
</div>
</div>
)
}
export default PriceChart