jTopo入门 简单实现拓扑图

2 篇文章 0 订阅

最近项目中需要绘制拓扑图,于是研究了下绘制拓扑图的组件,jtopo是一款比较简单易上手的开发工具包,分享给大家。

jtopo特点

1、完全基于HTML5 Canvas开发,API平易近人、几乎简单到了极致。
2、不依赖任何其他库、执行仅需一个Canvas。
3、体积小,压缩后仅几十KB。
5、免费!

下载jtopo

大家可以去官网自行下载 http://www.jtopo.com/download/jtopo-0.4.8-min.js,目前版本是0.4.8,官网有API文档和案例。
在这里插入图片描述

jtopo官网地址:http://www.jtopo.com/

jQuery

目前我是vue项目中引入的jtopo,jtopo只支持在jQuery语言编写,需要项目中引入jQuery,不是vue项目的可以自行去官网下载jQuery.js,vue引入jQuery可参考https://blog.csdn.net/JQdazhuang/article/details/107152192

举例代码

html代码:

<template>
  <div
    style="position: relative; width: 100%; height: 100%; margin: auto"
    id="topo_content"
  >
    <canvas id="topology-canvas" width="1000" height="600"></canvas>
  </div>
</template>

js代码:

var canvas = document.getElementById("topology-canvas");
        var stage = new JTopo.Stage(canvas);//创建一个画布

        var scene = new JTopo.Scene(stage);//创建一个背景

        _this.scene = scene;
        var childs = topoData;
        for (let i in childs) {
          var a = childs[i];
          if (a.elementType == "node") {//如果是拓扑节点
            //节点
            var node = new JTopo.Node(a.text);
            node.setLocation(a.x, a.y);
            node.setImage("/static/img/" + a.nodeImage, true);
            node.fontColor = "0,0,0";
            node.nodeId = a.nodeId;
            node.nodeType = a.nodeType;
            node.nodeImage = a.nodeImage;
            node.scaleX = a.scaleX;
            node.scaleY = a.scaleY;
            node.devIp = a.devIp;
            node.textPosition = a.textPosition;
            if (node) {
              node.alarm = a.alarm;
              node.alarmColor = "255,0,0";
              node.alarmAlpha = 0.5;
            }
            scene.add(node);
          } else if (a.elementType == "link") {
            //如果是连线
            var link = null;
            if (link == null) {
              var nodes = stage.find("node");
              var nodeA, nodeZ;
              if (a.nodeSrc && a.nodeDst) {
                nodes.forEach(function (nodeElement) {
                  if (nodeElement.elementType == "node") {
                    if (nodeElement.nodeId == a.nodeSrc) {
                      nodeA = nodeElement; //起点
                    }
                    if (nodeElement.nodeId == a.nodeDst) {
                      nodeZ = nodeElement; //终点
                    }
                  }
                });
              }

              //绘制折线
              var c = null;
              if (nodeA && nodeZ) {
                // 折线和直线绘制
                if (a["lineType"] == "line") {
                  c = new JTopo.Link(nodeA, nodeZ);
                  c.lineType = "line";
                }
                if (a["lineType"] == "foldLine") {
                  c = new JTopo.FoldLink(nodeA, nodeZ);
                  c.bundleOffset = 40;
                  c.direction = a.direction;
                  c.lineType = "foldLine";
                }
                if (a["lineType"] == "flexLine") {
                  c = new JTopo.FlexionalLink(nodeA, nodeZ);
                  c.offsetGap = 40;
                  c.direction = a.direction;
                  c.lineType = "flexLine";
                }
                if (a["lineType"] == "curveLine") {
                  c = new JTopo.CurveLink(nodeA, nodeZ);
                  c.lineType = "curveLine";
                }
                c.alpha = 1;
                c.shadow = false;
                c.font = "12px Consolas";
                c.arrowsRadius = 0;
                c.lineWidth = 1.5;
                c.bundleGap = 0;
                c.eagleEyeVsibleDefault = false;

                c.id = a.id;
                c.strokeColor = a.strokeColor;
                c.lineClass = a.lineClass;
                c.nodeSrc = nodeA.nodeId;
                c.nodeDst = nodeZ.nodeId;
                c.localDevId = a.localDevId;
                c.localDevIp = a.localDevIp;
                c.localDevIfindex = a.localDevIfindex;
                c.localDevIfname = a.localDevIfname;
                c.remoteDevId = a.remoteDevId;
                c.remoteDevIp = a.remoteDevIp;
                c.remoteDevIfindex = a.remoteDevIfindex;
                c.remoteDevIfname = a.remoteDevIfname;
                scene.add(c);
              }
            } else {
              link.strokeColor = a.strokeColor;
            }
          }
        }

代码运行效果图如下:
在这里插入图片描述
不懂可问,留言就行

具体完整代码可看我的码云,自行下载运行即可 https://gitee.com/niexiaohua/JTopo

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
双机房流量拓扑图是一种展示双机房流量网络的图形化表示方法。在网络架构中,双机房表示有两个机房,通过一个专线或者公网连接在一起,以实现数据的备份和冗余。而流量拓扑图是指展示网络中数据流动和通信路径的图形化表示方式。 在jtopo中,可以使用其提供的库和工具来绘制双机房流量拓扑图。首先,我们需要确定每个机房的位置和名称,可以使用节点来表示每个机房,并添加相应的标签。然后,我们可以使用连线来表示两个机房之间的连接,并使用连线上的箭头来表示数据流向。通过设置连线的样式和属性,我们可以更好地展现网络连接和数据传输的情况。 除了机房节点和连线之外,我们还可以添加其他图形元素来表示设备、服务器等。这些元素可以用来展示每个机房内部的网络设备、服务器和主机等信息,并通过连线与机房节点连接起来,以展示网络拓扑的完整性和连通性。 最后,我们可以使用jtopo提供的功能和特性来增强双机房流量拓扑图的可视化效果。例如,可以设置节点和连线的样式、颜色和大小,以及添加动画效果和交互功能,提升用户体验。 总之,通过使用jtopo来制作双机房流量拓扑图,可以更加清晰地展示双机房网络架构和数据流动情况,提供给用户更直观的理解和分析资源布局以及流量分布的方式。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值