"use client"; import { useEffect, useRef, useState } from "react"; import { useRouter } from "next/navigation"; import { PageContainer, ProCard } from "@ant-design/pro-components"; import { fetchApi } from "@/app/_modules/func"; import { faServer, faChartPie, faGaugeHigh, } from "@fortawesome/free-solid-svg-icons"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { Statistic, Col, Row, Flex } from "antd"; import { Chart as ChartJS, ArcElement, Tooltip, Legend } from "chart.js"; import { Doughnut } from "react-chartjs-2"; import CountUp from "react-countup"; import ReactSpeedometer from "react-d3-speedometer"; ChartJS.register(ArcElement, Tooltip, Legend); //查询API const queryAPI = "/api/monitor/cache"; //图表相关定义 type ChartDataSet = { label: string; data: string[]; backgroundColor: string[]; }; type ChartDataType = { labels: string[]; datasets: Array; }; export default function Cache() { const { push } = useRouter(); const [data, setData] = useState(undefined as any); const [commandChartData, setCommandChartData] = useState( {} as ChartDataType ); //查询数据 const queryData = async () => { const body = await fetchApi(queryAPI, push); if (body !== undefined) { if (body.code == 200) { setData(body.data); parseCommandChart(body.data.commandStats); } } }; //处理命令图表数据 const parseCommandChart = (commandStats: any[]) => { const labels: Array = new Array(); const datas: Array = new Array(); commandStats.forEach((item) => { labels.push(item.name); datas.push(item.value); }); const commandSet: ChartDataSet = { label: "数量", data: datas, backgroundColor: [ "#66BB6A", "#FFA726", "#42A5F5", "#EC407A", "#78909C", "#FF7043", "#26A69A", "#9575CD", "#FFB74D", ], }; const commandData: ChartDataType = { labels: labels, datasets: [commandSet], }; setCommandChartData(commandData); }; const options = { plugins: { tooltip: { callbacks: { label: function (context: any) { let label = context.dataset.label || ""; if (label) { label += ": "; } label += context.parsed + "(" + Math.round( (context.parsed * 100) / context.dataset.data.reduce( (a: string, b: string) => parseInt(a) + parseInt(b), 0 ) ) + "%)"; return label; }, }, }, }, }; const speedParentRef = useRef(null); useEffect(() => { queryData(); window.addEventListener("resize", handleResize); setDaynamicWidth(); return () => { window.removeEventListener("resize", handleResize); }; }, [speedParentRef]); //数字展示的动态效果 const formatter = (value: any) => ( ); //超小屏的边界值 const xsScreenWidth = 768; //界面两个ProCard横向布局空白空间宽度 const spaceTotalWidth = 212; //侧边栏展开占据的宽度 const siderExpand = 214; //计算初始的速度表宽度 const setDaynamicWidth = () => { const screenWidth = window.innerWidth; if (screenWidth < xsScreenWidth) { setParentWidth((screenWidth - spaceTotalWidth) / 2); } else if (screenWidth >= xsScreenWidth) { setParentWidth((screenWidth - spaceTotalWidth - siderExpand) / 2); } }; //速度表绘制的宽度值 const [speedWidth, setParentWidth] = useState(0); //处理屏幕变化时,速度表宽度动态变化 const handleResize = () => { if (speedParentRef.current) { setParentWidth(speedParentRef.current.clientWidth); } else { console.log("no current"); } }; return ( {data !== undefined && ( <> 基本信息 } direction="row" headerBordered bordered hoverable > 命令统计 } style={{ height: "100%" }} headerBordered bordered hoverable > 内存信息 } style={{ height: "100%" }} headerBordered bordered hoverable > )} ); }