修改首页echarts样式

This commit is contained in:
linxd
2025-07-29 09:28:19 +08:00
parent 9ebae85b77
commit 34746f5cf9

View File

@ -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<any>(null);
const noticeChartRef = useRef<any>(null);
const qaChartRef = useRef<any>(null);
const linkChartRef = useRef<any>(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 (
<div className="common-container">
<Row gutter={30}>
<Col span={12}>
<Card title="政策法规数量" className={styles.card}>
<div className={styles.flex}>
<Statistic title="发布" valueStyle={valueStyleBySuccess} value={regulationsCount.publishCount} />
<Statistic title="置顶" valueStyle={valueStyleByWarning} value={regulationsCount.topCount} />
<Statistic title="总数" valueStyle={valueStyleByError} value={regulationsCount.totalCount} />
</div>
<ReactECharts
ref={regChartRef}
option={getSingleChartOption(
'政策法规',
['发布', '置顶', '总数'],
[
regulationsCount.publishCount,
regulationsCount.topCount,
regulationsCount.totalCount,
]
)}
notMerge={true}
lazyUpdate={true}
style={{ height: 300, width: '100%', maxWidth: '100%' }}
/>
</Card>
</Col>
<Col span={12}>
<Card title="通知中心数量" className={styles.card}>
<div className={styles.flex}>
<Statistic title="发布" valueStyle={valueStyleBySuccess} value={noticeCount.publishCount} />
<Statistic title="置顶" valueStyle={valueStyleByWarning} value={noticeCount.topCount} />
<Statistic title="总数" valueStyle={valueStyleByError} value={noticeCount.totalCount} />
</div>
<ReactECharts
ref={noticeChartRef}
option={getSingleChartOption(
'通知中心',
['发布', '置顶', '总数'],
[
noticeCount.publishCount,
noticeCount.topCount,
noticeCount.totalCount,
]
)}
notMerge={true}
lazyUpdate={true}
style={{ height: 300, width: '100%', maxWidth: '100%' }}
/>
</Card>
</Col>
<Col span={12}>
<Card title="用户提问数量" className={styles.card}>
<div className={styles.flex}>
<Statistic title="未解决" valueStyle={valueStyleByWarning} value={customerQandaCount.noticeCount} />
<Statistic title="已解决" valueStyle={valueStyleBySuccess} value={customerQandaCount.resolvedCount} />
<Statistic title="总数" valueStyle={valueStyleByError} value={customerQandaCount.totalCount} />
</div>
<ReactECharts
ref={qaChartRef}
option={getSingleChartOption(
'用户提问',
['发布数', '已回答数', '总数'],
[
customerQandaCount.noticeCount,
customerQandaCount.resolvedCount,
customerQandaCount.totalCount,
]
)}
notMerge={true}
lazyUpdate={true}
style={{ height: 300, width: '100%', maxWidth: '100%' }}
/>
</Card>
</Col>
<Col span={12}>
<Card title="友情链接数量" className={styles.card}>
<div className={styles.flex}>
<Statistic title="发布" valueStyle={valueStyleBySuccess} value={linksCount.publishCount} />
<Statistic title="总数" valueStyle={valueStyleByError} value={linksCount.totalCount} />
</div>
<ReactECharts
ref={linkChartRef}
option={getSingleChartOption(
'友情链接',
['发布', '总数'],
[
linksCount.publishCount,
linksCount.totalCount,
]
)}
notMerge={true}
lazyUpdate={true}
style={{ height: 300, width: '100%', maxWidth: '100%' }}
/>
</Card>
</Col>
</Row>