-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
625 lines (524 loc) · 21.9 KB
/
index.html
File metadata and controls
625 lines (524 loc) · 21.9 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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>SpeedCooking</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Lora" rel="stylesheet">
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/d3-color.v1.min.js"></script>
<script src="https://d3js.org/d3-interpolate.v1.min.js"></script>
<script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script>
<!-- Tooltip style -->
<style>
.hidden {
display: none;
}
div.tooltip {
color: #222;
background-color: #fff;
padding: .5em;
text-shadow: #f5f5f5 0 1px 0;
border-radius: 2px;
opacity: 0.9;
position: absolute;
}
</style>
</head>
<body>
<!-- Barre de titre -->
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<span class="navbar-brand">SpeedCooking</span>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="https://github.com/tblondelle/DataVisualisationProject">Fork me on GitHub</a>
</li>
<li class="nav-item">
<a class="nav-link" target="_blank" href="https://github.com/tblondelle/DataVisualisationProject/blob/master/README.md">About</a>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" data-toggle="modal" data-target="#exampleModal">Ingredients</a>
</li>
</ul>
<select class="selectRecipes form-control"></select>
</form>
</div>
</nav>
<svg></svg>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">List of ingredients</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body" id="ingredients-list">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<script>
var width = window.innerWidth,
height = window.innerHeight - 100,
margins = {top: 15, right: 15, left: 15, bottom: 20},
size_node = 25;
var recipe_chosen,
data_recipe,
selectedNode = undefined;
var svg = d3.select("svg")
.attr("width", width)
.attr("height", height);
var select = d3.select("select");
var tooltip = d3.select('body').append('div')
.attr('class', 'hidden tooltip');
var bezierLine = d3.line()
.x(function(d) { return d[0]; })
.y(function(d) { return d[1]; })
.curve(d3.curveBasis);
// Depth evaluation
function set_and_get_height(i,etapes){
var children = etapes[i].enfants
var children_heights = [];
children.forEach(function (c) {
children_heights.push(set_and_get_height(c,etapes));
});
var h_c = Math.max.apply(null, children_heights) + 1;
var h = 1;
if (h_c > h) {
h = h_c;
};
etapes[i].height = h; // setting the heights for the children
return h;
};
d3.json("recettes.json", function(json) {
// By default, draw the first recipe.
data_recipe = json.recettes[0]
drawEverything(data_recipe)
// Add all recipes in the dropdown.
select.selectAll("option").data(json.recettes).enter()
.append("option")
.attr("value", function(d) {return d.nom})
.html(function(d) {return d.nom})
// Draw recipe when the select input is used.
select.on("change", function(){
recipe_chosen = d3.select(".selectRecipes").node().value;
for (var i = 0; i < json.recettes.length; i++){
if (json.recettes[i].nom == recipe_chosen) {
removeEverything();
data_recipe = json.recettes[i];
drawEverything(data_recipe);
break
}
}
});
// Redraw recipe when window is resized.
window.addEventListener("resize", function( ){
removeEverything(data_recipe)
width = window.innerWidth;
height = window.innerHeight - 150;
var svg = d3.select("svg")
.attr("width", width)
.attr("height", height)
drawEverything(data_recipe)
});
function removeEverything(){
svg.selectAll("*").remove();
}
function drawEverything(recipe){
// ================================
// ======= data modifications =====
// ================================
// Add final "terminé" node to the recipe.
if (recipe.etapes[recipe.etapes.length - 1].description_courte !== "Terminé !"){
var finish_object = {}
var but_last_object = recipe.etapes[recipe.etapes.length - 1]
finish_object.identifiant = +but_last_object.identifiant + 1
finish_object.description_courte = "Terminé !"
finish_object.description_longue = "La recette est terminée !"
finish_object.enfants = [but_last_object.identifiant]
finish_object.lieu = but_last_object.lieu
finish_object.parent = but_last_object.parent
finish_object.type = "termine"
but_last_object.parent = "" +finish_object.identifiant
recipe.etapes.push(finish_object)
}
// choix de localisation des lieux :
var locations_obj = [];
var lieux_visites = [];
// On commence par chercher un four. S'il y en a un, on le met au dessus
for (var i=0; i<recipe.etapes.length; i++){
if (recipe.etapes[i].lieu == 'four'){
locations_obj[0] = {lieu: "four", id: 1};
lieux_visites[0] = "four";
};
};
// Ensuite, on parcourt les étapes pour y ajouter les lieux.
var id_min = locations_obj.length; // (1 si on a ajouté le four, 0 sinon)
for (var i=0; i<recipe.etapes.length; i++){
var lieu = recipe.etapes[i].lieu;
if (! lieux_visites.includes(lieu)) {
locations_obj[id_min] = {lieu: lieu, id: id_min+1};
lieux_visites[id_min] = lieu;
id_min++;
};
};
// Add in location_obj the nodes classified.
locations_obj.forEach(function(location,j){
location.etapes = [];
})
recipe.etapes.forEach(function(etape,i){
locations_obj.forEach(function(location,j){
if (location.lieu === etape.lieu){
location.etapes.push(etape.identifiant)
}
})
})
// =================================
// ======= Print SVG elements ======
// =================================
// Create background for locations (four, plaque de travail 1, ...)
var heigth_rect = (height - margins.top - margins.bottom) / locations_obj.length;
locations = svg.selectAll(".locations").data(locations_obj).enter()
.append("g")
.attr("class", "locations")
.attr("transform", function(d, i) {
var translateY = parseInt(margins.top) + i*heigth_rect;
return "translate(" + margins.left + "," + translateY + ")"
})
locations.append("rect")
.attr("class", function(d){return d.lieu})
.attr("height", function(d){return heigth_rect})
.attr("x", 0)
.attr("y", 0)
.attr("fill", function(d,i){
if (i%2 !== 0){
return "#eee8aa"
} else {
return "#deb887"
}
})
.style("opacity", 0.5)
.attr("width", 0)
.transition().delay(function(d, i){return i*100}).duration(1200)
.attr("width", function(){return width - margins.right - margins.left})
locations.append("text")
.style("font-weight", "bold")
.style("opacity", 0.6)
.attr("class", "location_title")
.attr("transform", function(d, i) {
return "translate(10,20)"
})
.html(function(d){return capitalize(d.lieu)})
var max_height = set_and_get_height(recipe.etapes.length-1, recipe.etapes);
var scale_x = d3.scaleLinear() // prend en entrée les profondeurs, et renvoie les x
.domain([1, max_height+1])
.range([4*margins.left, width - margins.right - margins.left]);
var nodes = svg.selectAll(".node").data(recipe.etapes).enter()
.append("g")
.attr("class", "node")
.attr("id", function(d){
return "node_" + d.identifiant
})
.attr("transform", function(d){
// Get the x position of the node in the grid
var x = scale_x(d.height);
d.x = x;
d.x_index = d.height;
// Get the id of the y position of the node.
var y_index;
for (var ind = 0; ind < locations_obj.length; ind++){
if (locations_obj[ind].lieu === d.lieu){
y_index = locations_obj[ind].id;
}
}
var y = margins.top + heigth_rect * (y_index - 1/3);
d.y = y;
d.y_index = y_index;
return "translate(" + x + "," + y + ")"
})
nodes.append('image')
.attr("width", 0)
.attr("height", 0)
// Fonction pour avoir la bonne image en fonction du type d'étape
.attr('xlink:href',function(d){
switch (d.type){
case "merge":
return 'icones/merge.svg'
break
case "ajout":
return 'icones/ajout.svg'
break
case "decoupe":
return 'icones/decoupe.svg'
break
case "cuisson_plaque":
return 'icones/cuisson_plaque.svg'
break
case "cuisson_four":
return 'icones/cuisson_four.svg'
break
case "patienter":
return 'icones/patienter.svg'
break
case "termine":
return 'icones/termine.svg'
break
default:
return ('icones/autre.svg')}
})
.attr('class', 'pico')
// while adding an image to an svg these are the coordinates i think of the top left
.attr('x', '0')
.attr('y', '0')
.transition().ease(d3.easeBackOut.overshoot(10)).delay(function(d, i){return i*70}).duration(400)
.attr("transform", function(){
return "translate(" + (-size_node/2) + "," + (-size_node/2) + ")"
})
.attr("width", size_node)
.attr("height", size_node)
nodes.on("mouseover", function(d){
unhighlightSelectedNode(selectedNode);
selectedNode = d.identifiant;
highlightSelectedNode(selectedNode);
})
nodes.on("mouseout", function(d){
unhighlightSelectedNode(selectedNode);
})
// =======================
// ========= text ========
// =======================
var font_size = 15; //TODO
var margin_text = 2; // nb de pixels autour des textes
var delta_x = Math.round((width - margins.left - margins.right - margin_text)/(max_height + 1));
var max_letters = Math.floor(2.5*(delta_x-2*margin_text)/font_size) ; // nombre max. de lettres dans la description
// avant un retour à la ligne; le 2.5 est empirique.
//console.log(font_size,margin_text,delta_x,max_letters);
for (var i=0; i<recipe.etapes.length; i++){
var d = recipe.etapes[i];
var desc = d.description_courte;
d.descriptions_courtes_lignes=[] ;
if (desc.length < max_letters){
// On affiche la ligne en bas.
d.descriptions_courtes_lignes[0] = "";
d.descriptions_courtes_lignes[1] = capitalize((desc));
} else {
// on cherche le premier caractère 'espace', pour couper.
var search = max_letters;
while ((desc[search] != ' ')&&(search >=1) ){
search = search-1;
};
d.descriptions_courtes_lignes[0] = capitalize((desc).substring(0,search));
d.descriptions_courtes_lignes[1] = capitalize((desc).substring(search));
};
};
nodes.append("text")
.html(function(d){ return d.descriptions_courtes_lignes[1];})
.attr("transform", function(){
return "translate(" + (-Math.floor(0.7*size_node)) + "," + (-Math.floor(0.7*size_node)) + ")"
})
.attr("font-size", font_size)
.style("opacity", 0)
.transition().delay(function(d, i){return i*70}).duration(200)
.style("opacity", 1)
nodes.append("text")
.html(function(d){ return d.descriptions_courtes_lignes[0]; })
.attr("transform", function(){
return "translate(" + (-Math.floor(0.7*size_node))+ "," + (-Math.floor(0.7*size_node)-font_size) + ")"
})
.attr("font-size", font_size)
.style("opacity", 0)
.transition().delay(function(d, i){return i*70}).duration(200)
.style("opacity", 1)
// =======================
// ======== links ========
// =======================
// Create data for links
var etapes = recipe.etapes
// on suppose que les champs x et y sont déjà définis pour chaque étape
var all_lines_to_draw = []
for(var i=0; i< etapes.length; i++){
var xp = etapes[i].x - size_node*0.75;
var yp = etapes[i].y;
etapes[i].enfants.forEach(function (c) {
var xc = etapes[c].x + size_node*0.75;
var yc = etapes[c].y;
if (etapes[i].height > etapes[c].height+1){
// Ligne droite puis virage
var delta_height = etapes[i].height - etapes[c].height;
var new_xc = xp + (xc-xp)/delta_height;
all_lines_to_draw.push([[xp,yp],[new_xc,yc],[xc,yc]]);
} else {
// Simple virage.
all_lines_to_draw.push([[xp, yp],[xc,yc]])
};
});
};
// Trie les données selon l'abscisse.
all_lines_to_draw.sort(function (a, b) { return a[0][0] - b[0][0]; });
// Affiche les liens.
var links = svg.selectAll(".links").data(all_lines_to_draw).enter()
.append('path')
.attr("class", "links")
.attr("d", function(d){
var x_int1 = d[0][0]*(2/3) + d[1][0]*(1/3),
y_int1 = d[0][1];
var x_int2 = d[0][0]*(1/3) + d[1][0]*(2/3),
y_int2 = d[1][1];
if (d.length === 2){
return bezierLine([d[0],[x_int1,y_int1], [x_int2,y_int2], d[1]])
} else if (d.length === 3){
return bezierLine([d[0],[x_int1,y_int1], [x_int2,y_int2], d[1], d[2]])
}
})
.attr("stroke", "#680000")
.attr("stroke-width", 5)
.style("opacity", 0.5)
.attr("fill", "none")
.attr("stroke-dasharray", function(){
return this.getTotalLength() + ", " + this.getTotalLength()
})
.attr("stroke-dashoffset", function(){
return -this.getTotalLength()
});
links.transition().delay(function(d, i){return i*70}).duration(400)
.attr("stroke-dashoffset", function(){
return 0
});
// ====================================
// ======= keyboard interaction =======
// ====================================
function getNextNode(selectedNode, direction){
// Get next node when the keyboard is used.
var all_positions = []
recipe.etapes.forEach(function(d, i){
all_positions.push(d.x_index + " " + d.y_index)
})
// If no node are selected, select the first one.
if (selectedNode === undefined){
selectedNode = 0
return selectedNode
} else {
var data_node = recipe.etapes[selectedNode]
switch (direction){
case 37: //left
// Get the first child node of it.
if (data_node.enfants[0]){
selectedNode = +(data_node.enfants[0])
} else {
selectedNode = undefined
}
break
case 38: //up
// Find the nearest neighbor above. abscisse proximity is prefered to ordinate.
for (var x_index = data_node.x_index; x_index < 10; x_index++){
for (var y_index = data_node.y_index - 1; y_index >= 0; y_index--){
if (all_positions.indexOf(x_index + " " + y_index) !== -1){
selectedNode = all_positions.indexOf(x_index + " " + y_index)
return selectedNode
}
}
}
selectedNode = undefined
break
case 39: //right.
// Get the parent node of it.
if (data_node.parent === "-1"){
selectedNode = undefined
} else {
selectedNode = +data_node.parent
}
break
case 40: //down
// Find the nearest neighbor below. abcsisse proximity is prefered to ordinate.
for (var x_index = data_node.x_index; x_index < 10; x_index++){
for (var y_index = data_node.y_index + 1; y_index < 100; y_index++){
if (all_positions.indexOf(x_index + " " + y_index) !== -1){
selectedNode = all_positions.indexOf(x_index + " " + y_index)
return selectedNode
}
}
}
selectedNode = undefined
break
}
return selectedNode
}
}
function unhighlightSelectedNode(selectedNode){
// Remove old highlighted node.
nodes.selectAll("g#node_" + selectedNode + " > image")
.transition()
.attr("transform", function(){
return "translate(" + (-size_node/2) + "," + (-size_node/2) + ")"
})
.attr("width", size_node)
.attr("height", size_node)
// Hide tooltip
tooltip.classed('hidden', true)
}
function highlightSelectedNode(selectedNode){
nodes.selectAll("g#node_" + selectedNode + " > image")
.transition().ease(d3.easeBackOut.overshoot(3))
.attr("transform", "translate(" + (-size_node) + "," + (-size_node) + ")")
.attr("width", size_node*2)
.attr("height", size_node*2)
// Show tooltip.
tooltip
.classed('hidden', false)
.attr('style', function(){
var x = recipe.etapes[selectedNode].x - 100;
var y = recipe.etapes[selectedNode].y + 100;
if (x < 0) x = 0 ;
return 'left:' + x + 'px; top:' + y + 'px'
})
.html(function(){
return capitalize(recipe.etapes[selectedNode].description_longue)
})
}
d3.select('body').on("keydown", function(d) {
if (selectedNode !== undefined) unhighlightSelectedNode(selectedNode)
selectedNode = getNextNode(selectedNode, d3.event.keyCode)
if (selectedNode !== undefined) highlightSelectedNode(selectedNode)
})
// ====================================
// =========== ingredients ============
// ====================================
d3.select("#ingredients-list")
.html(function(){
var string = '<ul class="list-group">'
for (var i = 0; i<data_recipe.ingredients.length; i++){
string += '<li class="list-group-item list-group-item-action">' + data_recipe.ingredients[i] + "</li>"
}
return string + "</ul>"
})
}
})
function capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
</script>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script>
$('#myModal').on('shown.bs.modal', function () {
$('#myInput').trigger('focus')
})
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>