"use client"; import React, { useMemo, useRef } from "react"; import Draggable from "react-draggable"; import { Box, Chip, Stack, Typography } from "@mui/material"; import { ShowChart } from "@mui/icons-material"; import ReactECharts from "echarts-for-react"; import "dayjs/locale/zh-cn"; import { useHealthRisk } from "./HealthRiskContext"; export interface PredictDataPanelProps { /** 选中的要素信息列表,格式为 [[id, type], [id, type]] */ featureInfos: [string, string][]; /** Y 轴数值的小数位数 */ fractionDigits?: number; } const PredictDataPanel: React.FC = ({ featureInfos, fractionDigits = 4, }) => { const { predictionResults } = useHealthRisk(); const draggableRef = useRef(null); // 提取选中的设备 ID const selectedIds = useMemo( () => featureInfos.map(([id]) => id), [featureInfos] ); // 过滤出选中管道的预测结果 const filteredResults = useMemo(() => { return predictionResults.filter((res) => selectedIds.includes(res.link_id)); }, [predictionResults, selectedIds]); const hasData = filteredResults.length > 0; // 构建图表所需的数据集 const dataset = useMemo(() => { if (filteredResults.length === 0) return []; // 获取所有唯一的时间点并排序 const allX = Array.from( new Set(filteredResults.flatMap((res) => res.survival_function.x)) ).sort((a, b) => a - b); return allX.map((x) => { const row: any = { x, label: `${x}年` }; filteredResults.forEach((res) => { const index = res.survival_function.x.indexOf(x); if (index !== -1) { row[res.link_id] = res.survival_function.y[index]; } }); return row; }); }, [filteredResults]); const renderEmpty = () => ( 暂无预测数据 请在地图上选择已分析的管道 ); const renderChart = () => { if (!hasData) return renderEmpty(); const colors = [ "#1976d2", "#dc004e", "#ff9800", "#4caf50", "#9c27b0", "#00bcd4", "#f44336", "#8bc34a", "#ff5722", "#3f51b5", ]; const xData = dataset.map((item) => item.x); const series = filteredResults.map((res, index) => ({ name: `管道 ${res.link_id}`, type: "line", smooth: true, symbol: "circle", symbolSize: 6, itemStyle: { color: colors[index % colors.length], }, data: res.survival_function.y, })); const option = { tooltip: { trigger: "axis", formatter: (params: any) => { let res = `${params[0].name}年
`; params.forEach((item: any) => { res += `${item.marker} ${item.seriesName}: ${item.value.toFixed( fractionDigits )}
`; }); return res; }, }, legend: { top: "top", type: "scroll", }, grid: { left: "5%", right: "5%", bottom: "10%", containLabel: true, }, xAxis: { type: "category", name: "年", boundaryGap: false, data: xData, }, yAxis: { type: "value", name: "生存概率", min: 0, max: 1, }, series, }; return ( ); }; return ( {/* Header */} 健康预测曲线 {/* Content */} {renderChart()} ); }; export default PredictDataPanel;