-
Notifications
You must be signed in to change notification settings - Fork 4
/
facebookLayout.html
118 lines (101 loc) · 3.47 KB
/
facebookLayout.html
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
<!doctype html>
<html>
<body>
<svg id="graph"> </svg>
</body>
<script src='https://d3js.org/d3.v5.min.js'></script>
<script src="ogdf.js"></script>
<script>
initOGDF().then(function (Module) {
d3.json("facebook.json").then(function(graph) {
dic = {}
for(let i = 0; i < graph.nodes.length; ++i) {
if(graph.nodes[i]['id'] in dic) {
console.log('there is a bug');
} else dic[graph.nodes[i]['id']] = i;
}
let nodes = graph.nodes.length
let links = graph.links.length
let source = Module._malloc(4 * links);
let target = Module._malloc(4 * links);
for (let i = 0; i < links; ++i) {
Module.HEAP32[source / 4 + i] = dic[graph.links[i].source]; Module.HEAP32[target / 4 + i] = dic[graph.links[i].target];
}
console.log(nodes, links)
console.time("sort");
let result = Module._FM3(nodes, links, source, target);
console.timeEnd("sort");
console.log('complete layout')
for (let i = 0; i < nodes; ++i) {
graph.nodes[i]['x'] = Module.HEAPF32[(result >> 2) + i * 2]
graph.nodes[i]['y'] = Module.HEAPF32[(result >> 2) + i * 2 + 1];
}
for (let i = 0; i < links; ++i) {
graph.links[i]['source'] = graph.nodes[dic[graph.links[i]['source']]];
graph.links[i]['target'] = graph.nodes[dic[graph.links[i]['target']]];
}
const svg = d3.select("#graph");
const width = 1000
svg.attr("width", width).attr("height", width)
let container = svg.append("g");
const link = container
.append("g")
.attr("class", "links")
.selectAll("line")
.data(graph.links)
.enter()
.append("line")
.attr("stroke", "#2a2400")
.attr("stroke-width", 1)
const node = container
.append("g")
.attr("class", "nodes")
.selectAll("circle")
.data(graph.nodes)
.enter()
.append("circle")
.attr("r", 3)
.attr("fill", 'red')
function ticked() {
console.log('start tick');
const padding = 20
let max = {}
let min = {}
max.x = d3.max(graph.nodes, n => n.x)
max.y = d3.max(graph.nodes, n => n.y)
min.x = d3.min(graph.nodes, n => n.x)
min.y = d3.min(graph.nodes, n => n.y)
const xScale = d3
.scaleLinear()
.domain([min.x, max.x])
.range([padding, width - padding])
const yScale = d3
.scaleLinear()
.domain([min.y, max.y])
.range([padding, width - padding])
link.attr("x1", function (d) {
return xScale(d.source.x)
})
.attr("y1", function (d) {
return yScale(d.source.y)
})
.attr("x2", function (d) {
return xScale(d.target.x)
})
.attr("y2", function (d) {
return yScale(d.target.y)
})
node.attr("cx", function (d) {
return xScale(d.x)
}).attr("cy", function (d) {
return yScale(d.y)
})
}
ticked();
Module._free(source);
Module._free(target);
Module._free_buf(result);
});
});
</script>
</html>