From 34746f5cf982a422f86176c50d1d73a00206b492 Mon Sep 17 00:00:00 2001 From: linxd <544554903@qq.com> Date: Tue, 29 Jul 2025 09:28:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=A6=96=E9=A1=B5echarts?= =?UTF-8?q?=E6=A0=B7=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/index/index.tsx | 205 ++++++++++++++++++++++++++------------ 1 file changed, 144 insertions(+), 61 deletions(-) diff --git a/src/pages/index/index.tsx b/src/pages/index/index.tsx index 5453cdd..2979a32 100644 --- a/src/pages/index/index.tsx +++ b/src/pages/index/index.tsx @@ -1,105 +1,188 @@ -import React, { useState, useEffect } from 'react'; +import React, { useState, useEffect, useRef } from 'react'; import styles from './index.less'; +import ReactECharts from 'echarts-for-react'; +import * as echarts from 'echarts'; // 用于获取 echarts 实例 import { getNoticeCount, getRegulationsCount, getCustomerQandaCount, getLinksCount, } from '@/servers/api'; -import { Row, Col, Card, Statistic, Space } from 'antd'; +import { Card, Row, Col } from 'antd'; + const IndexPage: React.FC = () => { - // 通知中心数量 - const [noticeCount, setNoticeCount] = useState({ - publishCount: 0, - topCount: 0, - totalCount: 0, - }); - // 政策法规数量 + const [noticeCount, setNoticeCount] = useState({ publishCount: 0, topCount: 0, totalCount: 0 }); const [regulationsCount, setRegulationsCount] = useState({ publishCount: 0, topCount: 0, totalCount: 0, }); - // 用户提问数量 const [customerQandaCount, setCustomerQandaCount] = useState({ noticeCount: 0, resolvedCount: 0, totalCount: 0, }); - // 友情链接数量 - const [linksCount, setLinksCount] = useState({ - publishCount: 0, - totalCount: 0, - }); + const [linksCount, setLinksCount] = useState({ publishCount: 0, totalCount: 0 }); + + // 创建 4 个图表的 ref + const regChartRef = useRef(null); + const noticeChartRef = useRef(null); + const qaChartRef = useRef(null); + const linkChartRef = useRef(null); + + const defaultColors = [ + '#5470C6', '#73C0DE', '#3BA272', + '#FC8452', '#9A60B4', '#EA7CCC' + ]; useEffect(() => { - // 政策法规数量 - getRegulationsCount().then((res) => { - setRegulationsCount(res.data); - }); - // 通知中心数量 - getNoticeCount().then((res) => { - setNoticeCount(res.data); - }); - // 用户提问数量 - getCustomerQandaCount().then((res) => { - setCustomerQandaCount(res.data); - }); - // 友情链接数量 - getLinksCount().then((res) => { - setLinksCount(res.data); + Promise.all([ + getRegulationsCount(), + getNoticeCount(), + getCustomerQandaCount(), + getLinksCount(), + ]).then(([reg, notice, qa, link]) => { + setRegulationsCount(reg.data); + setNoticeCount(notice.data); + setCustomerQandaCount(qa.data); + setLinksCount(link.data); + + // 强制 resize 所有图表 + setTimeout(() => { + [regChartRef, noticeChartRef, qaChartRef, linkChartRef].forEach((ref) => { + if (ref.current) { + ref.current.getEchartsInstance().resize(); + } + }); + }, 0); }); }, []); - const valueStyleBySuccess = { - color: '#3f8600', - fontSize: 40, - }; - const valueStyleByError = { - color: '#cf1322', - fontSize: 40, - }; - const valueStyleByWarning = { - color: '#faad14', - fontSize: 40, - }; + const getSingleChartOption = (title: string, labels: string[], values: number[]) => ({ + title: { + text: title, + left: 'center', + textStyle: { fontSize: 16, fontWeight: 'bold' }, + }, + tooltip: { + trigger: 'axis', + axisPointer: { type: 'shadow' }, + }, + grid: { + left: '5%', + right: '5%', + bottom: '10%', + top: 50, + containLabel: true, + }, + xAxis: { + type: 'category', + data: labels, + axisLine: { lineStyle: { color: '#ccc' } }, + }, + yAxis: { + type: 'value', + splitLine: { lineStyle: { type: 'dashed' } }, + }, + series: [ + { + type: 'bar', + barWidth: '30%', + data: values.map((val, idx) => ({ + value: val, + itemStyle: { + color: defaultColors[idx % defaultColors.length], + borderRadius: [6, 6, 0, 0], + }, + })), + label: { + show: true, + position: 'top', + }, + emphasis: { + itemStyle: { opacity: 0.9 }, + }, + }, + ], + animationDuration: 800, + }); return (
-
- - - -
+
-
- - - -
+
-
- - - -
+
-
- - -
+