import React, { useEffect, useRef, useState } from 'react'; import { Card, Row, Col, Space } from 'antd'; import { BellOutlined, ExclamationCircleOutlined, ClockCircleOutlined, CheckCircleOutlined } from '@ant-design/icons'; import * as echarts from 'echarts'; // 接口引入 import { getAlarmStatistics } from '../../api/alarmApi.js'; import { useTranslation } from 'react-i18next'; const AlarmStatistics = () => { const { t } = useTranslation(); const barChartRef = useRef(null); const pieChartRef = useRef(null); const [statData, setStatData] = useState({ total: 0, levelStats: {}, pending: 0, processing: 0, closed: 0, }); // 获取统计(修复:接口 code = 0 才是成功) const fetchStat = async () => { try { const res = await getAlarmStatistics(); if (res.code === 200) { setStatData(res.data || {}); } } catch (err) { } }; // 柱状图 useEffect(() => { if (!barChartRef.current) return; const chart = echarts.init(barChartRef.current); const option = { title: { text: t('alerts.sevenDayTrend'), textStyle: { fontSize: 14 } }, xAxis: { type: 'category', data: [t('devices.daySuffix', { day: 1 }), t('devices.daySuffix', { day: 2 }), t('devices.daySuffix', { day: 3 }), t('devices.daySuffix', { day: 4 }), t('devices.daySuffix', { day: 5 }), t('devices.daySuffix', { day: 6 }), t('devices.daySuffix', { day: 7 })] }, yAxis: { type: 'value' }, series: [{ data: [5, 8, 12, 9, 15, 7, statData.total || 2], type: 'bar', itemStyle: { color: '#165DFF', borderRadius: 4 } }] }; chart.setOption(option); const resize = () => chart.resize(); window.addEventListener('resize', resize); return () => { chart.dispose(); window.removeEventListener('resize', resize); }; }, [statData]); // 饼图 useEffect(() => { if (!pieChartRef.current) return; const chart = echarts.init(pieChartRef.current); const pieData = [ { value: statData.levelStats?.[1] || 0, name: t('alerts.criticalAlert') }, { value: statData.levelStats?.[2] || 0, name: t('alerts.importantAlert') }, { value: statData.levelStats?.[3] || 0, name: t('alerts.generalAlert') }, { value: statData.levelStats?.[4] || 0, name: t('alerts.infoAlert') }, ].filter(i => i.value > 0); const option = { title: { text: t('alerts.alertLevelDistribution') }, tooltip: { trigger: 'item' }, legend: { bottom: 0 }, series: [{ type: 'pie', radius: ['40%', '70%'], data: pieData.length ? pieData : [{ value: 1, name: t('common.noData') }], color: ['#F53F3F', '#FF7D00', '#F7BA1E', '#165DFF'], }] }; chart.setOption(option); const resize = () => chart.resize(); window.addEventListener('resize', resize); return () => { chart.dispose(); window.removeEventListener('resize', resize); }; }, [statData]); useEffect(() => { fetchStat(); }, []); return (