|
| 1 | +/* eslint-disable no-console */ |
| 2 | +/* eslint-disable no-restricted-syntax */ |
| 3 | +/* eslint-disable no-plusplus */ |
| 4 | +import React from 'react'; |
| 5 | +import { Line } from 'react-chartjs-2'; |
| 6 | +import { |
| 7 | + Chart as ChartJS, |
| 8 | + CategoryScale, |
| 9 | + LinearScale, |
| 10 | + PointElement, |
| 11 | + LineElement, |
| 12 | + Title, |
| 13 | + Tooltip, |
| 14 | + Legend, |
| 15 | +} from 'chart.js'; |
| 16 | + |
| 17 | +ChartJS.register( |
| 18 | + CategoryScale, |
| 19 | + LinearScale, |
| 20 | + PointElement, |
| 21 | + LineElement, |
| 22 | + Title, |
| 23 | + Tooltip, |
| 24 | + Legend, |
| 25 | +); |
| 26 | + |
| 27 | +interface TeamChartProps { |
| 28 | + timeframe?: 'daily' | 'weekly' | 'monthly'; |
| 29 | + CurrentTeam: any[]; |
| 30 | + loginsbyDate: any[]; |
| 31 | +} |
| 32 | + |
| 33 | +function TeamChart({ |
| 34 | + timeframe = 'daily', |
| 35 | + CurrentTeam, |
| 36 | + loginsbyDate, |
| 37 | +}: TeamChartProps) { |
| 38 | + function organizeLoginData(loginData: any) { |
| 39 | + const currentDate = new Date(); |
| 40 | + const currentYear = currentDate.getFullYear(); |
| 41 | + function getWeekNumber(date: any) { |
| 42 | + const tempDate: any = new Date(date); |
| 43 | + tempDate.setUTCDate( |
| 44 | + tempDate.getUTCDate() + 4 - (tempDate.getUTCDay() || 7), |
| 45 | + ); |
| 46 | + const yearStart: any = new Date( |
| 47 | + Date.UTC(tempDate.getUTCFullYear(), 0, 1), |
| 48 | + ); |
| 49 | + return Math.ceil(((tempDate - yearStart) / 86400000 + 1) / 7); |
| 50 | + } |
| 51 | + // Initialize result arrays |
| 52 | + const weeklyData = Array(54) |
| 53 | + .fill(0) |
| 54 | + .map((_, i) => ({ week: i + 1, success: 0, failed: 0 })); |
| 55 | + const monthlyData = Array(12) |
| 56 | + .fill(0) |
| 57 | + .map((_, i) => ({ month: i + 1, success: 0, failed: 0 })); |
| 58 | + const dailyData = Array(7) |
| 59 | + .fill(0) |
| 60 | + .map((_, i) => ({ day: i, success: 0, failed: 0 })); |
| 61 | + for (const [dateString, { success, failed }] of Object.entries( |
| 62 | + loginData, |
| 63 | + ) as any) { |
| 64 | + const date = new Date(dateString); |
| 65 | + const isoWeekNumber = getWeekNumber(date); |
| 66 | + const month = date.getUTCMonth(); |
| 67 | + const dayOfWeek = (date.getUTCDay() + 6) % 7; |
| 68 | + const weekStart = new Date(currentDate); |
| 69 | + weekStart.setUTCDate( |
| 70 | + currentDate.getUTCDate() - currentDate.getUTCDay() + 1, |
| 71 | + ); |
| 72 | + const weekEnd = new Date(weekStart); |
| 73 | + weekEnd.setUTCDate(weekStart.getUTCDate() + 6); |
| 74 | + if (date >= weekStart && date <= weekEnd) { |
| 75 | + dailyData[dayOfWeek].success += success; |
| 76 | + dailyData[dayOfWeek].failed += failed; |
| 77 | + } |
| 78 | + // Weekly data |
| 79 | + if (isoWeekNumber <= 54) { |
| 80 | + weeklyData[isoWeekNumber - 1].success += success; |
| 81 | + weeklyData[isoWeekNumber - 1].failed += failed; |
| 82 | + } |
| 83 | + // Monthly data |
| 84 | + monthlyData[month].success += success; |
| 85 | + monthlyData[month].failed += failed; |
| 86 | + } |
| 87 | + const weekDays = [ |
| 88 | + 'Monday', |
| 89 | + 'Tuesday', |
| 90 | + 'Wednesday', |
| 91 | + 'Thursday', |
| 92 | + 'Friday', |
| 93 | + 'Saturday', |
| 94 | + 'Sunday', |
| 95 | + ]; |
| 96 | + const currentWeekData = dailyData.map((data, index) => ({ |
| 97 | + day: weekDays[index], |
| 98 | + success: data.success, |
| 99 | + failed: data.failed, |
| 100 | + })); |
| 101 | + return { |
| 102 | + currentWeek: currentWeekData, |
| 103 | + weekly: weeklyData, |
| 104 | + monthly: monthlyData.map((data, index) => ({ |
| 105 | + month: new Date(0, index).toLocaleString('en', { month: 'long' }), |
| 106 | + success: data.success, |
| 107 | + failed: data.failed, |
| 108 | + })), |
| 109 | + }; |
| 110 | + } |
| 111 | + |
| 112 | + const organizedData = organizeLoginData(loginsbyDate); |
| 113 | + |
| 114 | + const weeklyDataset = organizedData.weekly |
| 115 | + .filter((_, index) => index % 3 === 0) |
| 116 | + .map((item) => item.success); |
| 117 | + |
| 118 | + const chartData = { |
| 119 | + daily: { |
| 120 | + labels: ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su'], |
| 121 | + datasets: [ |
| 122 | + { |
| 123 | + label: CurrentTeam[0].name, |
| 124 | + data: organizedData.currentWeek.map((item: any) => item.success), |
| 125 | + fill: false, |
| 126 | + borderColor: '#4F46E5', |
| 127 | + tension: 0.4, |
| 128 | + }, |
| 129 | + ], |
| 130 | + }, |
| 131 | + weekly: { |
| 132 | + labels: [ |
| 133 | + '03', |
| 134 | + '06', |
| 135 | + '09', |
| 136 | + '12', |
| 137 | + '15', |
| 138 | + '18', |
| 139 | + '21', |
| 140 | + '24', |
| 141 | + '27', |
| 142 | + '30', |
| 143 | + '31', |
| 144 | + '34', |
| 145 | + '37', |
| 146 | + '40', |
| 147 | + '43', |
| 148 | + '46', |
| 149 | + '49', |
| 150 | + '54', |
| 151 | + ], |
| 152 | + datasets: [ |
| 153 | + { |
| 154 | + label: CurrentTeam[0].name, |
| 155 | + data: weeklyDataset, |
| 156 | + fill: false, |
| 157 | + borderColor: '#4F46E5', |
| 158 | + tension: 0.4, |
| 159 | + }, |
| 160 | + ], |
| 161 | + }, |
| 162 | + monthly: { |
| 163 | + labels: Array.from({ length: 12 }, (_, i) => |
| 164 | + String(i + 1).padStart(2, '0'), |
| 165 | + ), |
| 166 | + datasets: [ |
| 167 | + { |
| 168 | + label: CurrentTeam[0].name, |
| 169 | + data: organizedData.monthly.map((item: any) => item.success), |
| 170 | + fill: false, |
| 171 | + borderColor: '#4F46E5', |
| 172 | + tension: 0.4, |
| 173 | + }, |
| 174 | + ], |
| 175 | + }, |
| 176 | + }; |
| 177 | + |
| 178 | + const options = { |
| 179 | + responsive: true, |
| 180 | + maintainAspectRatio: false, |
| 181 | + plugins: { |
| 182 | + legend: { |
| 183 | + position: 'bottom' as const, |
| 184 | + }, |
| 185 | + tooltip: { |
| 186 | + mode: 'index' as const, |
| 187 | + intersect: false, |
| 188 | + }, |
| 189 | + }, |
| 190 | + scales: { |
| 191 | + y: { |
| 192 | + beginAtZero: true, |
| 193 | + grid: { |
| 194 | + color: '#D1D5DB', |
| 195 | + }, |
| 196 | + ticks: { |
| 197 | + color: '#6B7280', |
| 198 | + }, |
| 199 | + }, |
| 200 | + x: { |
| 201 | + grid: { |
| 202 | + display: false, |
| 203 | + }, |
| 204 | + ticks: { |
| 205 | + color: '#6B7280', |
| 206 | + }, |
| 207 | + }, |
| 208 | + }, |
| 209 | + }; |
| 210 | + |
| 211 | + return ( |
| 212 | + <div className="w-full max-w-4xl h-[60vh] max-h-[500px] mx-auto p-4 md:p-6 lg:p-8 bg-white dark:bg-gray-800 rounded-md shadow-md"> |
| 213 | + <Line data={chartData[timeframe]} options={options} /> |
| 214 | + </div> |
| 215 | + ); |
| 216 | +} |
| 217 | + |
| 218 | +export default TeamChart; |
0 commit comments