-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
290 lines (237 loc) · 7.76 KB
/
index.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.node {
cursor: pointer;
stroke-width: 1.5px;
}
.node circle {
stroke: #326B62;
/*stroke-width: 1.5px;*/
}
.node text {
font: bold 11px Calibre,Helvetica,Arial,sans-serif;
text-shadow: 2px 2px 3px #555555, 0px 0px 5px #FFFFFF;
color: #4D3636;
}
.link {
fill: none;
stroke-width: 3px;
stroke: #bbb;
}
body {
overflow:hidden;
}
</style>
<div id="graph-container"></div>
<link rel="stylesheet" type="text/css" href="style/ckstyles.css">
<!--<link href="http://cdn.sencha.io/ext-4.1.1-gpl/resources/css/ext-all.css" rel="stylesheet" />
<script src="http://cdn.sencha.io/ext-4.1.1-gpl/ext-all.js"></script>-->
<link href="https://cdnjs.cloudflare.com/ajax/libs/extjs/6.0.0/classic/theme-crisp/resources/theme-crisp-all.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/extjs/6.0.0/ext-all.js"></script>
<!-- TODO: gut jquery -->
<script src="bower_components/jquery/dist/jquery.min.js"></script>
<script src="scripts/ck-tools.js"></script>
<script src="scripts/jquery.blockUI.js"></script>
<script>
var DEFAULTKITTYID = "101" // BugCat
var viewerWidth, viewerHeight, center; // screen resolution and center
var forceDistance = (n) => 200 / n.gen ? n.gen : 1;
var forceStrength = 0.5;
var chargeForce = -1e4;
var zoomScale = 1; // current scale set via zooming
// size of the diagram
viewerWidth = $(document).width();
viewerHeight = $(document).height();
center = { x: viewerWidth/2, y: viewerHeight/2 };
// TODO: redesign
var kittyId;
function GetKittyId()
{
// check if kitty id already in url
var urlData = {};
var temp = window.location.href.split("?");
if (temp.length > 1)
urlData = Ext.urlDecode(temp[1]);
if ('kid' in urlData)
kittyId = urlData['kid'];
else
{
kittyId = prompt("Please enter the kitty id: ", DEFAULTKITTYID);
updateURL();
}
}
GetKittyId();
buildForceGraph(kittyId); // output -> kittyData
// TODO: should be a better way to keep track of these...
var allNodes = [],
kittyNodes = [],
links = [],
kittyIDs = new Set();
// define the zoomListener which calls the zoom function on the "zoom" event constrained within the scaleExtents
var zoomListener = d3.zoom().scaleExtent([0.1, 3]).on("zoom", zoom);
// Define the zoom function for the zoomable tree
function zoom()
{
var transform = d3.event.transform;
zoomScale = transform.k;
svgGroup.attr("transform", `translate(${[transform.x, transform.y]})scale(${zoomScale})`);
}
var svg = d3.select("#graph-container").append("svg")
.attr("width", viewerWidth)
.attr("height", viewerHeight)
.call(zoomListener);
var svgGroup = svg.append("g");
var color = d3.scaleOrdinal(d3.schemeCategory20);
var manyBodyForce = d3.forceManyBody().strength(chargeForce);
var simulation = d3.forceSimulation()
.force("link", d3.forceLink().distance(forceDistance).strength(forceStrength))
.force("charge", manyBodyForce)
.force("center", d3.forceCenter(viewerWidth / 2, viewerHeight / 2));
var gnode; // = svg.selectAll("g.node"),
var link = svgGroup.selectAll("path.link");
simulation
.nodes(allNodes)
.on("tick", ticked);
simulation.force("link")
.links(links);
function ticked() {
//link.attr("d", positionLink);
link
.attr("x1", (d) => d.source.x)
.attr("y1", (d) => d.source.y)
.attr("x2", (d) => d.target.x)
.attr("y2", (d) => d.target.y);
svgGroup.selectAll("g.node").data(kittyNodes, (d) => d.id || (d.id = ++i) )
.attr("transform", (d) => `translate(${d.x},${d.y})`);
}
function positionLink(d) {
return "M" + d[0].x + "," + d[0].y
+ "S" + d[1].x + "," + d[1].y
+ " " + d[2].x + "," + d[2].y;
}
function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(1).restart();
d.fx = d.x, d.fy = d.y;
}
function dragged(d) {
d.fx = d3.event.x, d.fy = d3.event.y;
}
function dragended(d) {
if (!d3.event.active) simulation.alphaTarget(0);
}
function unfixNode(d) {
d.fx = null, d.fy = null;
}
var updatePeriod = 3500;
var activityTimeout = 4500;
var nodeRadius = 29;
// TEMP: lower kitty limit
kittyLimit = 2500;
var dragHandler = d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended);
var linkPaths = [];
var updateLoop = setTimeout(updateGraph, 1500);
checkIfDone(); // follows the loop starting
// TODO: when should this be started?
// TODO: bake in updateLoop to make into a variable function
function checkIfDone() {
if (stillActive())
setTimeout(checkIfDone, activityTimeout);
else
{
console.log("DONE!");
clearTimeout(updateLoop);
}
}
function updateGraph() {
console.log(`Kitty count: ${kittyCnt}`);
update();
updateLoop = setTimeout(updateGraph, updatePeriod);
}
function update() {
// add new nodes
for (var key in kittyData.nodes) {
var value = kittyData.nodes[key];
if (!value.active) {
kittyNodes.push(value);
allNodes.push(value);
kittyIDs.add(Number(key));
value.active = true;
}
}
gnode = svgGroup.selectAll("g.node").data(kittyNodes, (d) => d.id || (d.id = ++i) );
//gnode = svgGroup.selectAll("g.node")
//node = node.data(kittyNodes, (d) => d.id);
gnode.exit().remove();
var nodeEnter = gnode.enter().append('g', ':first-child')
.attr('class', 'node')
.on('dblclick', unfixNode)
.call(dragHandler)
.merge(gnode);
nodeEnter.append("circle")
.attr('class', (d) => `nodeCircle u-bg-alt-${d.color}`)
//.attr("fill", (d) => color(d.id) )
.attr("r", nodeRadius);
nodeEnter.append("text")
.attr('class', 'nodeText')
.attr("x", 0)
.attr("y", -35 )
.attr("dy", "0em")
.attr("text-anchor", "middle")
.style("fill-opacity", 1)
.text( (d) => d.name );
nodeEnter.append("image")
.attr('title', (d) => d.bio)
.attr('kid', (d) => d.id)
.attr("xlink:href", (d) => d.image)
.attr("class", "nodepic")
.attr("x", -40)
.attr("y", -42)
.attr("width", 85)
.attr("height", 85);
/*nodeEnter.attr("transform", (d) => {
console.log(d);
return `translate(${d.x},${d.y})`;
});*/
// add links
kittyData.links.forEach(function(link) {
if (!link.active) {
var s = kittyData.nodes[link.source],
t = kittyData.nodes[link.target],
i = {class: 'hidden'}; // intermediate node
//allNodes.push(i);
if (s != undefined && t != undefined) { // make sure keys exist in nodes
links.push({source: s, target: t});
//linkPaths.push([s, t]);
link.active = true;
}
}
});
// Update and restart the simulation.
//console.log(allNodes);
simulation.stop();
simulation.nodes(allNodes);
simulation.force("link").links(links);
simulation.alpha(1).restart();
// Apply the general update pattern to the links.
/*link = link.data(linkPaths);
link.exit().remove();
link = link.enter().insert("path", "g")
.attr("class", "link").merge(link);*/
// Apply the general update pattern to the links.
//link = link.data(linkPaths);
link = link.data(links, (d) => d.source.id + "-" + d.target.id );
link.exit().remove();
link = link.enter().insert("line", "g").attr("class","link").attr("stroke-width", 2).merge(link);
}
function updateURL() {
if (history.pushState) {
var urlData = Ext.urlEncode({'kid': kittyId});
var newurl = `${window.location.protocol}//${window.location.host}${window.location.pathname}?${urlData}`;
window.history.pushState({path:newurl},'',newurl);
}
}
</script>