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

54 lines
1.2 KiB
TypeScript
Raw Normal View History

import { useMemo } from 'react'
2022-08-02 17:17:42 -07:00
import { Area, AreaChart, XAxis, YAxis } from 'recharts'
const SimpleAreaChart = ({
color,
data,
height,
name,
width,
2022-08-10 21:09:58 -07:00
xKey,
yKey,
2022-08-02 17:17:42 -07:00
}: {
color: string
data: any[]
height: number
name: string
width: number
2022-08-10 21:09:58 -07:00
xKey: string
yKey: string
2022-08-02 17:17:42 -07:00
}) => {
const flipGradientCoords = useMemo(
() => data[0][yKey] <= 0 && data[data.length - 1][yKey] < data[0][yKey],
[data]
)
2022-08-02 17:17:42 -07:00
return (
<AreaChart width={width} height={height} data={data}>
<defs>
<linearGradient
id={`gradientArea-${name}`}
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} />
2022-08-02 17:17:42 -07:00
</linearGradient>
</defs>
<Area
isAnimationActive={false}
type="monotone"
2022-08-10 21:09:58 -07:00
dataKey={yKey}
2022-08-02 17:17:42 -07:00
stroke={color}
fill={`url(#gradientArea-${name})`}
/>
2022-08-10 21:09:58 -07:00
<XAxis dataKey={xKey} hide />
<YAxis domain={['dataMin', 'dataMax']} dataKey={yKey} hide />
2022-08-02 17:17:42 -07:00
</AreaChart>
)
}
export default SimpleAreaChart