-
Notifications
You must be signed in to change notification settings - Fork 0
/
nutrition.js
324 lines (262 loc) · 7.97 KB
/
nutrition.js
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
let nutritionSelector = document.getElementById("nutrition-selector");
let nutritionFile = 'nutrition_files/Dunkin Nutrition.csv'
function genVisuals(res) {
let charPatt = new RegExp('^((?![A-Za-z]).)*$')
let raw = res[0]
let columns = raw.columns
raw.forEach(d => {
for (var key in d) {
if (charPatt.test(d[key])) {
d[key] = +d[key]
}
}
});
// Group and get group names for display
let groupedItems = d3.group(raw, d => d['Table Type'])
let tableTypes = []
let groupArr = Array.from(groupedItems.entries())
groupArr.forEach(function (k, i) { tableTypes[i] = k[0]} )
tableTypes.splice(0, 0, 'All')
let dd2 = new Dropdown({
id: 'dd2',
val: 'Select an Item Type',
data: tableTypes,
cb: function(newval) {
genTable(raw, newval,
['Item', 'Serving Size', 'Fat',
'Carbs', 'Protein', 'Calories from Fat',
'Calories from Carbs', 'Calories from Protein'],
)
}
});
}
function filterData(newval, data, filterCol) {
console.log('You\'ve selected: ', newval)
if (newval === 'All') { return data }
else { return data.filter(d => d[filterCol] == newval)}
// return data.filter(d => d['Table Type'] == newval)
}
function genTable(data, itemTypeFilter, columns) {
tableTypeData = filterData(itemTypeFilter, data, 'Table Type')
let table = new Tabulator('#table-space', {
selectable: 1,
responsiveLayout: "hide",
layout: "fitDataFill",
data:tableTypeData,
// maxHeight: '100%',
// responsiveLayout:'collapse',
// pagination:pagination,
// paginationSize:10,
columns:[
{title:'Name', field:'Item',
cellClick: function(e, cell){
genChart(e, cell, tableTypeData)}
},
{title:'Serving Size', field:'Serving Size'},
{title:'Fat (g)', field:'Fat'},
{title:'Carbs (g)', field:'Carbs'},
{title:'Protein (g)', field:'Protein'}
]
})
};
function genChart(e, cell, inData) {
let colorVals = ['#22B9F1', '#F4A53C', '#B0F323']
d3.select('svg')
.remove()
let item = cell._cell.initialValue
// filter data to selected item
// need to edit filterData so you pull selected columns, too
let filtered = filterData(item, inData, 'Item')
let wSelCols = filtered.map((d) => {
return {
FatCals: d['Calories from Fat'],
CarbCals: d['Calories from Carbs'],
ProtCals: d['Calories from Protein']}
})
var width = 900
height = 450
margin = 40
// The radius of the pieplot is half the width or half the height (smallest one). I subtract a bit of margin.
var radius = Math.min(width, height) / 2 - margin
var svg = d3.select("#svg-house")
.append('svg')
// .attr("viewBox", `0 0 300 600`)
.attr("width", width)
.attr("height", height)
.append("g")
.attr('transform', `translate(${width/2}, ${height/2})`);
// set the color scale
var color = d3.scaleOrdinal()
.range(colorVals)
var pie = d3.pie()
.value(function(d) { return d[1]; })
var data_ready = pie(Object.entries(wSelCols[0]))
let arcGenerator = d3.arc()
.innerRadius(100) // This is the size of the donut hole
.outerRadius(radius)
svg
.selectAll('slices')
.data(data_ready)
.join('path')
.attr('d', arcGenerator)
.attr('fill', function(d){ return(color(d.data[0])) })
.attr("stroke", "black")
.style("stroke-width", "2px")
// .style("opacity", 0.7)
d3.select('#item-name')
.text(filtered[0]['Item'])
.style('padding-top', '2%')
.style('font-family', 'Titillium Web')
.style('font-size', '1.4em')
.style('font-weight', 'bolder')
svg.append("text")
.attr("text-anchor", "middle")
.attr('dy', '-1em')
.style('font-family', 'Helvetica')
.style('font-weight', '1000')
.style('font-size', '1.15em')
.text('Total Calories:')
svg.append('text')
.attr('text-anchor', 'middle')
.attr('dy', '1.1em')
.style('font-family', 'Helvetica')
.style('font-size', '1.15em')
.text(filtered[0]['calc_cals'])
svg
.selectAll('slices')
.data(data_ready)
.enter()
.append('text')
.style('font-family', 'Helvetica')
.text(function(d){
let data = d.data[1]
if (data != 0) return data
else return
})
.attr("transform", function(d) { return "translate(" + arcGenerator.centroid(d) + ")"; })
.style("text-anchor", "middle")
.style("font-size", 17)
// Add the path using this helper function
function appendCircs(colorValues, textValues) {
if (colorValues.length === textValues.length) {
for (i = 0; i < colorValues.length; i++) {
let widthOffset = 3.75;
let heightOffset = 2.5;
svg.append('circle')
.attr('cx', width / widthOffset)
.attr('cy', (-height / heightOffset) + (30 * i))
.attr('r', 12)
.attr('stroke', 'black')
.attr('fill', colorValues[i]);
svg.append('text')
.attr('x', (width / widthOffset) + 20)
.attr('y', (-height / heightOffset) + (30 * i) + 5)
.text(textValues[i])
.style('font-family', 'Helvetica')
}
}
else console.log('YOU MUST INSERT COLORS === LEN(TEXT)')
};
appendCircs(colorVals, ['Calories from Fat', 'Calories from Carbs', 'Calories from Protein'])
}
// step1
function Dropdown(o) {
this.options = o;
// step3
window.getdd = function(elem) {
var id = elem.closest('.dropdown').parentElement.id;
return window.dropdowns[id];
}
// step1 - show val
this.init = function() {
this.elem = document.getElementById(this.options.id);
//step1 + step2
var html =
`<div class='dropdown'>
<div class='dropdown_value'></div>
<div class='dropdown_arrow'>▾</div>
<div class='dropdown_panel'>
<div class='dropdown_items'></div>
</div>
</div>`;
//step4 - misc
var self = this;
document.addEventListener("mousedown", function() {
if (self.isVisible) self.hide();
});
//step4 - misc
if (!window.dropdowns) window.dropdowns = {};
window.dropdowns[this.options.id] = this;
//step4 - misc
this.elem.style.display = 'inline-block';
//step1
this.elem.innerHTML = html;
var elem = this.elem;
this.items = elem.querySelector(".dropdown_items");
this.value = elem.querySelector(".dropdown_value");
//step2
this.panel = elem.querySelector(".dropdown_panel");
this.arrow = elem.querySelector(".dropdown_arrow");
//step1
var self = this;
this.value.innerHTML = this.options.val;
//step2
var data = this.options.data;
var html = "";
data.forEach(function(elem) {
html += `<div class='dropdown_item' onmousedown='var self=getdd(this);self.clicked(this)'>${elem}</div>`;
});
this.items.innerHTML = html;
//step2
this.elem.addEventListener('mousedown', function() {
event.stopPropagation();
if (self.isVisible)
self.hide();
else
self.show();
});
}
// step3
this.clicked = function(elem) {
event.stopPropagation();
this.hide();
var newval = elem.innerHTML;
this.value.innerHTML = newval;
if (this.options.cb)
this.options.cb(newval);
}
// step2
this.show = function() {
for (var dd in window.dropdowns)
window.dropdowns[dd].hide();
this.isVisible = true;
this.items.style.transform = 'translate(0px,0px)';
this.arrow.style.transform = 'rotate(180deg)';
}
// step2b.
this.hide = function() {
if (!this.items) return;
this.isVisible = false;
this.items.style.transform = 'translate(0px,-255px)';
this.arrow.style.transform = 'rotate(0deg)';
}
this.init();
return this;
}
// step1 - just show val
var dd = new Dropdown({
id: 'dd1',
val: 'Select a Nutrition File',
// step2 show items
data: ['Dunkin Nutrition'],
cb: function(newval) {
nutritionFile = 'nutrition_files/' + newval + '.csv'
Promise.all([
d3.csv(nutritionFile)
])
.then(genVisuals)
.catch((err) => {
console.log(err)
})
}
});