mango-v4-ui/components/shared/DetailedAreaChart.tsx

415 lines
14 KiB
TypeScript
Raw Normal View History

2023-02-27 23:20:11 -08:00
/* eslint-disable @typescript-eslint/no-explicit-any */
2022-10-23 18:43:07 -07:00
import { FunctionComponent, useMemo, useState } from 'react'
2022-08-03 02:49:31 -07:00
import dayjs from 'dayjs'
import relativeTime from 'dayjs/plugin/relativeTime'
import {
AreaChart,
Area,
XAxis,
YAxis,
Tooltip,
ResponsiveContainer,
} from 'recharts'
import FlipNumbers from 'react-flip-numbers'
import ContentBox from '../shared/ContentBox'
import SheenLoader from '../shared/SheenLoader'
import { COLORS } from '../../styles/colors'
import { useTheme } from 'next-themes'
import { IconButton } from './Button'
2022-12-11 15:21:40 -08:00
import { ArrowLeftIcon, NoSymbolIcon } from '@heroicons/react/20/solid'
2022-08-03 02:49:31 -07:00
import { FadeInFadeOut } from './Transitions'
2022-09-07 23:25:32 -07:00
import ChartRangeButtons from './ChartRangeButtons'
2022-10-05 22:11:28 -07:00
import Change from './Change'
2022-11-23 18:30:20 -08:00
import useLocalStorageState from 'hooks/useLocalStorageState'
import { ANIMATION_SETTINGS_KEY } from 'utils/constants'
2023-01-24 16:54:24 -08:00
import { formatNumericValue } from 'utils/numbers'
2022-11-24 18:39:14 -08:00
import { INITIAL_ANIMATION_SETTINGS } from '@components/settings/AnimationSettings'
2022-12-06 03:58:22 -08:00
import { AxisDomain } from 'recharts/types/util/types'
2022-12-11 15:21:40 -08:00
import { useTranslation } from 'next-i18next'
2023-01-24 16:54:24 -08:00
import FormatNumericValue from './FormatNumericValue'
2022-08-03 02:49:31 -07:00
dayjs.extend(relativeTime)
interface DetailedAreaChartProps {
data: any[]
2022-12-05 19:23:22 -08:00
daysToShow?: string
2022-12-06 03:58:22 -08:00
domain?: AxisDomain
2022-12-13 17:25:30 -08:00
heightClass?: string
2022-08-13 03:44:13 -07:00
hideChange?: boolean
2022-08-03 02:49:31 -07:00
hideChart?: () => void
2023-01-19 16:56:22 -08:00
loaderHeightClass?: string
2022-08-03 02:49:31 -07:00
loading?: boolean
2022-12-06 03:58:22 -08:00
prefix?: string
2022-12-05 19:23:22 -08:00
setDaysToShow?: (x: string) => void
2022-12-06 03:58:22 -08:00
small?: boolean
suffix?: string
tickFormat?: (x: number) => string
2022-08-03 02:49:31 -07:00
title?: string
xKey: string
2023-02-10 04:55:06 -08:00
yDecimals?: number
2022-08-03 02:49:31 -07:00
yKey: string
}
export const formatDateAxis = (date: string, days: number) => {
2022-10-21 05:54:18 -07:00
if (days === 1) {
return dayjs(date).format('h:mma')
} else {
return dayjs(date).format('D MMM')
}
}
2022-08-03 02:49:31 -07:00
const DetailedAreaChart: FunctionComponent<DetailedAreaChartProps> = ({
data,
2022-11-29 21:30:18 -08:00
daysToShow = '1',
2022-12-06 03:58:22 -08:00
domain,
2022-12-13 17:25:30 -08:00
heightClass,
2022-08-13 03:44:13 -07:00
hideChange,
2022-08-03 02:49:31 -07:00
hideChart,
2023-01-19 16:56:22 -08:00
loaderHeightClass,
2022-08-03 02:49:31 -07:00
loading,
2022-12-06 03:58:22 -08:00
prefix = '',
2022-08-10 21:09:58 -07:00
setDaysToShow,
2022-12-06 03:58:22 -08:00
small,
suffix = '',
2022-08-10 21:09:58 -07:00
tickFormat,
2022-08-03 02:49:31 -07:00
title,
xKey,
2023-02-10 04:55:06 -08:00
yDecimals,
2022-08-03 02:49:31 -07:00
yKey,
}) => {
2022-12-11 15:21:40 -08:00
const { t } = useTranslation('common')
2022-08-03 02:49:31 -07:00
const [mouseData, setMouseData] = useState<any>(null)
const { theme } = useTheme()
2022-11-23 18:30:20 -08:00
const [animationSettings] = useLocalStorageState(
ANIMATION_SETTINGS_KEY,
INITIAL_ANIMATION_SETTINGS
)
2022-08-03 02:49:31 -07:00
const handleMouseMove = (coords: any) => {
if (coords.activePayload) {
setMouseData(coords.activePayload[0].payload)
}
}
const handleMouseLeave = () => {
setMouseData(null)
}
2023-02-10 04:55:06 -08:00
const flipGradientCoords = useMemo(() => {
if (!data.length) return
return data[0][yKey] <= 0 && data[data.length - 1][yKey] <= 0
}, [data])
const filteredData = useMemo(() => {
if (!data.length) return []
2023-02-11 04:40:23 -08:00
const start = Number(daysToShow) * 86400000
const filtered = data.filter((d: any) => {
const dataTime = new Date(d[xKey]).getTime()
const now = new Date().getTime()
const limit = now - start
return dataTime >= limit
})
return filtered
2023-02-10 04:55:06 -08:00
}, [data, daysToShow])
2022-08-03 02:49:31 -07:00
const calculateChartChange = () => {
2023-02-10 04:55:06 -08:00
if (filteredData.length) {
2022-08-03 02:49:31 -07:00
if (mouseData) {
2023-02-10 04:55:06 -08:00
const index = filteredData.findIndex(
(d: any) => d[xKey] === mouseData[xKey]
)
const change =
index >= 0 ? filteredData[index][yKey] - filteredData[0][yKey] : 0
2022-08-10 21:09:58 -07:00
return isNaN(change) ? 0 : change
2023-02-10 04:55:06 -08:00
} else
return (
filteredData[filteredData.length - 1][yKey] - filteredData[0][yKey]
)
2022-08-03 02:49:31 -07:00
}
return 0
}
return (
<FadeInFadeOut show={true}>
<ContentBox hideBorder hidePadding>
{loading ? (
2022-09-08 18:00:47 -07:00
<SheenLoader className="flex flex-1">
2022-12-13 17:25:30 -08:00
<div
className={`${
2023-01-19 16:56:22 -08:00
loaderHeightClass ? loaderHeightClass : 'h-96'
2022-12-13 17:25:30 -08:00
} w-full rounded-lg bg-th-bkg-2`}
/>
2022-08-03 02:49:31 -07:00
</SheenLoader>
2023-02-10 04:55:06 -08:00
) : filteredData.length ? (
2022-08-03 02:49:31 -07:00
<div className="relative">
<div className="flex items-start justify-between">
<div className="flex flex-col md:flex-row md:items-start md:space-x-6">
2022-12-05 19:23:22 -08:00
{hideChart ? (
2023-01-14 03:53:28 -08:00
<IconButton
className="mb-6"
onClick={hideChart}
size="medium"
>
2022-12-05 19:23:22 -08:00
<ArrowLeftIcon className="h-5 w-5" />
</IconButton>
) : null}
2022-08-03 02:49:31 -07:00
<div>
2022-12-06 03:58:22 -08:00
<p
className={`${
small ? 'text-sm' : 'mb-0.5 text-base'
} text-th-fgd-3`}
>
{title}
</p>
2022-08-03 02:49:31 -07:00
{mouseData ? (
<div>
2022-12-06 03:58:22 -08:00
<div
className={`flex ${
small
? 'h-8 items-center text-2xl'
: 'mb-1 items-end text-4xl'
2022-12-13 02:49:56 -08:00
} font-display text-th-fgd-1`}
2022-12-06 03:58:22 -08:00
>
2022-11-23 18:30:20 -08:00
{animationSettings['number-scroll'] ? (
<FlipNumbers
2022-12-06 14:52:36 -08:00
height={small ? 24 : 40}
2022-12-13 15:16:50 -08:00
width={small ? 17 : 30}
2022-11-23 18:30:20 -08:00
play
numbers={`${
mouseData[yKey] < 0 ? '-' : ''
2023-01-24 16:54:24 -08:00
}${prefix}${formatNumericValue(
2023-02-10 04:55:06 -08:00
Math.abs(mouseData[yKey]),
yDecimals
)}${suffix}`}
2022-11-23 18:30:20 -08:00
/>
) : (
<span>
2023-01-24 16:54:24 -08:00
{mouseData[yKey] < 0 ? '-' : ''}
{prefix}
<FormatNumericValue
value={Math.abs(mouseData[yKey])}
2023-02-10 04:55:06 -08:00
decimals={yDecimals}
2023-01-24 16:54:24 -08:00
/>
{suffix}
2022-11-23 18:30:20 -08:00
</span>
)}
2022-08-13 03:44:13 -07:00
{!hideChange ? (
2022-09-08 18:00:47 -07:00
<span className="ml-3">
2022-11-20 03:23:23 -08:00
<Change
change={calculateChartChange()}
2023-02-10 04:55:06 -08:00
decimals={yDecimals}
2022-12-06 03:58:22 -08:00
prefix={prefix}
suffix={suffix}
2022-11-20 03:23:23 -08:00
/>
2022-08-03 02:49:31 -07:00
</span>
2022-08-13 03:44:13 -07:00
) : null}
2022-08-03 02:49:31 -07:00
</div>
2022-12-06 03:58:22 -08:00
<p
className={`${
small ? 'text-xs' : 'text-sm'
} text-th-fgd-4`}
>
2022-08-03 02:49:31 -07:00
{dayjs(mouseData[xKey]).format('DD MMM YY, h:mma')}
</p>
</div>
) : (
<div>
2022-12-06 03:58:22 -08:00
<div
className={`flex ${
small
? 'h-8 items-center text-2xl'
: 'mb-1 items-end text-4xl'
2022-12-13 02:49:56 -08:00
} font-display text-th-fgd-1`}
2022-12-06 03:58:22 -08:00
>
2022-11-23 18:30:20 -08:00
{animationSettings['number-scroll'] ? (
<FlipNumbers
2022-12-06 14:52:36 -08:00
height={small ? 24 : 40}
2022-12-13 15:16:50 -08:00
width={small ? 17 : 30}
2022-11-23 18:30:20 -08:00
play
numbers={`${
2023-02-10 04:55:06 -08:00
filteredData[filteredData.length - 1][yKey] < 0
? '-'
: ''
2023-01-24 16:54:24 -08:00
}${prefix}${formatNumericValue(
2023-02-10 04:55:06 -08:00
Math.abs(
filteredData[filteredData.length - 1][yKey]
),
yDecimals
)}${suffix}`}
2022-11-23 18:30:20 -08:00
/>
) : (
<span>
2023-02-10 04:55:06 -08:00
{filteredData[filteredData.length - 1][yKey] < 0
? '-'
: ''}
2023-01-24 16:54:24 -08:00
{prefix}
<FormatNumericValue
value={Math.abs(data[data.length - 1][yKey])}
2023-02-10 04:55:06 -08:00
decimals={yDecimals}
2023-01-24 16:54:24 -08:00
/>
{suffix}
2022-11-23 18:30:20 -08:00
</span>
)}
2022-08-13 03:44:13 -07:00
{!hideChange ? (
2022-09-08 18:00:47 -07:00
<span className="ml-3">
2022-11-20 03:23:23 -08:00
<Change
change={calculateChartChange()}
2023-02-10 04:55:06 -08:00
decimals={yDecimals}
2022-12-06 03:58:22 -08:00
prefix={prefix}
suffix={suffix}
2022-11-20 03:23:23 -08:00
/>
2022-08-03 02:49:31 -07:00
</span>
2022-08-13 03:44:13 -07:00
) : null}
2022-08-03 02:49:31 -07:00
</div>
2022-12-06 03:58:22 -08:00
<p
className={`${
small ? 'text-xs' : 'text-sm'
} text-th-fgd-4`}
>
2023-02-10 04:55:06 -08:00
{dayjs(
filteredData[filteredData.length - 1][xKey]
).format('DD MMM YY, h:mma')}
2022-08-03 02:49:31 -07:00
</p>
</div>
)}
</div>
</div>
</div>
2022-12-13 17:25:30 -08:00
<div
className={`-mt-1 ${heightClass ? heightClass : 'h-96'} w-auto`}
>
2022-12-05 19:23:22 -08:00
{setDaysToShow ? (
<div className="absolute -top-1 right-0 -mb-2 flex justify-end">
<ChartRangeButtons
activeValue={daysToShow}
names={['24H', '7D', '30D']}
values={['1', '7', '30']}
onChange={(v) => setDaysToShow(v)}
/>
</div>
) : null}
2022-08-03 02:49:31 -07:00
<div className="-mx-6 mt-6 h-full">
<ResponsiveContainer width="100%" height="100%">
<AreaChart
2023-02-10 04:55:06 -08:00
data={filteredData}
2022-08-03 02:49:31 -07:00
onMouseMove={handleMouseMove}
onMouseLeave={handleMouseLeave}
>
<Tooltip
cursor={{
strokeOpacity: 0.09,
}}
content={<></>}
/>
<defs>
<linearGradient
2022-12-13 19:02:23 -08:00
id={`gradientArea-${title?.replace(/\s/g, '')}`}
2022-08-03 02:49:31 -07:00
x1="0"
y1={flipGradientCoords ? '1' : '0'}
2022-08-03 02:49:31 -07:00
x2="0"
y2={flipGradientCoords ? '0' : '1'}
2022-08-03 02:49:31 -07:00
>
<stop
offset="0%"
stopColor={
calculateChartChange() >= 0
2022-11-30 19:32:32 -08:00
? COLORS.UP[theme]
: COLORS.DOWN[theme]
2022-08-03 02:49:31 -07:00
}
stopOpacity={0.15}
/>
<stop
offset="99%"
stopColor={
calculateChartChange() >= 0
2022-11-30 19:32:32 -08:00
? COLORS.UP[theme]
: COLORS.DOWN[theme]
2022-08-03 02:49:31 -07:00
}
stopOpacity={0}
/>
</linearGradient>
</defs>
<Area
isAnimationActive={false}
type="monotone"
dataKey={yKey}
stroke={
calculateChartChange() >= 0
2022-11-30 19:32:32 -08:00
? COLORS.UP[theme]
: COLORS.DOWN[theme]
2022-08-03 02:49:31 -07:00
}
strokeWidth={1.5}
2022-12-13 19:02:23 -08:00
fill={`url(#gradientArea-${title?.replace(/\s/g, '')})`}
2022-08-03 02:49:31 -07:00
/>
<XAxis
axisLine={false}
dataKey={xKey}
2022-12-06 03:58:22 -08:00
minTickGap={20}
2022-08-03 02:49:31 -07:00
padding={{ left: 20, right: 20 }}
tick={{
fill: 'var(--fgd-4)',
2022-08-03 02:49:31 -07:00
fontSize: 10,
}}
tickLine={false}
2022-11-29 21:30:18 -08:00
tickFormatter={(d) =>
formatDateAxis(d, parseInt(daysToShow))
}
2022-08-03 02:49:31 -07:00
/>
<YAxis
axisLine={false}
dataKey={yKey}
2022-12-06 03:58:22 -08:00
minTickGap={20}
2022-08-03 02:49:31 -07:00
type="number"
domain={
domain
? domain
: ([dataMin, dataMax]) => {
2023-02-13 19:31:01 -08:00
const difference = dataMax - dataMin
if (difference < 0.01) {
return [dataMin - 0.001, dataMax + 0.001]
} else if (difference < 0.1) {
return [dataMin - 0.01, dataMax + 0.01]
2023-02-03 13:37:52 -08:00
} else if (difference < 1) {
2023-02-13 19:31:01 -08:00
return [dataMin - 0.1, dataMax + 0.11]
2023-02-03 13:37:52 -08:00
} else if (difference < 10) {
2023-02-10 10:50:32 -08:00
return [dataMin - 1, dataMax + 1]
} else {
return [dataMin, dataMax]
}
}
}
2022-08-03 02:49:31 -07:00
padding={{ top: 20, bottom: 20 }}
tick={{
fill: 'var(--fgd-4)',
2022-08-03 02:49:31 -07:00
fontSize: 10,
}}
2022-08-10 21:09:58 -07:00
tickFormatter={
tickFormat ? (v) => tickFormat(v) : undefined
}
2022-08-03 02:49:31 -07:00
tickLine={false}
/>
</AreaChart>
</ResponsiveContainer>
</div>
</div>
</div>
) : (
2022-12-13 17:25:30 -08:00
<div
className={`flex ${
heightClass ? heightClass : 'h-96'
} items-center justify-center p-4 text-th-fgd-3`}
>
2022-08-03 02:49:31 -07:00
<div className="">
2022-12-11 15:21:40 -08:00
<NoSymbolIcon className="mx-auto mb-1 h-6 w-6 text-th-fgd-4" />
<p className="text-th-fgd-4">{t('chart-unavailable')}</p>
2022-08-03 02:49:31 -07:00
</div>
</div>
)}
</ContentBox>
</FadeInFadeOut>
)
}
export default DetailedAreaChart