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

70 lines
1.9 KiB
TypeScript
Raw Normal View History

2023-02-27 23:20:11 -08:00
/* eslint-disable @typescript-eslint/no-explicit-any */
import { useMemo } from 'react'
2022-12-21 03:07:17 -08:00
import { Area, AreaChart, ResponsiveContainer, XAxis, YAxis } from 'recharts'
2022-08-02 17:17:42 -07:00
const SimpleAreaChart = ({
color,
data,
name,
2022-08-10 21:09:58 -07:00
xKey,
yKey,
2022-08-02 17:17:42 -07:00
}: {
color: string
data: any[]
name: string
2022-08-10 21:09:58 -07:00
xKey: string
yKey: string
2022-08-02 17:17:42 -07:00
}) => {
2023-01-10 20:39:02 -08:00
const flipGradientCoords = useMemo(() => {
if (!data.length) return
return data[0][yKey] <= 0 && data[data.length - 1][yKey] <= 0
}, [data])
2022-08-02 17:17:42 -07:00
return (
2022-12-21 03:07:17 -08:00
<ResponsiveContainer width="100%" height="100%">
<AreaChart data={data}>
<defs>
<linearGradient
2023-04-03 04:01:17 -07:00
id={`gradientArea-${name.replace(/[^a-zA-Z]/g, '')}`}
2022-12-21 03:07:17 -08:00
x1="0"
y1={flipGradientCoords ? '0' : '1'}
x2="0"
y2={flipGradientCoords ? '1' : '0'}
>
<stop offset="0%" stopColor={color} stopOpacity={0} />
<stop offset="100%" stopColor={color} stopOpacity={0.6} />
</linearGradient>
</defs>
<Area
type="monotone"
dataKey={yKey}
stroke={color}
2023-04-03 04:01:17 -07:00
fill={`url(#gradientArea-${name.replace(/[^a-zA-Z]/g, '')}`}
2022-12-21 03:07:17 -08:00
/>
<XAxis dataKey={xKey} hide />
<YAxis
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-10 10:50:32 -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]
}
}}
dataKey={yKey}
hide
/>
2022-12-21 03:07:17 -08:00
</AreaChart>
</ResponsiveContainer>
2022-08-02 17:17:42 -07:00
)
}
export default SimpleAreaChart