-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
64c2ef0
commit 2f772ec
Showing
5 changed files
with
103 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { useMemo, useState } from 'react'; | ||
import { XAxis, AreaChart, Area, Tooltip, ResponsiveContainer } from 'recharts'; | ||
|
||
import dayjs from 'dayjs'; | ||
|
||
import { IHistoricalData } from '../interfaces/common'; | ||
|
||
import Loader from './Loader'; | ||
|
||
import { formatStringToPriceWithPrecision } from '../utils/numberUtils'; | ||
import { getTransformedPriceData } from '../utils/metricsUtils'; | ||
|
||
import { INITIAL_CHART_LABELS } from '../constants'; | ||
|
||
interface ILineChartProps { | ||
chartData: IHistoricalData[]; | ||
aggregatedValue: string; | ||
} | ||
|
||
const Chart = ({ chartData, aggregatedValue }: ILineChartProps) => { | ||
const [value, setValue] = useState<string>(aggregatedValue); | ||
const [dateLabel, setDateLabel] = useState<string>(''); | ||
|
||
const formattedPriceData = useMemo(() => { | ||
return getTransformedPriceData(chartData); | ||
}, [chartData]); | ||
|
||
return ( | ||
<div style={{ minHeight: '392px' }}> | ||
<div className="d-flex justify-content-between align-items-start"> | ||
<div> | ||
<p className="text-main text-gray d-flex align-items-center"> | ||
{INITIAL_CHART_LABELS.PRICE}{' '} | ||
</p> | ||
<p className="text-headline text-bold mt-3"> | ||
{formatStringToPriceWithPrecision(value?.toString() as string)} | ||
</p> | ||
{dateLabel ? ( | ||
<p className="text-small">{dayjs(dateLabel).format('MMM D, YYYY')}</p> | ||
) : ( | ||
<p className="text-small">-</p> | ||
)} | ||
</div> | ||
</div> | ||
{chartData.length !== 0 ? ( | ||
<ResponsiveContainer height={300}> | ||
<AreaChart | ||
onMouseMove={(e: any) => { | ||
if (e && e.activePayload) { | ||
const { time: currTime, value: currValue } = e.activePayload[0]?.payload; | ||
if (currTime !== dateLabel || value !== currValue) { | ||
setValue(currValue); | ||
setDateLabel(currTime); | ||
} | ||
} | ||
}} | ||
width={500} | ||
height={300} | ||
data={formattedPriceData} | ||
margin={{ | ||
top: 5, | ||
right: 30, | ||
left: 20, | ||
bottom: 5, | ||
}} | ||
onMouseLeave={() => { | ||
setValue(aggregatedValue); | ||
setDateLabel(''); | ||
}} | ||
> | ||
<defs> | ||
<linearGradient id="gradient" x1="0" y1="0" x2="0" y2="1"> | ||
<stop offset="5%" stopOpacity={0.5} /> | ||
<stop offset="100%" stopOpacity={0} /> | ||
</linearGradient> | ||
</defs> | ||
<XAxis | ||
dataKey="time" | ||
axisLine={false} | ||
tickLine={false} | ||
tickFormatter={time => dayjs(time).format('D')} | ||
minTickGap={10} | ||
interval="preserveStart" | ||
/> | ||
<Tooltip cursor={{ stroke: '#8884d8' }} contentStyle={{ display: 'none' }} /> | ||
<Area dataKey="value" type="monotone" strokeWidth={2} stroke="#E541EE" fill="#19193F" /> | ||
</AreaChart> | ||
</ResponsiveContainer> | ||
) : ( | ||
<Loader /> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default Chart; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters