|
| 1 | +import { useMemo } from 'react' |
| 2 | +import ReactECharts from 'echarts-for-react' |
| 3 | +import { useTranslation } from 'react-i18next' |
| 4 | +import dayjs from 'dayjs' |
| 5 | +import { DailyCompletedStats } from '@/utils' |
| 6 | + |
| 7 | +interface LineChartProps { |
| 8 | + data: DailyCompletedStats[] |
| 9 | + selectedDate: string | null |
| 10 | + onDateSelect: (date: string | null) => void |
| 11 | + highlightTag?: string | null |
| 12 | + className?: string |
| 13 | +} |
| 14 | + |
| 15 | +export default function LineChart({ |
| 16 | + data, |
| 17 | + selectedDate, |
| 18 | + onDateSelect, |
| 19 | + highlightTag, |
| 20 | + className |
| 21 | +}: LineChartProps) { |
| 22 | + const { t, i18n } = useTranslation(['statistics']) |
| 23 | + |
| 24 | + const option = useMemo(() => { |
| 25 | + const dates = data.map((d) => d.date) |
| 26 | + const counts = data.map((d) => d.count) |
| 27 | + |
| 28 | + const isDark = document.documentElement.classList.contains('dark') |
| 29 | + |
| 30 | + return { |
| 31 | + tooltip: { |
| 32 | + trigger: 'axis', |
| 33 | + formatter: (params: any) => { |
| 34 | + const param = params[0] |
| 35 | + const dateData = data.find((d) => d.date === param.axisValue) |
| 36 | + return ` |
| 37 | + <div style="padding: 8px;"> |
| 38 | + <div style="font-weight: bold; margin-bottom: 4px;">${param.axisValue}</div> |
| 39 | + <div>${t('statistics:CHART.COMPLETED_TASKS')}: ${param.value}</div> |
| 40 | + ${dateData?.tasks.length ? `<div style="margin-top: 4px; font-size: 12px; color: #666;">${t('statistics:CHART.CLICK_TO_VIEW')}</div>` : ''} |
| 41 | + </div> |
| 42 | + ` |
| 43 | + } |
| 44 | + }, |
| 45 | + grid: { |
| 46 | + left: '3%', |
| 47 | + right: '4%', |
| 48 | + bottom: '3%', |
| 49 | + top: '10%', |
| 50 | + containLabel: true |
| 51 | + }, |
| 52 | + xAxis: { |
| 53 | + type: 'category', |
| 54 | + boundaryGap: false, |
| 55 | + data: dates, |
| 56 | + axisLabel: { |
| 57 | + formatter: (value: string) => dayjs(value).format('MM/DD'), |
| 58 | + color: isDark ? '#9ca3af' : '#6b7280' |
| 59 | + }, |
| 60 | + axisLine: { |
| 61 | + lineStyle: { |
| 62 | + color: isDark ? '#4b5563' : '#e5e7eb' |
| 63 | + } |
| 64 | + } |
| 65 | + }, |
| 66 | + yAxis: { |
| 67 | + type: 'value', |
| 68 | + minInterval: 1, |
| 69 | + axisLabel: { |
| 70 | + color: isDark ? '#9ca3af' : '#6b7280' |
| 71 | + }, |
| 72 | + splitLine: { |
| 73 | + lineStyle: { |
| 74 | + color: isDark ? '#374151' : '#f3f4f6' |
| 75 | + } |
| 76 | + } |
| 77 | + }, |
| 78 | + series: [ |
| 79 | + { |
| 80 | + name: t('statistics:CHART.COMPLETED_TASKS'), |
| 81 | + type: 'line', |
| 82 | + smooth: true, |
| 83 | + data: counts, |
| 84 | + lineStyle: { |
| 85 | + width: 3, |
| 86 | + color: highlightTag ? '#f59e0b' : '#10b981' |
| 87 | + }, |
| 88 | + areaStyle: { |
| 89 | + color: { |
| 90 | + type: 'linear', |
| 91 | + x: 0, |
| 92 | + y: 0, |
| 93 | + x2: 0, |
| 94 | + y2: 1, |
| 95 | + colorStops: [ |
| 96 | + { |
| 97 | + offset: 0, |
| 98 | + color: highlightTag ? 'rgba(245, 158, 11, 0.3)' : 'rgba(16, 185, 129, 0.3)' |
| 99 | + }, |
| 100 | + { |
| 101 | + offset: 1, |
| 102 | + color: highlightTag ? 'rgba(245, 158, 11, 0.05)' : 'rgba(16, 185, 129, 0.05)' |
| 103 | + } |
| 104 | + ] |
| 105 | + } |
| 106 | + }, |
| 107 | + itemStyle: { |
| 108 | + color: highlightTag ? '#f59e0b' : '#10b981' |
| 109 | + }, |
| 110 | + emphasis: { |
| 111 | + itemStyle: { |
| 112 | + borderWidth: 3, |
| 113 | + borderColor: highlightTag ? '#f59e0b' : '#10b981' |
| 114 | + } |
| 115 | + }, |
| 116 | + markPoint: |
| 117 | + selectedDate && dates.includes(selectedDate) |
| 118 | + ? { |
| 119 | + data: [ |
| 120 | + { |
| 121 | + coord: [selectedDate, counts[dates.indexOf(selectedDate)]], |
| 122 | + itemStyle: { color: '#ef4444' } |
| 123 | + } |
| 124 | + ], |
| 125 | + symbol: 'circle', |
| 126 | + symbolSize: 12 |
| 127 | + } |
| 128 | + : undefined |
| 129 | + } |
| 130 | + ] |
| 131 | + } |
| 132 | + }, [data, selectedDate, highlightTag, t, i18n.language]) |
| 133 | + |
| 134 | + const onEvents = { |
| 135 | + click: (params: any) => { |
| 136 | + if (params.componentType === 'series') { |
| 137 | + const clickedDate = data[params.dataIndex]?.date |
| 138 | + if (clickedDate) { |
| 139 | + onDateSelect(selectedDate === clickedDate ? null : clickedDate) |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + return ( |
| 146 | + <div className={className}> |
| 147 | + <ReactECharts |
| 148 | + option={option} |
| 149 | + style={{ height: '300px', width: '100%' }} |
| 150 | + onEvents={onEvents} |
| 151 | + opts={{ renderer: 'canvas' }} |
| 152 | + /> |
| 153 | + </div> |
| 154 | + ) |
| 155 | +} |
0 commit comments