-
Notifications
You must be signed in to change notification settings - Fork 0
/
smallsVis.html
101 lines (88 loc) · 2.92 KB
/
smallsVis.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
<!doctype html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.8/d3.min.js" type="text/JavaScript"></script>
<style>
.node {
fill: #4F442B;
}
.link {
stroke: #93C464;
}
</style>
</head>
<body>
<div id="viz">
<svg style="width:2000px;height:900px;" ></svg>
</div>
<script>
/* var roleScale = d3.scaleOrdinal()
.domain(["contractor", "employee", "manager"])
.range(["#75739F", "#41A368", "#FE9922"]);*/
var PromiseWrapper = d => new Promise(resolve => d3.csv(d, p => resolve(p)));
Promise
.all([
PromiseWrapper("../data/nodelist.csv"),
PromiseWrapper("../data/edgelist.csv")
])
.then(resolve => {
createForceLayout(resolve[0], resolve[1]);
});
function createForceLayout(nodes,edges) {
var nodeHash = {};
nodes.forEach(node => {
nodeHash[node.id] = node;
});
edges.forEach(edge => {
edge.weight = parseInt(edge.weight);
edge.source = nodeHash[edge.source];
edge.target = nodeHash[edge.target];
});
var linkForce = d3.forceLink();
var simulation = d3.forceSimulation()
.force("charge", d3.forceManyBody().strength(-200))
.force("center", d3.forceCenter().x(300).y(300))
.force("link", linkForce)
.nodes(nodes)
.on("tick", forceTick);
simulation.force("link").links(edges);
d3.select("svg").selectAll("line.link")
.data(edges, d => `${d.source.id}-${d.target.id}`)
.enter()
.append("line")
.attr("class", "link")
.style("opacity", .5)
.style("stroke-width", parseInt(d => d.weight));
nodes.forEach(d => {
d.degreeCentrality = edges
.filter(p => p.source === d || p.target === d)
.length;
});
var nodeEnter = d3.select("svg").selectAll("g.node")
.data(nodes, d => d.id)
.enter()
.append("g")
.attr("class", "node");
nodeEnter.append("circle")
.attr("r", d => d.degreeCentrality * 2)
//.attr("r", d => parseInt( d.mentionWeight))
.style("opacity", .5)
.style("fill", "blue");
//.style("fill", d => roleScale(d.role));
nodeEnter.append("text")
.style("text-anchor", "middle")
.attr("y", 15)//;
.text(d => d.id);
function forceTick() {
d3.selectAll("line.link")
.attr("x1", d => d.source.x)
.attr("x2", d => d.target.x)
.attr("y1", d => d.source.y)
.attr("y2", d => d.target.y);
d3.selectAll("g.node")
.attr("transform", d => `translate(${d.x},${d.y})`);
}
}
</script>
</body>
</html>