import React, { useEffect, useState } from "react"; import { Line } from "react-chartjs-2"; import { Chart as ChartJS, CategoryScale, LinearScale, PointElement, LineElement, Title, Tooltip, Legend } from 'chart.js'; ChartJS.register( CategoryScale, LinearScale, PointElement, LineElement, Title, Tooltip, Legend ); const MAX_DATA_POINTS = 30; export default function Network() { const [networkData, setNetworkData] = useState({ uploadSpeed: 0, downloadSpeed: 0, totalSent: 0, totalReceived: 0 }); const [chartData, setChartData] = useState({ labels: [], datasets: [ { label: '上传速度 (KB/s)', data: [], borderColor: 'rgb(75, 192, 192)', tension: 0.1 }, { label: '下载速度 (KB/s)', data: [], borderColor: 'rgb(255, 99, 132)', tension: 0.1 } ] }); useEffect(() => { const fetchData = async () => { try { const response = await fetch('/r/api/network2'); const data = await response.json(); setNetworkData({ uploadSpeed: data.uploadSpeed, downloadSpeed: data.downloadSpeed, totalSent: data.totalSent, totalReceived: data.totalReceived }); setChartData(prevData => { const newLabels = [...prevData.labels, new Date().toLocaleTimeString()]; const newUploadData = [...prevData.datasets[0].data, data.uploadSpeed]; const newDownloadData = [...prevData.datasets[1].data, data.downloadSpeed]; // 保持最新的30个数据点 if (newLabels.length > MAX_DATA_POINTS) { newLabels.shift(); newUploadData.shift(); newDownloadData.shift(); } return { labels: newLabels, datasets: [ { ...prevData.datasets[0], data: newUploadData }, { ...prevData.datasets[1], data: newDownloadData } ] }; }); } catch (error) { console.error('获取网络数据失败:', error); } }; // 每秒更新一次数据 const interval = setInterval(fetchData, 1000); return () => clearInterval(interval); }, []); const chartOptions = { responsive: true, animation: { duration: 0 }, scales: { y: { beginAtZero: true } }, plugins: { legend: { position: 'top' } } }; return (
上传: {networkData.uploadSpeed} KB/s
下载: {networkData.downloadSpeed} KB/s
总发送: {networkData.totalSent} GB
总接收: {networkData.totalReceived} GB