|
| 1 | +import { ComponentProps, useMemo, useState } from 'react'; |
| 2 | + |
| 3 | +import { |
| 4 | + BarChart, |
| 5 | + Bar, |
| 6 | + XAxis, |
| 7 | + YAxis, |
| 8 | + Tooltip, |
| 9 | + ResponsiveContainer, |
| 10 | + CartesianGrid, |
| 11 | + Cell, |
| 12 | +} from 'recharts'; |
| 13 | +import { msToTimeString } from 'shared/util'; |
| 14 | + |
| 15 | +import { Stats } from '@/entities/stats'; |
| 16 | +import { cn, isoDurationToMs } from '@/shared/utils'; |
| 17 | + |
| 18 | +export type StatsChartProps = { |
| 19 | + dataFromServer: Stats['weaklyFocusTimeTrend']; |
| 20 | +}; |
| 21 | + |
| 22 | +type YAxisProps = ComponentProps<typeof YAxis>; |
| 23 | + |
| 24 | +const oneMinute = 60 * 1000; // 1분을 ms로 변환 |
| 25 | +const oneHour = 60 * oneMinute; // 1시간을 ms로 변환 |
| 26 | + |
| 27 | +const toFormattedDate = (str: string) => { |
| 28 | + const date = new Date(str); |
| 29 | + return `${date.getMonth() + 1}/${date.getDate()}`; |
| 30 | +}; |
| 31 | +const toFixed = (num: number, digit = 2) => Number(num.toFixed(digit)); |
| 32 | +const toMinutes = (ms: number) => toFixed(ms / oneMinute); |
| 33 | +const toHours = (ms: number) => toFixed(ms / oneHour); |
| 34 | + |
| 35 | +const range = ({ start, step, count }: { start: number; step: number; count: number }) => { |
| 36 | + return Array.from({ length: count + 1 }, (_, i) => start + i * step); |
| 37 | +}; |
| 38 | +const minmax = (min: number, value: number, max: number) => { |
| 39 | + return Math.min(Math.max(value, min), max); |
| 40 | +}; |
| 41 | + |
| 42 | +export const StatsChart = ({ dataFromServer }: StatsChartProps) => { |
| 43 | + const [activeIndex, setActiveIndex] = useState(6); |
| 44 | + const [tooltipPosition, setTooltipPosition] = useState<{ x: number; y: number } | undefined>(); |
| 45 | + |
| 46 | + const dataWithMs = useMemo(() => { |
| 47 | + return dataFromServer.dateToFocusTimeStatistics.map((item) => ({ |
| 48 | + date: item.date, |
| 49 | + time: isoDurationToMs(item.totalFocusTime), |
| 50 | + })); |
| 51 | + }, [dataFromServer]); |
| 52 | + |
| 53 | + const totalFocusTime = useMemo(() => { |
| 54 | + const totalMs = dataWithMs.reduce((acc, item) => acc + item.time, 0); |
| 55 | + if (totalMs === 0) return '0분'; |
| 56 | + return msToTimeString(totalMs); |
| 57 | + }, [dataWithMs]); |
| 58 | + |
| 59 | + const { data, ticks, tickUnit } = useMemo<{ |
| 60 | + ticks: YAxisProps['ticks']; |
| 61 | + tickUnit: 'minute' | 'hour'; |
| 62 | + data: typeof dataWithMs; |
| 63 | + }>(() => { |
| 64 | + const maxTime = Math.max(...dataWithMs.map((item) => item.time)); |
| 65 | + |
| 66 | + if (maxTime === 0) { |
| 67 | + return { |
| 68 | + ticks: [0, 10], |
| 69 | + tickUnit: 'minute', |
| 70 | + data: dataWithMs.map((item) => ({ |
| 71 | + date: toFormattedDate(item.date), |
| 72 | + time: toMinutes(item.time), |
| 73 | + })), |
| 74 | + }; |
| 75 | + } |
| 76 | + if (maxTime < oneHour) { |
| 77 | + return { |
| 78 | + ticks: [0, 15, 30, 45, 60], |
| 79 | + tickUnit: 'minute', |
| 80 | + data: dataWithMs.map((item) => ({ |
| 81 | + date: toFormattedDate(item.date), |
| 82 | + time: toMinutes(item.time), |
| 83 | + })), |
| 84 | + }; |
| 85 | + } |
| 86 | + if (maxTime < oneHour * 5) { |
| 87 | + // min: [0, 1, 2] |
| 88 | + // max: [0, 1, 2, 3, 4, 5] |
| 89 | + const ticks = range({ |
| 90 | + start: 0, |
| 91 | + step: 1, |
| 92 | + count: minmax(2, Math.ceil(maxTime / oneHour), 5), |
| 93 | + }); |
| 94 | + return { |
| 95 | + ticks: ticks, |
| 96 | + tickUnit: 'hour', |
| 97 | + data: dataWithMs.map((item) => ({ |
| 98 | + date: toFormattedDate(item.date), |
| 99 | + time: toHours(item.time), |
| 100 | + })), |
| 101 | + }; |
| 102 | + } |
| 103 | + if (maxTime < oneHour * 8) { |
| 104 | + return { |
| 105 | + // [0, 2, 4, 6, 8] |
| 106 | + ticks: range({ start: 0, step: 2, count: 4 }), |
| 107 | + tickUnit: 'hour', |
| 108 | + data: dataWithMs.map((item) => ({ |
| 109 | + date: toFormattedDate(item.date), |
| 110 | + time: toHours(item.time), |
| 111 | + })), |
| 112 | + }; |
| 113 | + } |
| 114 | + if (maxTime < oneHour * 20) { |
| 115 | + // min: [0, 5, 10] |
| 116 | + // max: [0, 5, 10, 15, 20] |
| 117 | + const ticks = range({ |
| 118 | + start: 0, |
| 119 | + step: 5, |
| 120 | + count: minmax(2, Math.ceil(maxTime / (5 * oneHour)), 5), |
| 121 | + }); |
| 122 | + return { |
| 123 | + ticks: ticks, |
| 124 | + tickUnit: 'hour', |
| 125 | + data: dataWithMs.map((item) => ({ |
| 126 | + date: toFormattedDate(item.date), |
| 127 | + time: toHours(item.time), |
| 128 | + })), |
| 129 | + }; |
| 130 | + } |
| 131 | + return { |
| 132 | + // [0, 6, 12, 18, 24] |
| 133 | + ticks: range({ start: 0, step: 6, count: 4 }), |
| 134 | + tickUnit: 'hour', |
| 135 | + data: dataWithMs.map((item) => ({ |
| 136 | + date: toFormattedDate(item.date), |
| 137 | + time: toHours(item.time), |
| 138 | + })), |
| 139 | + }; |
| 140 | + }, [dataWithMs]); |
| 141 | + |
| 142 | + return ( |
| 143 | + <div className="rounded-[16px] bg-white p-4"> |
| 144 | + <h3 className="header-4 mb-[50px] text-text-secondary">총 {totalFocusTime}</h3> |
| 145 | + <ResponsiveContainer width="100%" minHeight={240}> |
| 146 | + <BarChart |
| 147 | + data={data} |
| 148 | + onMouseMove={(state) => { |
| 149 | + if (state.isTooltipActive && state.activeTooltipIndex != null) { |
| 150 | + setActiveIndex(state.activeTooltipIndex); |
| 151 | + |
| 152 | + const barRects = document.querySelectorAll('.recharts-bar-rectangle'); |
| 153 | + const rect = barRects[state.activeTooltipIndex] as SVGGraphicsElement | undefined; |
| 154 | + if (rect) { |
| 155 | + const { x, y, width } = rect.getBBox(); |
| 156 | + setTooltipPosition({ x: x + width / 2, y: y - 8 }); |
| 157 | + } |
| 158 | + } |
| 159 | + }} |
| 160 | + > |
| 161 | + <CartesianGrid vertical={false} strokeDasharray="3 3" /> |
| 162 | + <XAxis |
| 163 | + dataKey="date" |
| 164 | + tick={{ |
| 165 | + fontSize: 11, |
| 166 | + fill: '#8F867E', |
| 167 | + }} |
| 168 | + axisLine={{ stroke: '#DFD8D2' }} |
| 169 | + /> |
| 170 | + <YAxis |
| 171 | + width={36} |
| 172 | + tick={{ |
| 173 | + fontSize: 11, |
| 174 | + fill: '#8F867E', |
| 175 | + }} |
| 176 | + domain={[0, 'dataMax']} |
| 177 | + ticks={ticks} |
| 178 | + tickFormatter={(tick) => `${tick}${tickUnit === 'minute' ? 'm' : 'h'}`} |
| 179 | + axisLine={false} |
| 180 | + tickLine={false} |
| 181 | + orientation="right" |
| 182 | + /> |
| 183 | + <Tooltip |
| 184 | + cursor={false} |
| 185 | + wrapperStyle={{ |
| 186 | + pointerEvents: 'none', |
| 187 | + top: tooltipPosition?.y, |
| 188 | + left: tooltipPosition?.x, |
| 189 | + transform: 'translate(-50%, -100%)', |
| 190 | + }} |
| 191 | + content={(data) => { |
| 192 | + const { active, payload } = data; |
| 193 | + if (!active || !payload || payload.length === 0) return null; |
| 194 | + const realPayload = payload[0].payload; |
| 195 | + if (realPayload.time === 0) return null; |
| 196 | + let content = ''; |
| 197 | + if (tickUnit === 'minute') { |
| 198 | + content = `${realPayload.time}분`; |
| 199 | + } else { |
| 200 | + const hour = Math.floor(realPayload.time); |
| 201 | + const minute = Math.round((realPayload.time - hour) * 60); |
| 202 | + content = [hour > 0 ? `${hour}시간` : '', minute > 0 ? `${minute}분` : ''] |
| 203 | + .filter(Boolean) |
| 204 | + .join('\n'); |
| 205 | + } |
| 206 | + return ( |
| 207 | + <div className="flex flex-col items-center"> |
| 208 | + <div |
| 209 | + className={cn( |
| 210 | + 'caption-sb whitespace-pre-wrap rounded-[8px] bg-icon-primary px-2 py-1 text-center text-white', |
| 211 | + )} |
| 212 | + > |
| 213 | + {content} |
| 214 | + </div> |
| 215 | + <div className="-mt-[7px] h-0 w-0 border-l-[7px] border-r-[7px] border-t-[14px] border-l-transparent border-r-transparent border-t-icon-primary" /> |
| 216 | + </div> |
| 217 | + ); |
| 218 | + }} |
| 219 | + /> |
| 220 | + <Bar dataKey="time" barSize={20} radius={[6, 6, 0, 0]} minPointSize={8}> |
| 221 | + {data.map((entry, index) => ( |
| 222 | + <Cell |
| 223 | + key={`cell-${index}`} |
| 224 | + fill={entry.time === 0 ? '#DFD8D2' : activeIndex === index ? '#F47A0A' : '#8F867E'} |
| 225 | + /> |
| 226 | + ))} |
| 227 | + </Bar> |
| 228 | + </BarChart> |
| 229 | + </ResponsiveContainer> |
| 230 | + </div> |
| 231 | + ); |
| 232 | +}; |
0 commit comments