-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdrinkVizCards.html
442 lines (375 loc) · 16.1 KB
/
drinkVizCards.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
438
439
440
441
442
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>drinkBase</title>
<script src="https://cdn.rawgit.com/eligrey/canvas-toBlob.js/f1a01896135ab378aa5c0118eadd81da55e698d8/canvas-toBlob.js"></script>
<script src="https://cdn.rawgit.com/eligrey/FileSaver.js/e9d941381475b5df8b7d7691013401e171014e89/FileSaver.min.js"></script>
<script src="https://d3js.org/d3.v4.min.js" charset="utf-8"></script>
<script type="application/javascript">
// SELECT MODE
// either select a drink with tcPick or set it to false to use drop down menu
var tcPick = false;
//var tcPick = 'Chartreuse Swizzle';
var boxWidth = 330;
var defaultStartingPick = 37; // the index of the starting drink in the drop down
var Xbase = 10;
var Ybase = 345;
var origDF;
var recipeDF;
var prepDF;
String.prototype.trunc =
function( n, useWordBoundary ){
if (this.length <= n) { return this; }
var subString = this.substr(0, n-1);
return (useWordBoundary
? subString.substr(0, subString.lastIndexOf('|'))
: subString) + "...";
};
function getData() {
//get recipes
d3.csv("drinkBase/data/recipe.csv", function(d) {
recipeDF = d;
});
//get prep
d3.csv("drinkBase/data/prep.csv", function(d) {
prepDF = d;
});
// select drink from file
d3.csv('drinkBase/data/style_drinks.csv', function(d) {
origDF = d;
fillDrinks();
drawPlot();
});
}
// fill the drop down menu
function fillDrinks() {
if (tcPick) {
console.log("naw")
} else {
d3.select("#drinkDropDown").selectAll("option").remove();
var p = d3.select("#drinkDropDown").selectAll("option")
.data(origDF.sort(function (a,b) {
return d3.ascending(a.name, b.name);
}))
.enter()
.append("option")
.text(function(d) {
return d.name
});
document.getElementById("drinkDropDown").selectedIndex = defaultStartingPick;
}
}
// draws the plot
function drawPlot() {
d3.select('#theDrinks').selectAll("*").remove();
if (tcPick) {
thePick = tcPick;
} else {
var e = document.getElementById("drinkDropDown");
var thePick = e.options[e.selectedIndex].text;
}
fillRecipe(thePick);
var drinksDF = origDF.filter(function(d) {
return d.name.toLowerCase() == thePick.toLowerCase()
});
drinksSVG = d3.select('#theDrinks');
// IMAGE //////////
/*
drinksSVG.append("svg:image")
.attr('x', 6)
.attr('y', 6)
.attr('width', 328)
.attr('height', 318)
.attr("xlink:href", "drinkBase/pics/french75.jpg")
//.attr("xlink:href", "drinkBase/pics/oldfashioned.jpg")
*/
var picsDir = "drinkBase/pics/"
var drinkPic = drinksSVG.selectAll("#drinkPic")
.data(drinksDF)
.enter().append("svg:image")
.attr('x', 6)
.attr('y', 6)
.attr('width', 328)
.attr('height', 318)
.attr("xlink:href", function(d) {
if (d.glass == 'coupe') {
if (d.ingredients.includes('Chartreuse')) {
return picsDir + "lastword.png";
} else if (
(d.ingredients.includes('gin')) |
(d.ingredients.includes('lime'))
) {
return picsDir + "gimlet.jpg";
} else {
return picsDir + "bijou.png";
}
} else if (d.name == 'Jungle Bird') {
return picsDir + "junglebird.png";
} else if (d.style == 'hot') {
return picsDir + "hottoddy.jpg";
} else if (d.style == 'swizzle') {
return picsDir + "swizzle.jpg";
} else if (d.glass == 'collins') {
return picsDir + "tomcollins.jpg";
} else if (d.style == 'built') {
return picsDir + "oldfashioned.jpg";
} else if (d.glass == 'old-fashioned') {
return picsDir + "bitterhandshake.png";
} else if (d.style == 'bubbly') {
return picsDir + "french75.jpg";
} else {
return picsDir + "oldfashioned.jpg";
}
})
///////////////////
// upper bounding boxes
drinksSVG.append("rect")
.attr ("x", 5 )
.attr ("y", 5 )
.attr("height", Ybase-25).attr("width", boxWidth)
.attr("fill", "none").attr ("stroke", '#999')
drinksSVG.append("rect")
.attr ("x", 8 )
.attr ("y", 8 )
.attr("height", Ybase-31).attr("width", boxWidth-6)
.attr("fill", "none").attr ("stroke", '#999')
// lower bounding boxes
drinksSVG.append("rect")
.attr ("x", 5 )
.attr ("y", Ybase-20 )
.attr("height", 170).attr("width", boxWidth)
.attr("fill", "none").attr ("stroke", '#999')
drinksSVG.append("rect")
.attr ("x", 8 )
.attr ("y", Ybase-17 )
.attr("height", 164).attr("width", boxWidth-6)
.attr("fill", "none").attr ("stroke", '#999')
// NAME
var nameText = drinksSVG.selectAll("#nameText")
.data(drinksDF)
.enter().append("text")
.attr("id", "nameText")
.attr ("dx", Xbase + 15 )
.attr ("dy", Ybase + 15 )
.attr ("font-size", 24)
.text(function(d){return d.name});
//.on("mouseover", fillRecipe)
//.on("mouseout", clearRecipe);
// INGREDIENTS
var ingredText = drinksSVG.selectAll("#ingredText")
.data(drinksDF)
.enter().append("text")
.attr("id", "ingredText")
.attr ("font-size", 12)
.attr ("font-style", "italic")
.attr ("dx", Xbase + 15 )
.attr ("dy", Ybase + 55 )
.text(function(d){return d.ingredients.trunc(50, true)})
// STYLE
var styleText = drinksSVG.selectAll("#styleText")
.data(drinksDF)
.enter().append("text")
.attr("id", "styleText")
.attr ("dx", Xbase + 40 )
.attr ("dy", Ybase + 35 )
.text(function(d){return d.style + ", " + Math.round(d.oz * 10) / 10 + " oz." })
// GLASS
/*
var glassText = drinksSVG.selectAll("#glassText")
.data(drinksDF)
.enter().append("text")
.attr("id", "glassText")
.attr ("dx", Xbase + 15 )
.attr ("dy", Ybase + 55 )
.text(function(d){return "served in " + d.glass + " glass"})
*/
drinksSVG.selectAll("circle").remove()
var barWidth = 130;
var barHeight = 20;
var barOffset = 75;
// ALCOHOL
var alcMult = 375;
drinksSVG.append("rect")
.attr ("x", Xbase + barOffset )
.attr ("y", Ybase + 63 )
.attr("height", barHeight).attr("width", barWidth)
.attr("fill", "none").attr ("stroke", '#999')
var alcRect = drinksSVG.selectAll("#alcRect").data(drinksDF).enter()
.append("rect")
.attr ("x", Xbase + barOffset )
.attr ("y", Ybase + 63 )
.attr("height", barHeight)
.attr("width", function(d) {
return Math.min(barWidth, Math.max(0, (d.alcohol * alcMult)));
})
.attr("fill", "#ec4527")
.attr ("stroke", 'none')
drinksSVG.append("text")
.attr ("dx", Xbase + 15 )
.attr ("dy", Ybase + 79 )
.text("ABV")
var alcText = drinksSVG.selectAll("#alcText").data(drinksDF).enter()
.append("text")
.attr("id", "alcText")
.attr ("dx", Xbase + barOffset + 4 )
.attr ("dy", Ybase + 79 )
.text(function(d){return Math.round(d.alcohol * 100) + "%" })
// SUGAR
var sugarMult = 900;
drinksSVG.append("rect")
.attr ("x", Xbase + barOffset )
.attr ("y", Ybase + 85.5 )
.attr("height", barHeight).attr("width", barWidth)
.attr("fill", "none").attr ("stroke", '#999')
var sugarRect = drinksSVG.selectAll("#sugarRect")
.data(drinksDF)
.enter().append("rect")
.attr ("x", Xbase + barOffset )
.attr ("y", Ybase + 85.5 )
.attr("height", barHeight)
.attr("width", function(d) {
return Math.min(barWidth, Math.max(0, (d.sugar * sugarMult)));
})
.attr("fill", "#dcd5ff")
.attr ("stroke", '#999')
drinksSVG.append("text")
.attr ("dx", Xbase + 15 )
.attr ("dy", Ybase + 101.5 )
.text("sugar")
var sugarText = drinksSVG.selectAll("#sugarText")
.data(drinksDF)
.enter().append("text")
.attr("id", "sugarText")
.attr ("dx", Xbase + barOffset + 4 )
.attr ("dy", Ybase + 101.5 )
//.text(function(d){return Math.round(Math.min(barWidth*10, Math.max(0, (d.sugar * sugarMult * 10))) / (barWidth/10)) / 10 })
.text(function(d){return Math.round(d.sugar * d.oz * 28.35) + "g"})
// ACID
var acidMult = 10000;
drinksSVG.append("rect")
.attr ("x", Xbase + barOffset )
.attr ("y", Ybase + 108 )
.attr("height", barHeight).attr("width", barWidth)
.attr("fill", "none").attr ("stroke", '#999')
var acidRect = drinksSVG.selectAll("#acidRect")
.data(drinksDF)
.enter().append("rect")
.attr ("x", Xbase + barOffset )
.attr ("y", Ybase + 108 )
.attr("height", barHeight)
.attr("width", function(d) {
return Math.min(barWidth, Math.max(0, (d.acid * acidMult)));
})
.attr("fill", "#f4e381")
.attr ("stroke", '#999')
drinksSVG.append("text")
.attr ("dx", Xbase + 15 )
.attr ("dy", Ybase + 124 )
.text("acid")
var acidText = drinksSVG.selectAll("#acidText").data(drinksDF).enter()
.append("text")
.attr("id", "acidText")
.attr ("dx", Xbase + barOffset + 4 )
.attr ("dy", Ybase + 124 )
//.text(function(d){return Math.round(Math.min(barWidth*10, Math.max(0, (d.acid * acidMult * 10))) / (barWidth/10)) / 10 })
.text(function(d){return Math.round(d.acid * 10000) / 100 + "%" })
var styleCirc = drinksSVG.selectAll("#styleCirc")
.data(drinksDF)
.enter().append("circle")
.attr("id", "styleCirc")
.attr ("stroke-width", 0.75)
.attr ("stroke", '#999')
.attr ("cx", Xbase + 25 )
.attr ("cy", Ybase + 30 )
.attr ("r", 9)
.attr ("fill-opacity", 0.85)
.attr("fill",function(d) {
if (d.style.includes("stirred")) {
return "#a5693d";
} else if (d.style.includes("bubbly")) {
return "#fcf5bf";
} else if (d.style.includes("shaken")) {
return "#100656";
//return "#240ccc";
} else if (d.style.includes("fizz")) {
return "#f4e381";
} else if (d.style.includes("swizzle")) {
return "#f27552";
} else if (d.style.includes("built")) {
return "#b5390c";
} else {
return "#bbb";
}
});
}
function fillRecipe(thisName) {
d3.select("#recipeTitle").text(thisName).style("font-weight", "bold");
d3.select("#recipeBox").selectAll('#recipeIng').remove();
d3.select("#recipeBox").selectAll('#recipeBody').remove();
var rec = d3.select("#recipeBox").selectAll("#recipeIng")
.data(recipeDF.filter(function(d) { return d.name == thisName }))
.enter()
.append("div")
.attr('id', 'recipeIng')
.text(function(d) {
return d.amount + ' ' + d.unit + ' ' + d.ingredient;
});
var inst = d3.select("#recipeBox").selectAll("#recipeBody")
.data(prepDF.filter(function(d) { return d.name == thisName }))
.enter()
.append("p")
.attr('id', 'recipeBody')
.text(function(d) {
var prepDirections = '';
// style
if (d.notes.length > 0) {
prepDirections += d.notes
} else if (d.style.includes("stirred")) {
prepDirections += "Combine ingredients in mixing glass and stir with ice until cold, then strain into glass.";
} else if (d.style.includes("bubbly")) {
prepDirections += "Stir all but bubbly with ice until cold. Strain and top with bubbly.";
} else if (d.style.includes("shaken")) {
prepDirections += "Combine ingredients in shaker and shake with ice until cold, then strain into glass.";
} else if (d.style.includes("fizz")) {
prepDirections += "Dry shake all ingredients (no ice) for 10 seconds. Then add ice and shake until cold. Strain into glass.";
} else if (d.style.includes("built")) {
prepDirections += "Build ingredients in serving glass, over ice. Stir and enjoy.";
} else if (d.style.includes("hot")) {
prepDirections += "Combine all ingredients and serve hot.";
}
//glass
prepDirections += ' Serve in ' + d.glass + ' glass.';
//
return prepDirections;
});
}
function clearRecipe() {
d3.select("#recipeTitle").text("Mouse over drink name to see the recipe")
d3.select("#recipeBox").selectAll('#recipeIng').remove();
d3.select("#recipeBox").selectAll('#recipeBody').remove();
}
</script>
<style>
.bigPlot {
visibility: visible;
background-color: rgba(200, 200, 200, 0.2);
}
*{font-family:"Trebuchet MS";}
</style>
</head>
<body onload="setTimeout(getData,500)">
<div>
<select id="drinkDropDown" style="font-size: 12pt;" onchange="drawPlot()">
</select>
</div>
<div class='thePlot'>
<svg class="bigPlot" id="theDrinks" width="340" height="500"></svg>
</div>
<div id="recipeBox">
<p id="recipeTitle"></p>
<p id="recipeIng"></p>
<p id="recipeBody"></p>
</div>
</body>
</html>