如何创建环型、树型双层布局

TWaver的Demo中有常用的环型布局和树型布局,但是当网元数量较多,又不想zoomOverView,聪明的我们自然会想到使用双层布局,整体效果既不会显得很拥挤,也能刚好充满整个窗口,如下图的效果:


实现这样布局效果实现的步骤:
1.将link个数最多的Node作为圆点或顶点。
2.分别计算所有点的位置。
查找圆点或顶点的核心代码如下:

1 var sizes = [];
2 this.box.forEach(function (element) {
3     if (element instanceof  twaver.Node) {
4         sizes.push(element.getLinks().size());
5     }
6 });
7  
8 Array.max=function(array)
9 {
10     return Math.max.apply(Math,array);
11 }
12 this.box.forEach(function(element){
13    if(element instanceof  twaver.Node){
14        if(Array.max(sizes) == element.getLinks().size()){
15            <a href='http://twaver.servasoft.com/wp-content/uploads/2014/09/AutoLayoutDemo.html'>AutoLayoutDemo</a>element.setClient('center','center');
16        }
17    }
18 });

圆形布局核心代码如下:

1 function roundLayout() {
2             var datas = box.getDatas().toArray();
3             var size = box.getDatas().toArray().length;
4             if (window.innerWidth)
5                 winWidth = window.innerWidth;
6             else if ((document.body) && (document.body.clientWidth))
7                 winWidth = document.body.clientWidth;
8 // 获取窗口高度
9             if (window.innerHeight)
10                 winHeight = window.innerHeight;
11             else if ((document.body) && (document.body.clientHeight))
12                 winHeight = document.body.clientHeight;
13             var centerX = winWidth / 2;//圆心坐标
14             var centerY = winHeight / 2;
15             var radius = 0;//半径
16             var N = 0//total number of node
17  
18             box.forEach(function (data) {
19                 if (data.getClient('center') !== undefined) {
20                     data.setCenterLocation(centerX, centerY);
21                 }
22                 if (data instanceof  twaver.Node && data.getClient('center') == undefined) {
23                     N++;
24                 }
25             });
26  
27             var c = getCRound(N);
28             var i = 0;
29             var n = parseInt(N / c);
30             radius = network.viewRect.height / 2 / c - 30 / c;
31             if (box.getClient("radius")) {
32                 radius = parseInt(box.getClient("radius"));
33             }
34             box.forEach(function (data) {
35                 if (data instanceof  twaver.Node && data.getClient('center') == undefined) {
36                     var e = parseInt(i / n);
37                     var x = centerX + (radius * Math.pow(1.5, e) * Math.cos(Math.PI * 2 / n * i + 0.2 * e));
38                     var y = centerY + (radius * Math.pow(1.5, e) * Math.sin(Math.PI * 2 / n * i + 0.2 * e));
39                     i++;
40                     data.setCenterLocation(x, y);
41                     data.setClient('level', e);
42                 }
43             });
44         }

树型布局核心代码:

1 function roundLayout() {
2             var datas = box.getDatas().toArray();
3             var size = box.getDatas().toArray().length;
4             if (window.innerWidth)
5                 winWidth = window.innerWidth;
6             else if ((document.body) && (document.body.clientWidth))
7                 winWidth = document.body.clientWidth;
8 // 获取窗口高度
9             if (window.innerHeight)
10                 winHeight = window.innerHeight;
11             else if ((document.body) && (document.body.clientHeight))
12                 winHeight = document.body.clientHeight;
13             var centerX = winWidth / 2;//圆心坐标
14             var centerY = winHeight / 2;
15             var radius = 0;//半径
16             var N = 0//total number of node
17  
18             box.forEach(function (data) {
19                 if (data.getClient('center') !== undefined) {
20                     data.setCenterLocation(centerX, centerY);
21                 }
22                 if (data instanceof  twaver.Node && data.getClient('center') == undefined) {
23                     N++;
24                 }
25             });
26  
27             var c = getCRound(N);
28             var i = 0;
29             var n = parseInt(N / c);
30             radius = network.viewRect.height / 2 / c - 30 / c;
31             if (box.getClient("radius")) {
32                 radius = parseInt(box.getClient("radius"));
33             }
34             box.forEach(function (data) {
35                 if (data instanceof  twaver.Node && data.getClient('center') == undefined) {
36                     var e = parseInt(i / n);
37                     var x = centerX + (radius * Math.pow(1.5, e) * Math.cos(Math.PI * 2 / n * i + 0.2 * e));
38                     var y = centerY + (radius * Math.pow(1.5, e) * Math.sin(Math.PI * 2 / n * i + 0.2 * e));
39                     i++;
40                     data.setCenterLocation(x, y);
41                     data.setClient('level', e);
42                 }
43             });
44         }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值