index.tsx
8.28 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import { FC, useState, useRef, useEffect, useMemo } from 'react'
import ReactECharts from 'echarts-for-react'
import cn from '@/utils/classnames'
import s from './chart.module.scss'
type ChartProps = {
content: any | null
}
const formatValue = (value: number) => {
if (value >= 100000000) {
return `${(Math.floor(value / 100000000 * 100) / 100).toFixed(2)}亿`
}
if (value >= 1000) {
return `${(Math.floor(value / 10000 * 100) / 100).toFixed(2)}万`
}
return `${value}`
}
const Chart: FC<ChartProps> = ({ content }) => {
const chartData = content
if (!chartData || !chartData.chart?.[0]) {
return null
}
if (chartData.is_chart == 'N') return null
let [data_1, data_2] = chartData.chart
let { type, metric } = data_1
let option: any = {}
let title = ''
let legendData = [metric]
if (data_2) legendData = [metric, data_2.metric]
const chartRef = useRef<any>(null)
const [visibleItems, setVisibleItems] = useState<Set<string>>(new Set())
// 存储原始数据
const originalDataRef = useRef<any[]>([])
if (type == 'bar' || type == 'line') {
option = {
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'shadow'
},
formatter: (params: any) => {
let result = params[0].axisValue + '<br/>'
params.forEach((item: any) => {
result += `${item.marker} ${item.seriesName}: ${formatValue(item.value)}<br/>`
})
return result
}
},
grid: {
left: '20%',
right: '5%',
top: '10%',
bottom: '30%',
},
legend: {
data: legendData,
bottom: '0%',
itemWidth: 12,
itemHeight: 8,
textStyle: {
fontSize: 12,
color: '#133368',
},
},
xAxis: {
type: 'category',
boundaryGap: type == 'bar',
data: data_1.name,
splitLine: {
show: true,
lineStyle: {
color: '#E5E7EB',
type: 'dashed',
},
},
},
yAxis: {
type: 'value',
axisLabel: {
formatter: (value: number) => {
if (value >= 10000) {
return `${(value / 10000).toFixed(2)}w`
}
return value.toString()
},
},
splitLine: {
show: true,
lineStyle: {
color: '#E5E7EB',
type: 'dashed',
},
},
},
series: [
{
name: metric,
data: data_1.data || [],
type,
barMaxWidth: 20,
lineStyle: {
color: '#2C66F7',
width: 2,
},
itemStyle: {
color: '#2C66F7',
borderWidth: 2,
borderColor: '#fff',
},
areaStyle: {
color: {
type: 'linear',
x: 0,
y: 0,
x2: 0,
y2: 1,
colorStops: [
{ offset: 0, color: 'rgba(44, 102, 247, 0.3)' },
{ offset: 1, color: 'rgba(44, 102, 247, 0)' },
],
},
},
},
],
}
if (data_2)
option.series = [
...option.series,
{
name: data_2.metric,
data: data_2.data || [],
type,
barMaxWidth: 20,
lineStyle: {
color: '#FF8D1A',
width: 2,
},
itemStyle: {
color: '#FF8D1A',
borderWidth: 2,
borderColor: '#fff',
},
areaStyle: {
color: {
type: 'linear',
x: 0,
y: 0,
x2: 0,
y2: 1,
colorStops: [
{ offset: 0, color: 'rgba(255, 141, 26, 0.2)' },
{ offset: 1, color: 'rgba(255, 141, 26, 0)' },
],
},
},
},
] as any
title = type == 'bar' ? '柱状图' : '折线图'
}
const pieColor = ['#2A82E4', '#00BAAD', '#43CF7C', '#A5D63F', '#FF8D1A', '#FFC300', '#7948EA', '#C6C6C6']
if (type == 'pie') {
option = {
color: pieColor,
// legend: {
// left: '52%',
// top: 'center',
// orient: 'vertical',
// itemWidth: 8,
// itemHeight: 8,
// textStyle: {
// fontSize: 12,
// color: '#133368',
// overflow: 'truncate',
// width: 130,
// },
// itemGap: 8,
// },
series: [
{
type: 'pie',
radius: ['70%', '100%'],
center: ['50%', '50%'],
padAngle: 3,
label: {
show: false,
position: 'center',
},
labelLine: {
show: false,
},
emphasis: {
disabled: true,
},
data: (() => {
const dataArray = data_1.data || []
const nameArray = data_1.name || []
if (dataArray.length <= 7) {
return dataArray.map((item: any, index: number) => ({ value: item, name: nameArray[index] }))
}
const top7 = dataArray.slice(0, 7).map((item: any, index: number) => ({
value: item,
name: nameArray[index],
}))
const otherSum = dataArray.slice(7).reduce((sum: number, val: number) => sum + val, 0 as number)
return [...top7, { value: otherSum, name: '其他' }]
})(),
},
],
}
title = '饼状图'
// 保存原始数据
originalDataRef.current = option.series[0].data
}
if (!title) return null
// 初始化可见项状态
useEffect(() => {
if (type === 'pie' && visibleItems.size === 0) {
const allNames = new Set<string>(option.series[0].data.map((item: any) => item.name))
setVisibleItems(allNames)
}
}, [type])
// 计算当前显示的图表数据
const currentOption = useMemo(() => {
if (type !== 'pie') return option
const updatedData = option.series[0].data.map((item: any) => {
const original = originalDataRef.current.find((d: any) => d.name === item.name)
return {
...item,
value: visibleItems.has(item.name) ? original.value : 0
}
})
return {
...option,
series: [{ ...option.series[0], data: updatedData }]
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [visibleItems, type, option.series[0]?.data?.length])
// 切换图例项显示/隐藏
const handleToggleLegend = (name: string) => {
setVisibleItems(prev => {
const next = new Set(prev)
if (next.has(name)) {
next.delete(name)
} else {
next.add(name)
}
return next
})
}
return (
<div className="chart-container">
<div className="chart-title">{title}</div>
<div className={cn('chart-box flex', type == 'pie' && s['chart-box-pie'])}>
<div className={cn(s['chart-canvas'])}>
<ReactECharts ref={chartRef} option={currentOption} style={{ height: 240 }} />
</div>
{type == 'pie' && option.series[0]?.data && (
<div className={cn(s['chart-legend'])}>
{option.series[0].data.map((item: any, index: number) => (
<div
key={item.name}
className={cn(s['chart-legend-item'], !visibleItems.has(item.name) && s['chart-legend-item-hidden'])}
onClick={() => handleToggleLegend(item.name)}
>
<div
className={cn(s['chart-legend-color'])}
style={{ backgroundColor: pieColor[index] }}></div>
<div className={cn(s['chart-legend-text-box'])}>
<span className={cn(s['chart-legend-text'])}>{item.name}</span>
</div>
<div className={cn(s['chart-legend-line'])}></div>
<div className={cn(s['chart-legend-value'])}>{formatValue(item.value)}</div>
</div>
))}
</div>
)}
</div>
</div>
)
}
export default Chart