chart.tsx
3.5 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
import { Card } from "antd";
import * as echarts from "echarts/core";
import {
GridComponent,
TooltipComponent,
LegendComponent,
TitleComponent,
} from "echarts/components";
import {
BarChart,
PieChart,
LineChart,
BarSeriesOption,
PieSeriesOption,
LineSeriesOption,
} from "echarts/charts";
import { CanvasRenderer } from "echarts/renderers";
import { useRef, useEffect, useState } from "react";
import { ChartComponentProps, ChartData } from "./getChartData";
// 注册必要的组件
echarts.use([
GridComponent,
TooltipComponent,
LegendComponent,
TitleComponent,
BarChart,
PieChart,
LineChart,
CanvasRenderer,
]);
type EChartsOption = echarts.ComposeOption<
BarSeriesOption | PieSeriesOption | LineSeriesOption
>;
const tabList = [
{ key: "bar", label: "柱状图" },
{ key: "pie", label: "饼图" },
{ key: "line", label: "折线图" },
];
// 图表配置生成器
const getOption = (type: string, data: ChartData): EChartsOption => {
const commonOption = {
title: { text: `${tabList.find((t) => t.key === type)?.label}` },
tooltip: { trigger: "item" },
};
// 提取数值数组
const values = data.values.map((item) => item.value);
switch (type) {
case "bar":
return {
...commonOption,
xAxis: { type: "category", data: data.categories },
yAxis: { type: "value" },
series: [{ data: values, type: "bar" }],
};
case "pie":
return {
...commonOption,
series: [
{
type: "pie",
data: data.categories.map((name, i) => ({
name,
value: values[i],
})),
radius: "50%",
},
],
};
case "line":
return {
...commonOption,
xAxis: { type: "category", data: data.categories },
yAxis: { type: "value" },
series: [
{
data: values,
type: "line",
smooth: true,
areaStyle: {},
},
],
};
default:
return {};
}
};
export function ChartComponent({
data = { categories: [], values: [] },
}: ChartComponentProps) {
const [activeTabKey, setActiveTabKey] = useState("bar");
const chartRef = useRef<HTMLDivElement>(null);
const chartInstance = useRef<echarts.ECharts | null>(null);
useEffect(() => {
if (!chartRef.current) return;
// 数据校验
if (
!data?.categories?.length ||
!data?.values?.length ||
data.values.some((item) => typeof item.value !== "number")
) {
console.warn("Invalid chart data");
return;
}
// 销毁旧实例
if (chartInstance.current) {
chartInstance.current.dispose();
}
// 初始化新图表
chartInstance.current = echarts.init(chartRef.current);
chartInstance.current.setOption(getOption(activeTabKey, data));
// 窗口resize监听
const resizeHandler = () => chartInstance.current?.resize();
window.addEventListener("resize", resizeHandler);
// 清理函数
return () => {
window.removeEventListener("resize", resizeHandler);
chartInstance.current?.dispose();
};
}, [activeTabKey, data]);
return (
<Card
style={{ width: "100%", minHeight: 400 }}
tabList={tabList}
activeTabKey={activeTabKey}
onTabChange={setActiveTabKey}
tabProps={{ size: "middle" }}
>
<div
ref={chartRef}
style={{
width: "100%",
height: 400,
minHeight: 400,
}}
/>
</Card>
);
}