-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
437 lines (416 loc) · 16 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
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
<!--INFO 3300 Project 2-->
<!--Created by -->
<head>
<title>Recipe Visualization</title>
<link rel="stylesheet" href="css/stylesheet.css">
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src = 'script/underscore.js'></script>
<script src = 'script/script.js'></script>
<script src = 'https://cdnjs.cloudflare.com/ajax/libs/fuse.js/2.6.1/fuse.min.js'></script>
<script src="script/search.js"></script>
<link rel = "stylesheet" href = "css/animation.css">
<link href="https://fonts.googleapis.com/css?family=Montserrat+Alternates" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet">
<style>
/*svg {border: 1px black solid;}*/
body {
font-family: 'Nunito', sans-serif;
background-color: #FFDE84;
color: brown;
font-size: 15pt;
margin: 0 auto;
padding-top: 20px;
width: 1080px;
}
#text {font-size: 15; font-weight: bold;}
.ttl{
font-family: 'Montserrat Alternates', sans-serif;
font-size: 30pt;
font-weight: bold;
color: #BF262E;
}
.subttl{
font-family: 'Montserrat Alternates', sans-serif;
font-style: italic;
color: darkseagreen;
font-size: 18pt;
}
#personalInfo{
width: 400px;
}
.labels{
float: left;
margin-left: 2px;
}
.inputs{
float: right;
margin-right: 60px;
padding: 3px;
}
#data{
float: right;
width: 600px;
padding-top: 0px;
}
#pot{
float: left;
}
#search-box{
width: 400px;
height: 40px;
font-size: 18pt;
color: #BF262E;
margin-top: 5px;
}
h2{
margin-top: -10px;
margin-bottom: 0px;
}
h3{
margin-top: 0px;
margin-bottom: 0px;
}
#submit{
width: 300px;
margin-left: 50px;
color: white;
background-color: darkseagreen;
height: 30px;
border: 1px solid darkseagreen;
font-size: 17pt;
font-family: 'Montserrat Alternates', sans-serif;
margin-top: 10px;
}
#outer-recipe-box{
margin-top: -10px;
height: 300px;
width: 550px;
overflow: auto;
}
.ing_title{
font-size: 17pt;
color: red;
padding-top: 10px;
}
.ingredients p{
margin: 5px;
}
.backbtn{
font-size: 12pt;
color: darkseagreen;
}
.ingredient:hover,.titles:hover{
color: darkseagreen;
}
</style>
</head>
<body>
<div id = "topText">
<span class = "ttl">What to Eat?</span> <span class = "subttl">A Recipe and Nutrient Data Visualization</span>
<div class = "sub">
<p>If you are ever curious about the nutritional values of each recipe you follow, you are not alone.
There are numerous recipe websites out there: Bon Appetit, Epicurious, NYTimes, Food Network, to name a few.
Very few recipe websites provide nutritional values, though many, even online food ordering services like Grubhub,
have made the attempt and released the beta version. We are fans of the recipe website Epicurious, but unfortunately
Epicurious does not contain nutritional information beyond a total calorie count.<p>
<p> We took the ingredient lists for 5,000 recipes taken from Epicurious website and converted
the amount of each ingredient from kitchen units into grams. Then, we searched for each ingredient in the USDA food database to find the exact nutritional value each recipe contains.
Think of each visualization as a Nutritional Fact label required by FDA on all processed foods,
except here it is real recipe data you can follow at home, allowing you to see which recipe
fits your nutritional needs. Furthermore, total nutritional information can be broken down by ingredient.If you strictly follow along the recipe, this is how much nutrient you will get from it.</p>
<p>To get started, search for an ingredient you have in your pantry (if nothing seems to happen, you may have to refresh. Once the recipes appear (you might have to wait a bit;
count to ten!), click on the recipe for more information! Once on the recipe page, click on the ingredient for more
information about the nutritional breakdown! </p>
</div>
<div id = "pot">
<svg id = "testSvg" width = "400" height = "350">
<image y = "90" xlink:href = "icon/pot.svg"></image>
</svg>
<form>
<input type ='text' id = 'search-box' placeholder = 'Try typing in "banana"!'></input>
</form>
<div id = "personalInfo">
<form name = "macronutrient">
<!-- unable to include other for gender as I have no idea how to calculate BMI for other genders-->
<h2>Macronutrient Calculator</h2>
<div class = "labels">
Gender:<br>
Weight(Lbs):</br>
Height(Inches):</br>
Age(Years):<br>
</div>
<div class = "inputs">
<input type = "radio" name = "gender" value = "0" checked = "true" onclick = "updateRecommendValue()">Male
<input type = "radio" name = "gender" value = "1">Female<br>
<input type = "range" min = "0" max = "400" value = "150" name = "weight" onchange = "updateRecommendValue()"><span id = "weightVal">150</span><br>
<input type = "range" min = "0" max = "100" value = "70" name = "height" onchange = "updateRecommendValue()"><span id = "heightVal">70</span><br>
<input type = "range" min = "0" max = "200" value = "30" name = "age" onchange = "updateRecommendValue()"><span id = "ageVal">30</span><br>
</div>
</form>
</div>
<div id = "error"><p id = "errorMessage"></p></div>
</div>
<div id = "data">
<div id = "recipesContainer">
<h2 id = 'ing-name'>Recipes</h2>
<h3 id = "recipe-list"></h3>
</div>
<div id = "graph">
<svg id = "barGraph" width = '600' height = '750'>
<g id = "graph-elements">
<g id = "axes"></g>
<g id= "text"></g>
<g id = "bars"></g>
</g>
</svg>
</div>
</div>
</body>
<script>
var recipe_ing, recipe_info, ing_nut, scale, recipe_data, slow_write, recipe_search_list;
var color_scale = ['#252525', '#525252', '#969696', '#fc9272','#fb6a4a','#ef3b2c','#cb181d','#a50f15','#67000d']
var scales;
var nutrient_list = ['Calories', 'Fat', 'Cholesterol','Carbohydrates', 'Fiber','Sugar',
'Protein', 'Calcium', 'Iron', 'Vitamin A', 'Vitamin C'];
var svg = d3.select("#barGraph");
var instructionsActive = false;
var search_count = 0;
var clickedElements = [];
$(document).keypress(
function(event){
if (event.which == '13') {
event.preventDefault();
}
});
d3.queue()
.defer(d3.json, "data/recipe_to_nutrition_final.json")
.defer(d3.json, "data/nutrition_reduced.json")
.defer(d3.json, "data/full_format_recipes.json")
.await(function (error, file1, file2, file3) {
console.log(error);
recipe_ing = {};
recipe_info = {};
recipe_search_list = [];
var removed_recipes = {"Tomato Butter ":"Tomato Butter "};
for (var i = 0; i < file3.length; i++){
if (file3[i]['desc'] != null && file3[i]['calories'] != null && file3[i]['calories'] >100 && file3[i]['title'].indexOf("'")==-1){
recipe_info[file3[i]['title']] = {'calories': file3[i]['calories'], 'description': file3[i]['desc'], 'instructions': file3[i]['directions']};
}
else{
removed_recipes[file3[i]['title']] = file3[i]['title'];
}
}
for (var i = 0; i < file1.length; i++){
var title = Object.keys(file1[i])[0];
if (title!=null && removed_recipes[title]==null && title.indexOf("'") ==-1
&& title.indexOf("Dressing")==-1 && title.indexOf("Stock")==-1 &&
title.indexOf("Broth") == -1 && title.indexOf("Butter") == -1){
recipe_ing[title] = file1[i][title];
recipe_search_list.push({'title': title, 'ingredients': Object.keys(file1[i][title])});
}
}
ing_nut = file2;
scales = [];
var options = {keys:['ingredients'], id: 'title'};
var fuse = new Fuse (recipe_search_list, options);
var prev_search;
$("#search-box").keyup(function(){
var val = this.value;
if(!val.match("^[a-zA-Z0-9 ]*$")){
clear_everything();
document.getElementById("errorMessage").innerHTML = "Please re-type.";
}
else if (val.trim() != prev_search){
var final_results = slow_write(val);
}
});
d3.select("#recipesContainer").append("div").attr("id", "outer-recipe-box");
for (var i = 0; i < 100; i++){
d3.select("#outer-recipe-box").append("div").attr("class", "recipe-box").attr("id", "recipe-name-box"+i);
d3.select("#recipe-name-box"+i).append("p").attr("id", "recipe-name"+i).attr("class", "titles").attr("onclick", "toggleInstruction('#ingredientsContainer"+i+"')");
d3.select("#recipe-name-box"+i).append("div").attr("class", "ingredients").attr("id", "ingredientsContainer" + i).style("display", "none");
d3.select("#ingredientsContainer"+i).append("div").attr("id", "ingredientsTitle" + i).attr("class", "ing_title");
d3.select("#ingredientsContainer"+i).append("p").attr("id", "description"+i).attr("class", "description");
for (var j = 0; j <10; j++){
d3.select("#ingredientsContainer"+i).append("p").attr("id", "#ingredients"+ i + "-" + j).attr("class", "ingredient")
.on("click", function(){
var l = this.id.charAt(this.id.length-1);
var class_name = this.innerHTML.slice(6);
class_name = class_name.replace(/\W/g, '');
if (clickedElements[this.id]==null || clickedElements[this.id] =="off"){
clickedElements[this.id]="on";
this.style.color = color_scale[l];
d3.selectAll("."+class_name).attr("fill", color_scale[l]).attr("stroke", color_scale[l]);
}
else{
clickedElements[this.id]="off";
this.style.color = "brown";
d3.selectAll("."+class_name).attr("fill", "#3E59B3").attr("stroke", "#3E59B3");
}
});
}
d3.select("#ingredientsContainer"+i).append("p").attr("id", "instructions"+i).attr("class", "description");
}
var slow_write = _.debounce( function(term){
document.getElementById("recipe-list").innerHTML = "You searched for: " + term + " <br><br> ";
prev_search = term.trim();
if (term.trim()==''){
clear_everything();
}
else{
search_count += 1;
fuse.search(" "+term);
var results = fuse.results;
var new_results = [];
for (i = 0; i <results.length; i++){
if (results[i]['score'] < .30){
new_results.push(results[i]['item']);
}
}
arr = new_results.filter( function( item, index, inputArray ) {
return inputArray.indexOf(item) == index;
});
if (arr.length == 0){
clear_everything();
document.getElementById("errorMessage").innerHTML = term + " was not found. Please try again.";
}
else{
for (var i = 0; i < Math.min(arr.length, 100); i++){
var recipe_name = arr[i];
if (recipe_info[recipe_name]!=null){
document.getElementById("recipe-name"+i).innerHTML = recipe_name;
document.getElementById("description" + i).innerHTML = "Description: <br>" + recipe_info[recipe_name]['description'];
document.getElementById("instructions" + i).innerHTML = "Instructions: <br>" + recipe_info[recipe_name]['instructions'][0]+"<br><br>"
for ( var j = 1; j < recipe_info[recipe_name]['instructions'].length; j++){
document.getElementById("instructions" + i).innerHTML += recipe_info[recipe_name]['instructions'][j]+"<br><br>";
}
var ing_list = Object.keys(recipe_ing[recipe_name]);
if (ing_list.length < 10){
list_max = ing_list.length;
}
else {
list_max = 10;
}
for (var j = 0; j < list_max; j++){
if (ing_list[j].trim() !=''){
document.getElementById("#ingredients"+ i + "-" + j).innerHTML = ing_list[j];
}
}
}
document.getElementById("ingredientsTitle"+i).innerHTML = recipe_name + "<span class = 'backbtn' onclick = 'toggleInstruction("+'"#ingredientsContainer'+i+'")'+"'>Back</span>";
document.getElementById("errorMessage").innerHTML = '';
}
}
}
getIcon(term);
}, 700);
});
var rad = document.macronutrient.gender;
var height = document.macronutrient.height;
var weight = document.macronutrient.weight;
var age = document.macronutrient.age;
function updateRecommendValue(){
rad = document.macronutrient.gender;
height = document.macronutrient.height;
weight = document.macronutrient.weight;
age = document.macronutrient.age;
var bmr = BMRCalculator(rad.value, weight.value, height.value, age.value);
console.log(bmr);
var calories = caloriesCalculator(1, bmr);
var macro = macronutrientCalculator(1, calories);
var tmp = [3,6,1];
d3.select("#recommendLines0").attr("x1", scales[0](calories)+100);
d3.select("#recommendLines0").attr("x2", scales[0](calories)+100);
for (var i = 1; i < macro.length; i++)
{
console.log(scales[tmp[i]](macro[i]));
console.log(macro);
d3.select("#recommendLines"+tmp[i]).attr("x1", scales[tmp[i]](macro[i])+100);
d3.select("#recommendLines"+tmp[i]).attr("x2", scales[tmp[i]](macro[i])+100);
}
document.getElementById("weightVal").innerHTML = weight.value;
document.getElementById("heightVal").innerHTML = height.value;
document.getElementById("ageVal").innerHTML = age.value;
}
/* Macronutrient Calculator */
function BMRCalculator(gender, weight, height, age){
//gender: 0 = male, 1 = female
if (gender == 1)
return 655.1 + (4.35 * weight) + (4.7 * height) - (4.7 * age);
else
return 66 + (6.2 * weight) + (12.7 * height) - (6.76 * age);
}
function caloriesCalculator(exercise, BMR){
return BMR * (1.2 + exercise * 0.175);
}
function macronutrientCalculator(ratio, calories){
//ratio = 0, body building ratio 50-30-20 (carb-protein-fat)
//ratio = 1, maintenance ratio 40-30-30
//ratio = 2, fat loss ratio 20-35-45
if (ratio == 0)
return [calories * .5 / 4, calories * .3 / 5.5, calories * .2 / 9];
else if (ratio == 1)
return [calories * .4 / 4, calories * .3 / 5.5, calories * .3 / 9];
else if (ratio == 2)
return [calories * .2 / 4, calories * .35 /5.5, calories * .45 / 9];
else
return [281, 112.5, 75];
}
/* Animation/Interface Functions */
function getIcon(ingredient){
$.ajax({
url: "icon/" + ingredient + ".svg",
type: "HEAD",
error: function(){fadeInText(ingredient);},
success: function(){fadeIn(ingredient);}
});
}
function fadeIn(element){
svg = d3.select("#testSvg");
d3.select(".fade").remove();
svg.append("image")
.attr("x", 180)
.attr("y", 0)
.attr("height", 125)
.attr("class", "fade fall")
.attr("id", element)
.attr("xlink:href", "./icon/" + element + ".svg");
}
function fadeInText(element){
svg = d3.select("#testSvg");
d3.select(".fade").remove();
svg.append("text")
.attr("x", 200)
.attr("y", 0)
.text(element)
.attr("class", "fade fall")
.attr("id", element);
}
function toggleInstruction(element){
var titles = document.getElementsByClassName("titles");
if (d3.select(element).style("display") == "none"){
if (!instructionsActive){
d3.select(element).style("display", "block");
instructionsActive = true;
create_graph(titles[element.substring(element.length-1)].innerText + " ",recipe_ing, recipe_info, ing_nut, scales);
for (var i = 0; i < titles.length; i++){
titles[i].style.display = "none";
}
}
}
else {
d3.select(element).style("display", "none");
instructionsActive = false;
for (var i = 0; i < titles.length; i++){
titles[i].style.display = "block";
}
}
}
function clear_everything(){
d3.selectAll('.titles').text(" ");
document.getElementsByClassName('ingredient').innerHTML=' ';
document.getElementsByClassName('description').innerHTML=' ';
d3.select(".ing_title").text(" ");
}
</script>