1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
define(['jquery', 'd3', 'rz_core'],
function($, d3, rz_core) {
function layout__d3_force(graph) {
return d3.layout.force()
.distance(240)
.gravity(0.12)
.charge(-1800)
.linkDistance(function (link, i) {
var d_src = graph.degree(link.__src),
d_dst = graph.degree(link.__dst),
ret = (d_src + d_dst) * 10 + 10;
return ret;
});
}
function layout__cola(graph) {
return cola.d3adaptor()
//.linkDistance(120)
.avoidOverlaps(true);
/*
.linkDistance(function (link, i) {
var d_src = graph.degree(link.__src),
d_dst = graph.degree(link.__dst),
ret = (d_src + d_dst) * 10 + 10;
return ret;
})
*/
}
function layout__empty(graph) {
var donothing = function () { return ret; },
ret = {
resume: donothing,
stop: donothing,
nodes: donothing,
links: donothing,
alpha: donothing,
start: donothing,
size: donothing,
on: donothing,
};
return ret;
}
function layout__sync(layer) {
var layout = {
_nodes: undefined,
_links: undefined,
_tick: function () {
console.log('tick not set');
},
_end: function () {
console.log('end not set');
},
wh: [1, 1] // width + height
},
bound_layer = layer.bind(layout);
layout.resume = function () {
layout.start();
return layout;
};
layout.stop = function () {
return layout;
};
layout.start = function () {
bound_layer();
layout._tick();
layout._end();
return layout;
}
layout.alpha = layout.start;
layout.nodes = function (_nodes) {
layout._nodes = _nodes;
return layout;
}
layout.links = function (_links) {
layout._links = _links;
return layout;
}
layout.on = function (on_type, func) {
switch (on_type) {
case 'tick':
layout._tick = func;
break;
case 'end':
layout._end = func;
break;
};
return layout;
}
layout.size = function (_wh) {
layout.wh = _wh;
return layout;
}
return layout;
}
function layout__concentric() {
var cx = $(document.body).innerWidth() / 2,
cy = $(document.body).innerHeight() / 2;
return layout__sync(function () {
var bytype = _.sortBy(_.groupBy(this._nodes, 'type'), "length"),
types = _.keys(bytype),
i,
r,
nodes,
count,
j,
node,
angle,
ring_radius = Math.max(50, (Math.min.apply(null, this.wh) - 50) / (types.length + 1));
for (i = 0; i < types.length ; ++i) {
r = (i + 1) * ring_radius;
nodes = bytype[types[i]];
count = nodes.length;
for (j = 0 ; j < count; ++j) {
node = nodes[j];
angle = j * Math.PI * 2 / count;
node.x = node.px = cx + r * Math.cos(angle);
node.y = node.py = cy + r * Math.sin(angle);
}
}
});
};
function layout__grid() {
return layout__sync(function () {
var nodes = this._nodes,
count = nodes.length,
line = Math.floor(Math.sqrt(count)) + 1,
rows = Math.ceil(count / line),
wspace = this.wh[0] / (line + 2),
hspace = this.wh[1] / (rows + 1);
for (var i = 0 ; i < nodes.length ; ++i) {
var node = nodes[i];
node.x = node.px = ((i % line) + 1) * wspace;
node.y = node.py = (Math.floor(i / line) + 1) * hspace;
}
});
};
var layouts = [
{
name: 'Force',
create: layout__d3_force,
clazz: 'btn_layout_d3_force'
},
{
name: 'Ring',
create: layout__concentric,
clazz: 'btn_layout_concentric'
},
/*
{
name: 'Grid',
create: layout__grid,
clazz: 'btn_layout_grid'
},
*/
];
return {
layouts: layouts,
empty: layout__empty
};
});
|