From e3520a5650f10148b38cd19caab9e89492156323 Mon Sep 17 00:00:00 2001 From: ingawei Date: Fri, 23 Sep 2022 13:12:16 -0700 Subject: [PATCH] getting site numbers to update graph upon hover --- .../portfolio/portfolio-value-graph.tsx | 101 +++++++++--------- .../portfolio/portfolio-value-section.tsx | 14 ++- 2 files changed, 60 insertions(+), 55 deletions(-) diff --git a/web/components/portfolio/portfolio-value-graph.tsx b/web/components/portfolio/portfolio-value-graph.tsx index 51381fef..be5c3ef0 100644 --- a/web/components/portfolio/portfolio-value-graph.tsx +++ b/web/components/portfolio/portfolio-value-graph.tsx @@ -1,18 +1,22 @@ import { ResponsiveLine } from '@nivo/line' import { PortfolioMetrics } from 'common/user' import { formatMoney } from 'common/util/format' -import { last } from 'lodash' +import dayjs from 'dayjs' +import { last, set } from 'lodash' import { memo } from 'react' import { useWindowSize } from 'web/hooks/use-window-size' import { formatTime } from 'web/lib/util/time' +import { Col } from '../layout/col' export const PortfolioValueGraph = memo(function PortfolioValueGraph(props: { portfolioHistory: PortfolioMetrics[] mode: 'value' | 'profit' + setGraphDisplayNumber: (value: number | string) => void height?: number includeTime?: boolean }) { - const { portfolioHistory, height, includeTime, mode } = props + const { portfolioHistory, height, includeTime, mode, setGraphDisplayNumber } = + props const { width } = useWindowSize() function getPoints(line: 'value' | 'posProfit' | 'negProfit') { @@ -62,8 +66,8 @@ export const PortfolioValueGraph = memo(function PortfolioValueGraph(props: { ] } const firstPoints = data[0].data - const numXTickValues = !width || width < 800 ? 2 : 5 - const numYTickValues = 4 + // const numXTickValues = !width || width < 800 ? 2 : 5 + // const numYTickValues = 4 const endDate = last(firstPoints)?.x const firstPointsY = firstPoints @@ -73,8 +77,7 @@ export const PortfolioValueGraph = memo(function PortfolioValueGraph(props: { .map((p) => p.y) // console.log(firstPointsY) - console.log( - 'MIN: ', + const yMin = mode === 'value' ? Math.min(...firstPointsY) : Math.min( @@ -84,9 +87,9 @@ export const PortfolioValueGraph = memo(function PortfolioValueGraph(props: { return p.y !== null }) .map((p) => p.y) - ), + ) - 'MAX: ', + const yMax = mode === 'value' ? Math.max(...firstPointsY) : Math.max( @@ -97,16 +100,15 @@ export const PortfolioValueGraph = memo(function PortfolioValueGraph(props: { }) .map((p) => p.y) ) - ) return (
= 800 ? 350 : 250) }} + style={{ height: height ?? (!width || width >= 800 ? 200 : 100) }} > { - return p.y !== null - }) - .map((p) => p.y) - ), - max: - mode === 'value' - ? Math.max(...firstPointsY) - : Math.max( - ...firstPointsY, - ...data[1].data - .filter((p) => { - return p.y !== null - }) - .map((p) => p.y) - ), + min: yMin, + max: yMax, }} - gridYValues={numYTickValues} - curve="linear" + curve="stepAfter" enablePoints={false} colors={{ datum: 'color' }} axisBottom={{ - tickValues: numXTickValues, + tickValues: 0, format: (time) => formatTime(+time, !!includeTime), }} pointBorderColor="#fff" pointSize={firstPoints.length > 100 ? 0 : 6} axisLeft={{ - tickValues: numYTickValues, + tickValues: 0, format: (value) => formatMoney(value), }} - enableGridX={!!width && width >= 800} - enableGridY={true} + enableGridX={false} + enableGridY={false} enableSlices="x" animate={false} yFormat={(value) => formatMoney(+value)} - // defs={[ - // { - // id: 'purpleGradient', - // type: 'linearGradient', - // colors: [ - // { offset: 0, color: '#7c3aed' }, - // { - // offset: 100, - // color: '#inherit', - // opacity: 0, - // }, - // ], - // }, - // ]} enableArea={true} + areaOpacity={0.1} + sliceTooltip={({ slice }) => { + slice.points.map((point) => + setGraphDisplayNumber(point.data.yFormatted) + ) + return ( +
+ {slice.points.map((point) => ( +
+ +
+ Time [{point.data.xFormatted}] +
+
+ {point.serieId} [{point.data.yFormatted}] +
+ +
+ ))} +
+ ) + }} >
) diff --git a/web/components/portfolio/portfolio-value-section.tsx b/web/components/portfolio/portfolio-value-section.tsx index d1b20305..e6049f0f 100644 --- a/web/components/portfolio/portfolio-value-section.tsx +++ b/web/components/portfolio/portfolio-value-section.tsx @@ -17,6 +17,7 @@ export const PortfolioValueSection = memo( const [portfolioPeriod, setPortfolioPeriod] = useState('weekly') const portfolioHistory = usePortfolioHistory(userId, portfolioPeriod) const [graphMode, setGraphMode] = useState<'profit' | 'value'>('profit') + const [graphDisplayNumber, setGraphDisplayNumber] = useState(null) // Remember the last defined portfolio history. const portfolioRef = useRef(portfolioHistory) @@ -41,7 +42,11 @@ export const PortfolioValueSection = memo( Portfolio value
- {formatMoney(totalValue)} + {graphMode === 'value' + ? graphDisplayNumber + ? graphDisplayNumber + : formatMoney(totalValue) + : formatMoney(totalValue)}
@@ -52,7 +57,11 @@ export const PortfolioValueSection = memo( 'text-lg sm:text-xl' )} > - {formatMoney(totalProfit)} + {graphMode === 'profit' + ? graphDisplayNumber + ? graphDisplayNumber + : formatMoney(totalProfit) + : formatMoney(totalProfit)} @@ -62,6 +71,7 @@ export const PortfolioValueSection = memo( portfolioHistory={currPortfolioHistory} includeTime={true} mode={graphMode} + setGraphDisplayNumber={setGraphDisplayNumber} />