From f916f2bd74c5bdaef47dd0452366c301fcc49b83 Mon Sep 17 00:00:00 2001 From: ingawei Date: Fri, 23 Sep 2022 12:08:41 -0700 Subject: [PATCH] getting color graphs --- .../portfolio/portfolio-value-graph.tsx | 136 ++++++++++++++++-- .../portfolio/portfolio-value-section.tsx | 89 ++++++++---- 2 files changed, 180 insertions(+), 45 deletions(-) diff --git a/web/components/portfolio/portfolio-value-graph.tsx b/web/components/portfolio/portfolio-value-graph.tsx index d8489b47..51381fef 100644 --- a/web/components/portfolio/portfolio-value-graph.tsx +++ b/web/components/portfolio/portfolio-value-graph.tsx @@ -15,20 +15,90 @@ export const PortfolioValueGraph = memo(function PortfolioValueGraph(props: { const { portfolioHistory, height, includeTime, mode } = props const { width } = useWindowSize() - const points = portfolioHistory.map((p) => { - const { timestamp, balance, investmentValue, totalDeposits } = p - const value = balance + investmentValue - const profit = value - totalDeposits + function getPoints(line: 'value' | 'posProfit' | 'negProfit') { + let points = portfolioHistory.map((p) => { + const { timestamp, balance, investmentValue, totalDeposits } = p + const value = balance + investmentValue + const profit = value - totalDeposits + let posProfit = null + let negProfit = null - return { - x: new Date(timestamp), - y: mode === 'value' ? value : profit, - } - }) - const data = [{ id: 'Value', data: points, color: '#11b981' }] + // const profit = value - totalDeposits + if (profit < 0) { + negProfit = profit + } else { + posProfit = profit + } + + return { + x: new Date(timestamp), + y: + line === 'value' + ? value + : line === 'posProfit' + ? posProfit + : negProfit, + } + }) + return points + } + + let data + + if (mode === 'value') { + data = [{ id: 'value', data: getPoints('value'), color: '#4f46e5' }] + } else { + data = [ + { + id: 'negProfit', + data: getPoints('negProfit'), + color: '#dc2626', + }, + { + id: 'posProfit', + data: getPoints('posProfit'), + color: '#14b8a6', + }, + ] + } + const firstPoints = data[0].data const numXTickValues = !width || width < 800 ? 2 : 5 const numYTickValues = 4 - const endDate = last(points)?.x + const endDate = last(firstPoints)?.x + + const firstPointsY = firstPoints + .filter((p) => { + return p.y !== null + }) + .map((p) => p.y) + // console.log(firstPointsY) + + console.log( + 'MIN: ', + mode === 'value' + ? Math.min(...firstPointsY) + : Math.min( + ...firstPointsY, + ...data[1].data + .filter((p) => { + 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) + ) + ) + return (
p.y)), + min: + mode === 'value' + ? Math.min(...firstPointsY) + : Math.min( + ...firstPointsY, + ...data[1].data + .filter((p) => { + 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) + ), }} gridYValues={numYTickValues} - curve="stepAfter" + curve="linear" enablePoints={false} colors={{ datum: 'color' }} axisBottom={{ @@ -56,7 +147,7 @@ export const PortfolioValueGraph = memo(function PortfolioValueGraph(props: { format: (time) => formatTime(+time, !!includeTime), }} pointBorderColor="#fff" - pointSize={points.length > 100 ? 0 : 6} + pointSize={firstPoints.length > 100 ? 0 : 6} axisLeft={{ tickValues: numYTickValues, format: (value) => formatMoney(value), @@ -66,6 +157,21 @@ export const PortfolioValueGraph = memo(function PortfolioValueGraph(props: { 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} >
) diff --git a/web/components/portfolio/portfolio-value-section.tsx b/web/components/portfolio/portfolio-value-section.tsx index f46eed8d..d1b20305 100644 --- a/web/components/portfolio/portfolio-value-section.tsx +++ b/web/components/portfolio/portfolio-value-section.tsx @@ -4,6 +4,7 @@ import { last } from 'lodash' import { memo, useRef, useState } from 'react' import { usePortfolioHistory } from 'web/hooks/use-portfolio-history' import { Period } from 'web/lib/firebase/users' +import { PillButton } from '../buttons/pill-button' import { Col } from '../layout/col' import { Row } from '../layout/row' import { Spacer } from '../layout/spacer' @@ -15,6 +16,7 @@ export const PortfolioValueSection = memo( const [portfolioPeriod, setPortfolioPeriod] = useState('weekly') const portfolioHistory = usePortfolioHistory(userId, portfolioPeriod) + const [graphMode, setGraphMode] = useState<'profit' | 'value'>('profit') // Remember the last defined portfolio history. const portfolioRef = useRef(portfolioHistory) @@ -32,38 +34,34 @@ export const PortfolioValueSection = memo( return ( <> - - -
Profit
-
{formatMoney(totalProfit)}
- - {/* */} + + + +
+ Portfolio value +
+
+ {formatMoney(totalValue)} +
+ + +
Profit
+
0 ? 'text-green-500' : 'text-red-600', + 'text-lg sm:text-xl' + )} + > + {formatMoney(totalProfit)} +
+ +
+
- - -
Portfolio value
-
{formatMoney(totalValue)}
- - +