Posted on 2014-09-19 10:42
TWaver 阅读(3174)
评论(2) 编辑 收藏
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()){ |
圆形布局核心代码如下:
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 ; |
16 | var N = 0 ; //total number of node |
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 ; |
16 | var N = 0 ; //total number of node |
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); |