2022-06-24 17:14:20 +00:00
|
|
|
import { ResponsiveLine } from '@nivo/line'
|
|
|
|
import { PortfolioMetrics } from 'common/user'
|
|
|
|
import { formatMoney } from 'common/util/format'
|
|
|
|
import { last } from 'lodash'
|
|
|
|
import { memo } from 'react'
|
|
|
|
import { useWindowSize } from 'web/hooks/use-window-size'
|
|
|
|
import { formatTime } from 'web/lib/util/time'
|
|
|
|
|
|
|
|
export const PortfolioValueGraph = memo(function PortfolioValueGraph(props: {
|
|
|
|
portfolioHistory: PortfolioMetrics[]
|
|
|
|
height?: number
|
2022-08-27 08:09:01 +00:00
|
|
|
includeTime?: boolean
|
2022-06-24 17:14:20 +00:00
|
|
|
}) {
|
2022-08-27 08:09:01 +00:00
|
|
|
const { portfolioHistory, height, includeTime } = props
|
2022-06-24 17:14:20 +00:00
|
|
|
const { width } = useWindowSize()
|
|
|
|
|
2022-08-27 08:09:01 +00:00
|
|
|
const points = portfolioHistory.map((p) => {
|
2022-06-24 17:14:20 +00:00
|
|
|
return {
|
|
|
|
x: new Date(p.timestamp),
|
|
|
|
y: p.balance + p.investmentValue,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
const data = [{ id: 'Value', data: points, color: '#11b981' }]
|
|
|
|
const numXTickValues = !width || width < 800 ? 2 : 5
|
|
|
|
const numYTickValues = 4
|
|
|
|
const endDate = last(points)?.x
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className="w-full overflow-hidden"
|
|
|
|
style={{ height: height ?? (!width || width >= 800 ? 350 : 250) }}
|
|
|
|
>
|
|
|
|
<ResponsiveLine
|
|
|
|
data={data}
|
|
|
|
margin={{ top: 20, right: 28, bottom: 22, left: 60 }}
|
|
|
|
xScale={{
|
|
|
|
type: 'time',
|
2022-07-03 19:18:12 +00:00
|
|
|
min: points[0]?.x,
|
2022-06-24 17:14:20 +00:00
|
|
|
max: endDate,
|
|
|
|
}}
|
|
|
|
yScale={{
|
|
|
|
type: 'linear',
|
|
|
|
stacked: false,
|
|
|
|
min: Math.min(...points.map((p) => p.y)),
|
|
|
|
}}
|
|
|
|
gridYValues={numYTickValues}
|
2022-08-09 17:08:14 +00:00
|
|
|
curve="stepAfter"
|
|
|
|
enablePoints={false}
|
2022-06-24 17:14:20 +00:00
|
|
|
colors={{ datum: 'color' }}
|
|
|
|
axisBottom={{
|
|
|
|
tickValues: numXTickValues,
|
2022-08-27 08:09:01 +00:00
|
|
|
format: (time) => formatTime(+time, !!includeTime),
|
2022-06-24 17:14:20 +00:00
|
|
|
}}
|
|
|
|
pointBorderColor="#fff"
|
|
|
|
pointSize={points.length > 100 ? 0 : 6}
|
|
|
|
axisLeft={{
|
|
|
|
tickValues: numYTickValues,
|
|
|
|
format: (value) => formatMoney(value),
|
|
|
|
}}
|
|
|
|
enableGridX={!!width && width >= 800}
|
|
|
|
enableGridY={true}
|
|
|
|
enableSlices="x"
|
|
|
|
animate={false}
|
2022-07-03 19:18:12 +00:00
|
|
|
yFormat={(value) => formatMoney(+value)}
|
2022-06-24 17:14:20 +00:00
|
|
|
></ResponsiveLine>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
})
|