-
Notifications
You must be signed in to change notification settings - Fork 0
/
smallVis2.html
129 lines (113 loc) · 3.93 KB
/
smallVis2.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
<!doctype html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.8/d3.min.js" type="text/JavaScript"></script>
<style>
.link {
stroke: #93C464;
}
marker {
fill: #93C464;
}
</style>
</head>
<body>
<div id="controls">
</div>
<div id="viz">
<svg style="width:1700px;height:1500px;" ></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("../nodeList.csv"),
PromiseWrapper("../edgeList.csv")
])
.then(resolve => {
createForceLayout(resolve[0], resolve[1]);
});
function createForceLayout(nodes,edges) {
var marker = d3.select("svg").append('defs')
/*.append('marker')
.attr("id", "Triangle")
.attr("refX", 12)
.attr("refY", 6)
.attr("markerUnits", 'userSpaceOnUse')
.attr("markerWidth", 12)
.attr("markerHeight", 18)
.attr("orient", 'auto')*/
.append('path')
.attr("d", 'M 0 0 12 6 0 12 3 6');
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];
});
nodes.forEach(d => {
d.degreeCentrality = edges
.filter(p => p.source === d || p.target === d)
.length;
});
var linkForce = d3.forceLink().strength(d => d.weight * .1);
var simulation = d3.forceSimulation()
.force("charge", d3.forceManyBody().strength(-650))
.force("x", d3.forceX(950))
.force("y", d3.forceY(550))
.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", d => d.weight * 2);
d3.selectAll("line").attr("marker-end", "url(#Triangle)");
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)
.style("fill", "blue")
.style("opacity", .4);
//.style("fill", d => roleScale(d.role));
// Trying to only get text to appear over more important nodes to reduce clutter
//having a tough time refering to the individual node
nodeEnter.append("text")
.style("text-anchor", "middle")
.attr("y", 15)
.text(d => d.id)
.style("opacity", d=> d.degreeCentrality * .1);
console.log(d=> d.degreeCentrality * .1);
//function ( ){
/*if (d => d.degreeCentrality > 7){
return 1;
} else{
return 0;
}
});*/
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>