-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLineChart.tsx
197 lines (178 loc) · 5.3 KB
/
LineChart.tsx
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { useQuery } from '@apollo/client';
import React, { useEffect, useState } from 'react';
import {
LineChart,
Line,
CartesianGrid,
XAxis,
YAxis,
Tooltip,
Legend,
ResponsiveContainer,
} from 'recharts';
import dayjs from 'dayjs';
import isoWeek from 'dayjs/plugin/isoWeek';
import GET_ROLE_QUERY from '../containers/admin-dashBoard/GetRolesQuery';
dayjs.extend(isoWeek);
function UserGrowth() {
const [orgToken, setOrgToken] = useState<string | null>(null);
const [period, setPeriod] = useState<'daily' | 'weekly' | 'monthly'>('daily');
const [selectedYear, setSelectedYear] = useState<string>(
dayjs().year().toString(),
);
useEffect(() => {
const token = localStorage.getItem('orgToken');
setOrgToken(token);
}, []);
const { data, loading, error } = useQuery(GET_ROLE_QUERY, {
variables: { orgToken },
skip: !orgToken,
});
if (loading) {
return <p>Loading...</p>;
}
if (error) {
return <p>Error: {error.message}</p>;
}
const users = data?.getAllUsers || [];
const userGrowth = (users: any[]) => {
const growthData: { [key: string]: number } = {};
users.forEach((user: any) => {
const timestamp = user.createdAt || user.updatedAt;
if (timestamp) {
const date = dayjs(parseInt(timestamp, 10));
const year = date.year().toString();
if (year === selectedYear) {
let periodKey = '';
if (period === 'daily') {
periodKey = date.format('YYYY-MM-DD');
} else if (period === 'weekly') {
periodKey = `${date.year()}-W${date.isoWeek()}`;
} else if (period === 'monthly') {
periodKey = date.format('YYYY-MM');
}
growthData[periodKey] = (growthData[periodKey] || 0) + 1;
}
}
});
const allMonths = Array.from({ length: 12 }, (_, i) =>
dayjs().month(i).format('YYYY-MM'),
);
allMonths.forEach((month) => {
if (!growthData[month]) {
growthData[month] = 0;
}
});
return Object.entries(growthData).map(([date, count]) => ({ date, count }));
};
const growthData = userGrowth(users);
return (
<div className="px-9 py-10 bg-tertiary dark:bg-dark-bg">
<h3 className="text-3xl text-center text-grey-600 font-bold">
User Growth
</h3>
<div className="flex flex-wrap flex-row justify-between">
<div>
<label
htmlFor="year"
style={{ color: '#bdbdbd', marginRight: '10px' }}
>
Year:
</label>
<select
id="year"
value={selectedYear}
onChange={(e) => setSelectedYear(e.target.value)}
style={{
padding: '5px 10px',
borderColor: '#ccc',
borderRadius: '4px',
color: '#bdbdbd',
}}
>
{Array.from({ length: 5 }).map((_, index) => {
const year = dayjs().year() - index;
return (
<option key={year} value={year.toString()}>
{year}
</option>
);
})}
</select>
</div>
<div>
<label
htmlFor="period"
style={{ color: '#bdbdbd', marginRight: '10px' }}
>
Period:
</label>
<select
id="period"
value={period}
onChange={(e) =>
setPeriod(e.target.value as 'daily' | 'weekly' | 'monthly')
}
style={{
padding: '5px 10px',
borderColor: '#ccc',
borderRadius: '4px',
color: '#bdbdbd',
}}
>
<option value="daily">Daily</option>
<option value="weekly">Weekly</option>
<option value="monthly">Monthly</option>
</select>
</div>
</div>
{growthData.length === 0 ? (
<p>No data available</p>
) : (
<div>
<ResponsiveContainer width="100%" height={400}>
<LineChart
data={growthData}
margin={{ top: 20, right: 30, left: 20, bottom: 20 }}
>
<CartesianGrid strokeDasharray="3 3" />
<XAxis
dataKey="date"
tickFormatter={(str) => {
if (period === 'daily') {
return dayjs(str).format('MMM DD');
}
if (period === 'weekly') {
return str.split('-')[1];
}
if (period === 'monthly') {
return dayjs(str).format('MMM YYYY');
}
return str;
}}
/>
<YAxis
label={{
value: 'USERS',
angle: -90,
position: 'insideLeft',
style: { fontSize: 14, fill: '#bdbdbd' },
}}
/>
<Tooltip />
<Legend />
<Line
type="monotone"
dataKey="count"
stroke="#8884d8"
name="User(s)"
dot={false}
/>
</LineChart>
</ResponsiveContainer>
</div>
)}
</div>
);
}
export default UserGrowth;