-
Notifications
You must be signed in to change notification settings - Fork 1
/
gobble-grapher.hbs
171 lines (139 loc) · 3.65 KB
/
gobble-grapher.hbs
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
<!DOCTYPE html>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<!-- <script src="https://d3js.org/d3.v3.js" charset="utf-8"></script> -->
<style>
.node {
stroke: #fff;
stroke-width: 1.5px;
}
.link {
stroke: #999;
stroke-opacity: 1;
fill: none;
}
text {
fill: #000;
stroke-width: 0;
font: 10px sans-serif;
pointer-events: none;
}
/*svg {
width: 100vw;
height: 100vh;
}*/
body {
margin: 0;
padding: 0;
overflow: hidden;
}
</style>
<body>
<script>
var links = {{{ links }}};
var nodes = {{{ nodes }}};
console.log('Links: ', links);
console.log('Link 1: ', links[0]);
console.log('Nodes: ', nodes);
// var width = 960,
// height = 960;
var width = window.innerWidth,
height = window.innerHeight;
var force = d3.layout.force()
.charge(-300)
.gravity(0.01)
.linkStrength(0.3)
.linkDistance(50)
.size([width, height]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
// Clamp root (always node zero) to bottom
nodes[0].fixed = true;
nodes[0].x = width/2;
nodes[0].y = height - 30;
// Start source nodes (group 1) at top, equally spaced
var sourceNodeCount = 0;
for (var i in nodes) {
if (nodes[i].group === 1) {
sourceNodeCount++;
}
}
var j = 1;
var startX = 30;
for (var i in nodes) {
if (nodes[i].group === 1) {
nodes[i].fixed = true;
nodes[i].y = startX += 10;
nodes[i].x = j * (width / (sourceNodeCount+1) )
j++;
}
}
// var color = d3.scale.category20();
function color(group) {
if (group === 1) { return '#FF7F0E'; } // Source, orange
if (group === 2) { return '#28B41F'; } // Merger, some kind of green
if (group === 3) { return '#AEC7E8'; } // Transformer, sky blue
if (group === 4) { return '#CEB4D1'; } // Observer, purple
}
force
.nodes(nodes)
.links(links)
.start();
// build the arrow.
svg.append("svg:defs").selectAll("marker")
.data(["end"])
.enter().append("svg:marker")
.attr("id", String)
.attr("viewBox", "0 -5 10 10")
.attr("refX", 15)
.attr("refY", -1.5)
.attr("markerWidth", 10)
.attr("markerHeight", 10)
.attr("orient", "auto")
.append("svg:path")
.attr("d", "M0,-5L10,0L0,5");
var link = svg.selectAll(".link")
.data(links)
// .enter().append("line") // Line rendering
.enter().append("path") // Curved path rendering
.attr("class", "link")
.style("stroke-width", function(d) { return Math.sqrt(d.value); })
.attr("marker-end", "url(#end)"); // arrowhead
var node = svg.selectAll(".node")
.data(nodes)
.enter()
.append('g')
.attr("class", "node")
.call(force.drag);
node.append("circle")
.attr("class", "node")
.attr("r", 5)
.style("fill", function(d) { return color(d.group); });
node.append("text")
.attr("x", 12)
.attr("dy", ".35em")
.text(function(d) { return d.name; });
force.on("tick", function() {
// Line rendering
// link.attr("x1", function(d) { return d.source.x; })
// .attr("y1", function(d) { return d.source.y; })
// .attr("x2", function(d) { return d.target.x; })
// .attr("y2", function(d) { return d.target.y; });
// Curved path rendering
link.attr("d", function(d) {
var dx = d.target.x - d.source.x,
dy = d.target.y - d.source.y,
dr = Math.sqrt(dx * dx + dy * dy) * 3;
return "M" + d.source.x + "," + d.source.y + "A" + dr + "," + dr + " 0 0,1 " + d.target.x + "," + d.target.y;
});
// node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
node.attr("transform", function(d) {
var x = Math.max(0, Math.min(width, d.x));
var y = Math.max(0, Math.min(height, d.y));
return "translate(" + x + "," + y + ")";
});
});
</script>
</body>
</html>