import React, { useEffect, useRef, useState } from 'react'; import { Card, Result, Select, Button } from 'antd'; import { CheckCircleOutlined } from '@ant-design/icons'; import * as echarts from 'echarts'; import StandardTable from '@/components/StandardTable'; import styles from './RiskAssessment.less'; // import './RiskAssessment.less'; import img1 from '@/assets/safe_majorHazard/online_monitoring/img1.png'; import img2 from '@/assets/safe_majorHazard/online_monitoring/img2.png'; import img3 from '@/assets/safe_majorHazard/online_monitoring/img3.png'; import map1 from '@/assets/safe_majorHazard/online_monitoring/map.png'; import risk1 from '@/assets/safe_majorHazard/online_monitoring/risk1.png'; import risk2 from '@/assets/safe_majorHazard/online_monitoring/risk2.png'; import risk3 from '@/assets/safe_majorHazard/online_monitoring/risk3.png'; const RiskAssessment = () => { const chartRef = useRef(null); const [selectedRowKeys, setSelectedRowKeys] = useState([]); const [selectedRows, setSelectedRows] = useState([]); const [loading, setLoading] = useState(false); const [dataSource, setDataSource] = useState([]); const [pagination, setPagination] = useState({ current: 1, pageSize: 5, total: 0, }); useEffect(() => { if (chartRef.current) { const chart = echarts.init(chartRef.current); // 强制初始化时调整大小 setTimeout(() => { if (chart && !chart.isDisposed()) { chart.resize(); } }, 100); const option = { color: ['#FF2526', '#FF8800', '#FFC403', '#65E5F9'], legend: { data: ['重大风险', '较高风险', '一般风险', '低风险'], top: "-3px", left: "center", itemGap: 40, itemWidth: 20, itemHeight: 9, textStyle: { fontSize: 10 } }, grid: { left: '2%', right: '4%', bottom: '2%', top: '12%', containLabel: true }, xAxis: { type: 'category', boundaryGap: false, data: ['9/22', '9/23', '9/24', '9/25', '9/26', '9/27', '9/28'], axisLabel: { fontSize: 10 } }, yAxis: { type: 'value', min: 0, max: 30, axisLabel: { formatter: '{value}', fontSize: 10 } }, series: [ { name: '重大风险', type: 'line', smooth: true, lineStyle: { width: 1.5, color: '#FF2526' }, areaStyle: { color: { type: 'linear', x: 0, y: 0, x2: 0, y2: 1, colorStops: [ { offset: 0, color: 'rgba(38, 12, 12, 0.4)' }, { offset: 1, color: 'rgba(255, 37, 38, 0)' } ] } }, symbol: 'none', symbolSize: 6, itemStyle: { color: '#fff', borderColor: '#FF2526', borderWidth: 1 }, data: [8, 15, 12, 22, 18, 26, 20] }, { name: '较高风险', type: 'line', smooth: true, lineStyle: { width: 1.5, color: '#FF8800' }, areaStyle: { color: { type: 'linear', x: 0, y: 0, x2: 0, y2: 1, colorStops: [ { offset: 0, color: 'rgba(255, 136, 0, 0.4)' }, { offset: 1, color: 'rgba(255, 136, 0, 0)' } ] } }, symbol: 'none', symbolSize: 6, itemStyle: { color: '#fff', borderColor: '#FF8800', borderWidth: 1 }, data: [5, 8, 6, 12, 10, 15, 13] }, { name: '一般风险', type: 'line', smooth: true, lineStyle: { width: 1.5, color: '#FFC403' }, areaStyle: { color: { type: 'linear', x: 0, y: 0, x2: 0, y2: 1, colorStops: [ { offset: 0, color: 'rgba(255, 196, 3, 0.4)' }, { offset: 1, color: 'rgba(255, 196, 3, 0)' } ] } }, symbol: 'none', symbolSize: 6, itemStyle: { color: '#fff', borderColor: '#FFC403', borderWidth: 1 }, data: [12, 18, 15, 25, 22, 24, 26] }, { name: '低风险', type: 'line', smooth: true, lineStyle: { width: 1.5, color: '#65E5F9' }, areaStyle: { color: { type: 'linear', x: 0, y: 0, x2: 0, y2: 1, colorStops: [ { offset: 0, color: 'rgba(101, 229, 249, 0.4)' }, { offset: 1, color: 'rgba(101, 229, 249, 0)' } ] } }, symbol: 'none', symbolSize: 6, itemStyle: { color: '#fff', borderColor: '#65E5F9', borderWidth: 1 }, data: [3, 5, 7, 9, 6, 8, 4] } ] }; chart.setOption(option); // 响应式调整 - 使用多种方式监听容器尺寸变化 let resizeTimer = null; const handleResize = () => { // 防抖处理,避免频繁调用resize if (resizeTimer) { clearTimeout(resizeTimer); } resizeTimer = setTimeout(() => { if (chart && !chart.isDisposed()) { chart.resize(); } }, 50); // 减少延迟时间 }; // 监听窗口大小变化 window.addEventListener('resize', handleResize); // 监听容器尺寸变化(解决菜单栏伸缩时的自适应问题) let resizeObserver = null; if (window.ResizeObserver) { resizeObserver = new ResizeObserver((entries) => { for (let entry of entries) { // 使用requestAnimationFrame确保在下一帧执行 requestAnimationFrame(() => { handleResize(); }); } }); resizeObserver.observe(chartRef.current); } // 额外监听父容器的尺寸变化 const parentContainer = chartRef.current?.parentElement; let parentObserver = null; if (parentContainer && window.ResizeObserver) { parentObserver = new ResizeObserver((entries) => { for (let entry of entries) { requestAnimationFrame(() => { handleResize(); }); } }); parentObserver.observe(parentContainer); } // 使用MutationObserver监听DOM结构变化(菜单展开收起时) const mutationObserver = new MutationObserver((mutations) => { mutations.forEach((mutation) => { if (mutation.type === 'attributes' && (mutation.attributeName === 'class' || mutation.attributeName === 'style')) { // 延迟执行,确保DOM更新完成 setTimeout(() => { handleResize(); }, 200); } }); }); // 监听整个页面的class和style变化 mutationObserver.observe(document.body, { attributes: true, attributeFilter: ['class', 'style'], subtree: true }); return () => { window.removeEventListener('resize', handleResize); if (resizeObserver) { resizeObserver.disconnect(); } if (parentObserver) { parentObserver.disconnect(); } if (mutationObserver) { mutationObserver.disconnect(); } if (resizeTimer) { clearTimeout(resizeTimer); } if (chart && !chart.isDisposed()) { chart.dispose(); } }; } }, []); // 表格列定义 const columns = [ { title: '编号', dataIndex: 'id', key: 'id', width: 80, render: (text, record, index) => { const page = pagination.current || 1; const pageSize = pagination.pageSize || 5; const number = (page - 1) * pageSize + index + 1; return `0${number}`.slice(-2); } }, { title: '设备编号', dataIndex: 'deviceId', key: 'deviceId', width: 120, }, { title: '风险等级', dataIndex: 'riskLevel', key: 'riskLevel', width: 100, render: (text) => { const colorMap = { '高': '#FF4D4F', '中': '#FAAD14', '低': '#52C41A' }; return {text}; } }, { title: '预警时间', dataIndex: 'warningTime', key: 'warningTime', width: 150, }, { title: '主要原因', dataIndex: 'mainReason', key: 'mainReason', width: 200, }, { title: '处理状态', dataIndex: 'status', key: 'status', width: 100, render: (text) => { const statusMap = { '未处理': { color: '#FF4D4F', bg: '#FFF2F0' }, '处理中': { color: '#FAAD14', bg: '#FFFBE6' }, '已处理': { color: '#52C41A', bg: '#F6FFED' } }; const status = statusMap[text] || { color: '#333', bg: '#F5F5F5' }; return ( {text} ); } }, { title: '操作', key: 'action', width: 120, render: (_, record) => (