-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsquareLayout.html
130 lines (112 loc) · 3.86 KB
/
squareLayout.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
119
120
121
122
123
124
125
126
127
128
129
130
<!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) {
let n = 100;
let nodes = n * n;
let links = 4 * 2 + 4 * (n - 2) * 3 + (n - 2) * (n - 2) * 4;
let source = Module._malloc(4 * links);
let target = Module._malloc(4 * links);
let cnt = 0;
linkData = []
nodeData = []
for (let i = 0; i < n * n; ++i) {
if (Math.floor(i / n) > 0) {
Module.HEAP32[source / 4 + cnt] = i; Module.HEAP32[target / 4 + cnt] = i - n; cnt++;
linkData.push({ 'sourceId': i, 'targetId': i - n });
}
if (Math.floor(i / n) < n - 1) {
Module.HEAP32[source / 4 + cnt] = i; Module.HEAP32[target / 4 + cnt] = i + n; cnt++;
linkData.push({ 'sourceId': i, 'targetId': i + n });
}
if (i % n > 0) {
Module.HEAP32[source / 4 + cnt] = i; Module.HEAP32[target / 4 + cnt] = i - 1; cnt++;
linkData.push({ 'sourceId': i, 'targetId': i - 1 });
}
if (i % n < n - 1) {
Module.HEAP32[source / 4 + cnt] = i; Module.HEAP32[target / 4 + cnt] = i + 1; cnt++;
linkData.push({ 'sourceId': i, 'targetId': i + 1 });
}
}
console.log(nodes, links);
console.time("sort");
let result = Module._FM3(nodes, links, source, target);
console.timeEnd("sort");
for (let i = 0; i < nodes; ++i) {
nodeData.push({ 'id': i, 'x': Module.HEAPF32[(result >> 2) + i * 2], 'y': Module.HEAPF32[(result >> 2) + i * 2 + 1] });
}
for (let i = 0; i < links; ++i) {
linkData[i]['source'] = nodeData[linkData[i]['sourceId']];
linkData[i]['target'] = nodeData[linkData[i]['targetId']];
}
let graph = {'nodes': nodeData, 'links': linkData};
// console.log(graph)
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() {
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>