-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBarChart.tsx
More file actions
129 lines (121 loc) · 3.37 KB
/
BarChart.tsx
File metadata and controls
129 lines (121 loc) · 3.37 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
import { useMemo } from 'react'
import ReactECharts from 'echarts-for-react'
import { useTranslation } from 'react-i18next'
import { PriorityStats, PRIORITY_LABELS } from '@/utils'
interface BarChartProps {
data: PriorityStats[]
selectedPriority: 'high' | 'medium' | 'low' | null
onPrioritySelect: (priority: 'high' | 'medium' | 'low' | null) => void
className?: string
}
const PRIORITY_COLORS = {
high: '#ef4444',
medium: '#f59e0b',
low: '#10b981'
}
export default function BarChart({
data,
selectedPriority,
onPrioritySelect,
className
}: BarChartProps) {
const { t, i18n } = useTranslation(['statistics'])
const option = useMemo(() => {
const isDark = document.documentElement.classList.contains('dark')
const priorityLabels = data.map((item) =>
t(`statistics:PRIORITY.${item.priority.toUpperCase()}`)
)
const counts = data.map((item) => item.count)
const colors = data.map((item) => PRIORITY_COLORS[item.priority])
return {
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'shadow'
},
formatter: (params: any) => {
const param = params[0]
return `
<div style="padding: 8px;">
<div style="font-weight: bold; margin-bottom: 4px;">${param.name}</div>
<div>${t('statistics:CHART.TASK_COUNT')}: ${param.value}</div>
<div style="margin-top: 4px; font-size: 12px; color: #666;">${t('statistics:CHART.CLICK_TO_FILTER')}</div>
</div>
`
}
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
top: '10%',
containLabel: true
},
xAxis: {
type: 'category',
data: priorityLabels,
axisLabel: {
color: isDark ? '#9ca3af' : '#6b7280'
},
axisLine: {
lineStyle: {
color: isDark ? '#4b5563' : '#e5e7eb'
}
}
},
yAxis: {
type: 'value',
minInterval: 1,
axisLabel: {
color: isDark ? '#9ca3af' : '#6b7280'
},
splitLine: {
lineStyle: {
color: isDark ? '#374151' : '#f3f4f6'
}
}
},
series: [
{
name: t('statistics:CHART.TASK_COUNT'),
type: 'bar',
barWidth: '50%',
data: data.map((item, index) => ({
value: item.count,
itemStyle: {
color: PRIORITY_COLORS[item.priority],
borderRadius: [8, 8, 0, 0],
opacity: selectedPriority === null || selectedPriority === item.priority ? 1 : 0.3
}
})),
emphasis: {
itemStyle: {
shadowBlur: 10,
shadowColor: 'rgba(0, 0, 0, 0.3)'
}
}
}
]
}
}, [data, selectedPriority, t, i18n.language])
const onEvents = {
click: (params: any) => {
if (params.componentType === 'series') {
const clickedPriority = data[params.dataIndex]?.priority
if (clickedPriority) {
onPrioritySelect(selectedPriority === clickedPriority ? null : clickedPriority)
}
}
}
}
return (
<div className={className}>
<ReactECharts
option={option}
style={{ height: '300px', width: '100%' }}
onEvents={onEvents}
opts={{ renderer: 'canvas' }}
/>
</div>
)
}