-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathSmallGraph.tsx
More file actions
148 lines (137 loc) · 3.64 KB
/
SmallGraph.tsx
File metadata and controls
148 lines (137 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { type FC, Fragment } from 'react';
import Box from '@mui/material/Box';
import Card from '@mui/material/Card';
import Stack from '@mui/material/Stack';
import { useTheme } from '@mui/material/styles';
import Typography from '@mui/material/Typography';
import {
Area,
AreaChart,
CartesianGrid,
ResponsiveContainer,
Tooltip,
type TooltipProps,
XAxis,
YAxis,
} from 'recharts';
import { useIsMobile } from '@/shared/hooks/useBreakpoints';
import formatDate from '../lib/formatDate';
import formatNumber from '../lib/formatNumber';
import ToggleButtons from './ToggleButtons';
const CustomSmallChartTooltip: FC<TooltipProps<number, string>> = ({
payload,
active,
}) => {
if (!active) return null;
return (
<Card
elevation={0}
sx={{
border: '1px solid',
borderColor: 'fog.light',
borderRadius: 2,
}}
>
<Box px={1} py={0}>
{payload?.map((elem) => (
<Fragment key={elem.name}>
<Typography variant="tooltip">
{formatDate(elem.payload.date, 'MMMM DD, YYYY')}
</Typography>
<Typography fontWeight={500} variant="body1">
{elem.value ? elem.value.toLocaleString('en-US') : ''}
</Typography>
</Fragment>
))}
</Box>
</Card>
);
};
type SmallGraphProps = {
graphData: {
date: string;
value: number;
}[];
title: string;
};
const GraphSettings = ({ title }: { title: string }) => (
<Stack
direction={{ sm: 'column', md: 'row' }}
justifyContent="end"
alignItems="center"
mt={{ xs: 1.5, md: 0 }}
mb={{ xs: 0, md: 2 }}
gap={2}
flexWrap="wrap"
>
<Typography variant="body1" component="p">
{title}
</Typography>
<ToggleButtons />
</Stack>
);
const SmallGraph: FC<SmallGraphProps> = ({ title, graphData }) => {
const isMobile = useIsMobile();
const theme = useTheme();
return (
<>
{!isMobile && <GraphSettings title={title} />}
<ResponsiveContainer height={isMobile ? 150 : 215}>
<AreaChart
data={graphData}
margin={{
top: 5,
right: 50,
left: 20,
}}
>
<defs>
<linearGradient id="value" x1="0" y1="0" x2="0" y2="1">
<stop offset="90%" stopColor="#244CB20F" stopOpacity={0.9} />
<stop offset="100%" stopColor="#B4C2E505" stopOpacity={0} />
</linearGradient>
</defs>
<XAxis
style={{
fontSize: 10,
fontWeight: 500,
}}
axisLine={false}
interval="preserveStartEnd"
dataKey="date"
stroke={theme.palette.fog.main}
tickFormatter={(value) => formatDate(value, 'DD MMMM')}
tick={{ dy: 10 }}
tickSize={0}
/>
<YAxis
style={{
fontSize: 10,
fontWeight: 500,
}}
axisLine={false}
dataKey="value"
tick={{ dx: -10 }}
tickSize={0}
stroke={theme.palette.fog.main}
tickFormatter={formatNumber}
/>
<CartesianGrid
stroke={theme.palette.fog.light}
strokeDasharray={1}
vertical={false}
/>
<Tooltip content={<CustomSmallChartTooltip />} />
<Area
type="monotone"
dataKey="value"
stroke={theme.palette.secondary.main}
fill="url(#value)"
/>
</AreaChart>
</ResponsiveContainer>
{isMobile && <GraphSettings title={title} />}
</>
);
};
export default SmallGraph;