-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
482 lines (409 loc) · 11.7 KB
/
script.js
File metadata and controls
482 lines (409 loc) · 11.7 KB
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
var memberNumber = 0;
//go thorugh the form, add alert for unfilled fields and remove alert for filled fields
function checkInput() {
memberNumber = 0;
var inputcheck = true;
$(".familyinput").each(function() {
if (!$(this).find(".icon-selector input:checked").val() ||
!$(this).find(".familyname").val() ||
!$(this).find(".familyrelationship").val()
) {
$(this).find(".alert").show();
inputcheck = false;
} else {
$(this).find(".alert").hide();
}
memberNumber++;
});
if (!$("input[name=familyiconme]:checked").val()) {
$(".meinput").find(".alert").show();
inputcheck = false;
} else if ($("input[name=familyiconme]:checked").val()) {
$(".meinput").find(".alert").hide();
}
return inputcheck;
}
//add input field
function addNewMember() {
if (checkInput() && memberNumber < 10) {
//will not render new position if no input is given
$(".iconselector-container").append(
"<fieldset>" +
"<div class='card-body familyinput' id='familyinput-" + memberNumber + "'>" +
'<button class="btn btn-danger" onclick="removeMember(this)">Remove</button>' +
'<div class="form-row">' +
'<div class="col">' +
"<label for='familyname-" + memberNumber + "'>Name:</label><br>" +
"<input type='text' class='familyname form-control' id='familyname-" + memberNumber + "'>" +
'</div>' +
' <div class="col">' +
"<label for='familyrelationship-" + memberNumber + "'>This person is my:</label><br>" +
"<input type='text' id='familyrelationship-" + memberNumber + "' class='familyrelationship form-control'>" +
'</div>' +
' <div class="col">' +
'<label for="icon-selector-' + memberNumber + '">Choose an avatar from below</label>' +
'<div class="icon-selector">' +
'<img src="icon-1.png" alt="">'+
'<input id="familyicon-0' + memberNumber + '" type="radio" name="familyicon' + memberNumber + '" value="familyicon-0" />' +
'<label class="iconlabel familyicon-0" for="familyicon-0' + memberNumber + '"></label>' +
'<img src="icon-2.png" alt="">'+
'<input id="familyicon-1' + memberNumber + '" type="radio" name="familyicon' + memberNumber + '" value="familyicon-1" />' +
'<label class="iconlabel familyicon-1"for="familyicon-1' + memberNumber + '"></label>' +
'<img src="icon-3.png" alt="">'+
'<input id="familyicon-2' + memberNumber + '" type="radio" name="familyicon' + memberNumber + '" value="familyicon-2" />' +
'<label class="iconlabel familyicon-2"for="familyicon-2' + memberNumber + '"></label>' +
'<img src="icon-4.png" alt="">'+
'<input id="familyicon-3' + memberNumber + '" type="radio" name="familyicon' + memberNumber + '" value="familyicon-3" />' +
'<label class="iconlabel familyicon-3"for="familyicon-3' + memberNumber + '"></label>' +
'</div>' +
'</div>' +
'</div>' +
'<div class="alert alert-danger" role="alert" id="alert-' + memberNumber + '" style="display:none">' +
' Plese input your family member\'s information!' +
' </div>' +
"</div>" +
"</fieldset>"
);
//new position of the add button
$('#addnewinput').remove();
$(".iconselector-container").append(
' <div class="form-row">' +
'<div class="col-lg-4" ></div>' +
' <button onclick="addNewMember()" id="addnewinput">Add another family member</button>' +
'</div>' +
' </div>'
);
}
}
//remove a family member's input field when it is not wanted
function removeMember(a) {
$(a).parent().parent().remove();
}
//prepare the data for the mapping, check the input and start the mapping
function start() {
checkInput();
if (checkInput()) {
var id = 0;
var x = 200;
var y = 200;
//initialise user's data
var meData = {
name: "me",
relationship: "N/A",
id: 0,
x: 400 - 25,
y: 300 - 25, //half the width and height of the image
avatar: 0
};
var meIcon = $("input[name=familyiconme]:checked").val();
avatar = meIcon.match(/familyicon-(\d+)/)[1];
meData.avatar = Number(avatar);
data.nodes.push(meData);
id = 0; //0 is me, family members start from 1
// initialise the family member's data
$(".familyinput").each(function() {
id++;
//init the temporary data
var familymemberData = {
name: "",
relationship: "",
id: 0,
x: 0,
y: 0,
avatar: 0
};
var serial = $(this).find('.familyname').attr("id").match(/familyname-(\d+)/)[1];
var selectedicon = $('input[name=familyicon' + serial + ']:checked').val();
avatar = selectedicon.match(/familyicon-(\d+)/)[1];
avatar = Number(avatar);
familymemberData.avatar = avatar;
familymemberData.name = $(this).find('.familyname').val();
familymemberData.relationship = $(this).find('.familyrelationship').val();
familymemberData.id = id;
data.nodes.push(familymemberData);
});
// startMapping
$(".iconselector-container").remove();
startMapping();
}
}
// d3v4
//data for nodes and links
var data = {
nodes: [],
links: []
};
//avatar urls
var familyimages = ["icon-1.png", "icon-2.png", "icon-3.png", "icon-4.png"];
var tempLink = {
firstNode: null,
secondNode: null
};
//svg elements
var svg, layer0, layer1, layer2, links, nodes, texts;
//main function
function startMapping() {
$("#familymappingsubmit").show();
$('#familytutorial').show();
//append our main element for all the graphics
svg = d3.select(".svg-container")
.append("svg")
.attr("width", 800)
.attr("height", 600);
//group the elements
layer0 = svg.append('g');
layer1 = svg.append('g');
layer2 = svg.append('g');
layer3 = svg.append('g');
//append the arrow
svg.append("defs").selectAll("marker")
.data(["arrow"]) //used for id
.enter()
.append("marker")
.attr("id", function(d) {
return d;
})
.attr("viewBox", "0 -5 10 10")
.attr("refX", 30)
.attr("refY", 0)
.attr("markerWidth", 10)
.attr("markerHeight", 10)
.attr("orient", "auto")
.append("path")
.attr("d", "M0,-5L10,0L0,5");
// data to generate the target: three circles
//origin:(400,300)
var targetData = [{
cx: 400,
cy: 300,
r: 300,
color: '#D4DDE5 '
}, {
cx: 400,
cy: 300,
r: 200,
color: '#90A4C1'
}, {
cx: 400,
cy: 300,
r: 80,
color: '#2685D4' //deepest color in the middle, representing the closest relationship with the people in that area
}];
//draw the target, origin:500,300
target = layer0.selectAll("circle")
.data(targetData)
.enter()
.append('circle')
.attr("cx", function(d, i) {
return d.cx;
})
.attr("cy", function(d, i) {
return d.cy;
})
.attr("r", function(d, i) {
return d.r;
})
.attr("fill", function(d, i) {
return d.color;
});
//place the limited area in a rectangle
canvas = layer0
.append('rect')
.attr("height", 600)
.attr("width", 800)
.attr("x", 0)
.attr("y", 0)
.attr("fill", "none")
.attr("stroke", "gray");
//draw the nodes/family members
nodes = layer2.selectAll("node")
.data(data.nodes)
.enter()
.append('image')
.attr('xlink:href', function(d, i) {
return familyimages[d.avatar];
})
.attr("width", 50)
.attr("height", 50)
.attr("x", function(d, i) {
if (i > 5) {
return d.x += 80;
} else {
return d.x;
}
})
.attr("y", function(d, i) {
if (i > 5) {
return d.y += (i - 5) * 80;
} else {
return d.y += i * 80;
}
})
.on('click', function() {
//store the temporary nodes
if (tempLink.firstNode === null) {
tempLink.firstNode = d3.select(this).datum().id;
} else if (tempLink.firstNode != null && tempLink.secondNode === null && tempLink.firstNode != d3.select(this).datum().id) {
tempLink.secondNode = d3.select(this).datum().id;
//redraw the links
if (data.links.length == 0) {
var newlink = {
source: tempLink.firstNode,
target: tempLink.secondNode
};
data.links.push(newlink);
drawLinks();
tempLink.firstNode = null;
tempLink.secondNode = null;
} else if (data.links.length != 0) {
links.each(function(l, li) {
if ((l.source == tempLink.firstNode && l.target == tempLink.secondNode) || (l.source == tempLink.secondNode && l.target == tempLink.firstNode)) {
// this.remove();
data.links.splice(li, 1); //remove the link in the data array
layer1.selectAll("line").remove().exit();
redrawLinks();
tempLink = {
firstNode: null,
secondNode: null
};
} else if (li == data.links.length - 1 && tempLink.firstNode != null) {
layer1.selectAll("line").remove().exit();
var newlink = {
source: tempLink.firstNode,
target: tempLink.secondNode
};
data.links.push(newlink);
redrawLinks();
tempLink.firstNode = null;
tempLink.secondNode = null;
}
});
}
}
})
.call(d3.drag().on("drag", function(d, i) {
x = d3.event.x;
y = d3.event.y;
if (x > 0 && y > 0 && x < 800 && y < 600) {
//change data.nodes
d3.select(this).datum().x = d3.event.x;
d3.select(this).datum().y = d3.event.y;
//change texts position
texts.remove().exit();
reDrawTexts();
d3.select(this).attr("x", x).attr("y", y);
//change link position
thisNode = d3.select(this).datum().id;
d3.selectAll('line').each(function(l, i) {
if (l.source == thisNode) {
d3.select(this).attr("x1", x).attr("y1", y);
} else if (l.target == thisNode) {
d3.select(this).attr("x2", x).attr("y2", y);
}
});
}
}));
texts = layer3.selectAll("text")
.data(data.nodes)
.enter()
.append('text')
.text(function(d, i) {
return d.name;
})
.attr('fill','#FCB4BC')
.attr('font-size','20px')
.attr("x", function(d, i) {
return d.x;
})
.attr("y", function(d, i) {
return d.y;
});
}
//draw the connections
function drawLinks() {
links = layer1.selectAll("link")
.data(data.links)
.enter()
.append("line")
.attr("class", "link")
.attr("x1", function(l) {
var sourceNode = data.nodes.filter(function(d, i) {
return i == l.source;
})[0];
d3.select(this).attr("y1", sourceNode.y);
return sourceNode.x;
})
.attr("x2", function(l) {
var targetNode = data.nodes.filter(function(d, i) {
return i == l.target;
})[0];
d3.select(this).attr("y2", targetNode.y);
return targetNode.x;
})
.attr("fill", "none")
.attr("stroke", "black")
.attr("stroke-width", 2)
.attr("transform", "translate(" + 30 + "," + 30 + ")")
.attr("marker-end", "url(#arrow)");
}
//redraw the texts using name
function reDrawTexts() {
texts = layer3.selectAll("text")
.data(data.nodes)
.enter()
.append('text')
.text(function(d, i) {
return d.name;
})
.attr('fill','#FCB4BC')
.attr('font-size','20px')
.attr("x", function(d, i) {
return d.x;
})
.attr("y", function(d, i) {
return d.y;
});
}
function redrawLinks() {
links = layer1.selectAll("node")
.data(data.links)
.enter()
.append("line")
.attr("class", "link")
.attr("x1", function(l) {
var sourceNode = data.nodes.filter(function(d, i) {
return i == l.source;
})[0];
d3.select(this).attr("y1", sourceNode.y);
return sourceNode.x;
})
.attr("x2", function(l) {
var targetNode = data.nodes.filter(function(d, i) {
return i == l.target;
})[0];
d3.select(this).attr("y2", targetNode.y);
return targetNode.x;
})
.attr("fill", "none")
.attr("stroke", "black")
.attr("stroke-width", 2)
.attr("transform", "translate(" + 30 + "," + 30 + ")")
.attr("marker-end", "url(#arrow)");
}
// //redraw the texts using relationships
// function reDrawTextsUseRelation() {
// texts = layer3.selectAll("text")
// .data(data.nodes)
// .enter()
// .append('text')
// .text(function(d, i) {
// return d.relationship;
// })
// .attr("x", function(d, i) {
// return d.x
// })
// .attr("y", function(d, i) {
// return d.y
// });
//
// }