javascript - 如何在反应中显示网络拓扑和状态?
问题描述
我想在本地网络中显示网络设备(即计算机、交换机等及其连接和比特率)。
对于这样的简单示例(实际上问题可能有更多设备和连接):
Computer A ----\ 3kb/sec Computer C
\ /
Switch --/ 4kb/sec
/
Computer B -----/ 1kb/sec
我有设备图标、链接状态和比特率值,但我不知道如何在反应项目中有效地显示和管理这些。在第一次尝试中,我<svg>
静态地在 HTML 标记中绘制每个设备和链接(即在特定坐标中绘制每个设备......)。
import React from 'react';
const renderInput = (line, idx) => {
let l = line.points.reduce(
(prev, next) =>
(prev.x && prev.y ? 'M' + prev.x + ',' + prev.y : prev) + ' L' + next.x + ',' + next.y
);
return [
<text
key={'input-caption'}
x={line.points[0].x + 3}
y={line.points[0].y - 3}
fill={line.caption.fill}
style={line.caption.style}
>
{line.caption.text}
</text>,
<defs key={`arrow-${idx}`}>
<marker id="markerArrow" markerWidth="13" markerHeight="13" refX="3" refY="6" orient="auto">
<path d="M2,4 L2,8 L5,6 L2,4" style={{ fill: '#2dc408' }} />
</marker>
</defs>,
<path
key={`line-${idx}`}
d={l}
style={{
stroke: '#2dc408',
strokeWidth: '2px',
fill: 'none',
// markerStart: "url(#markerCircle)",
markerEnd: 'url(#markerArrow)'
}}
/>
];
};
const renderInputs = inputs => {
const result = inputs.map((line, idx) => renderInput(line, idx));
return result;
};
const Asset = ({ asset, inputs }) => {
const renderedInputs = renderInputs(inputs);
return [
renderedInputs,
<image
key={'logo-img'}
href={asset.icon}
x={asset.logo.location.x}
y={asset.logo.location.y}
height={asset.logo.size.height}
width={asset.logo.size.width}
/>,
<text
key={'asset-name'}
x={asset.caption.location.x}
y={asset.caption.location.y}
fill={asset.caption.fill}
style={asset.caption.style}
>
{asset.caption.text}
</text>
];
};
export default Asset;
正如您在上面的代码中看到的那样,我在道具中获取设备位置和名称,并在asset
道具中获取其链接以将input
其绘制在屏幕上。
这种方法存在以下问题。
- 结果没有响应
- 它不灵活(即,用户无法缩放/取消缩放或扩展/取消扩展网络拓扑以获得所需的视图)
- 管理设备和链接是一项繁琐的工作
解决方案
推荐阅读
- swift - 为什么cornerRadius.width/2 不给我一个完美的圆?
- json - 如何在 JSON 模式中扩展模式?
- php - 为 PHP 用户类创建 mySQLi 准备好的语句
- javascript - 如何使用 Node 和 Mongoose 实现多租户架构?
- c# - 有没有办法使用 Microsoft-Graph C# 客户端库删除消息?
- javascript - 如何在 ASP.NET MVC 中使用 Razor 视图从对象列表中获取控制器中的单个对象
- java - 为什么 Java 有嵌套的 try 语句?
- python - 如何使用预训练的张量流模型指定 Keras 中的可训练变量?
- python - 如何在构建后设置 QCompleter 模型
- sql - 如何在不使用 TOP、MAX 和 Order by 等系统函数的情况下从表中获取第 n 个最高记录?