chart.tsx
4.7 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
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, ValueItem } 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 getValue = (item: number | ValueItem): number =>
typeof item === "number" ? item : item.value;
const getItemStyle = (item: number | ValueItem) =>
typeof item === "object" ? item.itemStyle : undefined;
switch (type) {
case "bar":
return {
...commonOption,
xAxis: { type: "category", data: data.categories },
yAxis: { type: "value" },
series: [
{
data: data.values,
type: "bar",
itemStyle: {
color: (params) => {
const dataItem = params.data as ValueItem | number;
if (typeof dataItem === "object" && dataItem.itemStyle?.color) {
return dataItem.itemStyle.color;
}
// 返回一个默认颜色,比如 ECharts 默认色系中的颜色
return "#5470c6";
},
},
},
],
};
case "pie":
return {
...commonOption,
series: [
{
type: "pie",
data: data.values.map((item, i) => ({
name: data.categories[i],
value: getValue(item),
itemStyle: getItemStyle(item),
})),
radius: "50%",
},
],
};
case "line":
return {
...commonOption,
xAxis: { type: "category", data: data.categories },
yAxis: { type: "value" },
series: [
{
data: data.values.map(getValue),
type: "line",
smooth: true,
areaStyle: {},
itemStyle: {
color: (params) => {
const dataItem = params.data as ValueItem | number;
if (typeof dataItem === "object" && dataItem.itemStyle?.color) {
return dataItem.itemStyle.color;
}
// 返回一个默认颜色,比如 ECharts 默认色系中的颜色
return "#5470c6";
},
},
},
],
};
default:
return commonOption;
}
};
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 !== "number" &&
(typeof (item as ValueItem).value !== "number" ||
((item as ValueItem).itemStyle &&
typeof (item as ValueItem).itemStyle?.color !== "string")),
)
) {
console.warn("Invalid chart data");
return;
}
if (chartInstance.current) {
chartInstance.current.dispose();
}
chartInstance.current = echarts.init(chartRef.current);
chartInstance.current.setOption(getOption(activeTabKey, data));
const resizeHandler = () => chartInstance.current?.resize();
window.addEventListener("resize", resizeHandler);
return () => {
window.removeEventListener("resize", resizeHandler);
chartInstance.current?.dispose();
};
}, [activeTabKey, data]);
return (
<Card
style={{ width: "auto", minHeight: 400 }}
tabList={tabList}
activeTabKey={activeTabKey}
onTabChange={setActiveTabKey}
tabProps={{ size: "middle" }}
>
<div
ref={chartRef}
style={{
width: "100%",
height: 400,
minHeight: 400,
alignItems: "center",
}}
/>
</Card>
);
}