如何创建环型、树型双层布局
2024-07-23 08:36:39 222人阅读
TWaver的Demo中有常用的环型布局和树型布局,但是当网元数量较多,又不想zoomOverView,聪明的我们自然会想到使用双层布局,整体效果既不会显得很拥挤,也能刚好充满整个窗口,如下图的效果:
实现这样布局效果实现的步骤:
1.将link个数最多的Node作为圆点或顶点。
2.分别计算所有点的位置。
查找圆点或顶点的核心代码如下:
2 | this .box.forEach(function (element) { |
3 | if (element instanceof twaver.Node) { |
4 | sizes.push(element.getLinks().size()); |
8 | Array.max=function(array) |
10 | return Math.max.apply(Math,array); |
12 | this .box.forEach(function(element){ |
13 | if (element instanceof twaver.Node){ |
14 | if (Array.max(sizes) == element.getLinks().size()){ |
15 | <a href=http://www.mamicode.com/ ‘http://twaver.servasoft.com/wp-content/uploads/2014/09/AutoLayoutDemo.html‘ >AutoLayoutDemo</a>element.setClient( ‘center‘ , ‘center‘ ); |
圆形布局核心代码如下:
1 | function roundLayout() { |
2 | var datas = box.getDatas().toArray(); |
3 | var size = box.getDatas().toArray().length; |
5 | winWidth = window.innerWidth; |
6 | else if ((document.body) && (document.body.clientWidth)) |
7 | winWidth = document.body.clientWidth; |
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 ; |
18 | box.forEach(function (data) { |
19 | if (data.getClient( ‘center‘ ) !== undefined) { |
20 | data.setCenterLocation(centerX, centerY); |
22 | if (data instanceof twaver.Node && data.getClient( ‘center‘ ) == undefined) { |
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" )); |
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)); |
40 | data.setCenterLocation(x, y); |
41 | data.setClient( ‘level‘ , e); |
树型布局核心代码:
1 | function roundLayout() { |
2 | var datas = box.getDatas().toArray(); |
3 | var size = box.getDatas().toArray().length; |
5 | winWidth = window.innerWidth; |
6 | else if ((document.body) && (document.body.clientWidth)) |
7 | winWidth = document.body.clientWidth; |
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 ; |
18 | box.forEach(function (data) { |
19 | if (data.getClient( ‘center‘ ) !== undefined) { |
20 | data.setCenterLocation(centerX, centerY); |
22 | if (data instanceof twaver.Node && data.getClient( ‘center‘ ) == undefined) { |
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" )); |
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)); |
40 | data.setCenterLocation(x, y); |
41 | data.setClient( ‘level‘ , e); |
如何创建环型、树型双层布局
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉:
投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。