summaryrefslogtreecommitdiff
path: root/result/krafcik.js
blob: cdb0b9e7cc1c185b39cf7799bf79231bd7672695 (plain)
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
$(document).ready(function() {

    $.when(
        $.getJSON('beers.json'),
        $.getJSON('breweries.json'),
        $.getJSON('styles.json')
    ).done(function(beerData, breweryData, styleData) {
        var beers = beerData[0];
        $.each(beers, function(idx, beer) {
            beer['scatter-data'] = [
                {
                    x: beer.abv,
                    y: beer.ratings
                },
                {
                    x: beer.abv,
                    y: beer.average
                },
                {
                    x: beer.ratings,
                    y: beer.average
                }
            ];
        });
        var minYear = Math.min.apply(null, beers.map(b => b.year));
        var maxYear = Math.max.apply(null, beers.map(b => b.year));
        var minMonth = Math.min.apply(null, beers.filter(b => (b.year == minYear)).map(b => b.month));
        var maxMonth = Math.min.apply(null, beers.filter(b => (b.year == maxYear)).map(b => b.month));
        var fullYears = maxYear - minYear - 1;
        var dateCount = fullYears * 12 + (12 - minMonth + 1) + maxMonth;
        var dateMapping = [];
        var year = minYear;
        var month = minMonth;
        for (var c = 0; c < dateCount; c++) {
            dateMapping[c] = year + '-' + ('0' + month).slice(-2);
            month++;
            if (month > 12) {
                year++;
                month = 1;
            }
        }
        var valSpan = [0, dateCount-1];
        var currRange = 12-minMonth+1;
        var lastRange = 'odd';
        var yearRanges = [{'start': 0, 'end': currRange-1, 'class': lastRange}];
        while (currRange < dateCount) {
            lastRange = lastRange == 'odd' ? 'even' : 'odd';
            yearRanges.push({
                'start': currRange,
                'end': Math.min(currRange+11, dateCount-1),
                'class': lastRange
            });
            currRange += 12;
        }
        $('#date-filter input[name="date"]').slider({
            min: 0,
            max: dateCount-1,
            value: valSpan,
            ticks: valSpan,
            ticks_labels: valSpan.map(v => dateMapping[v]),
            rangeHighlights: yearRanges,
            formatter: function(val) {
                if (isNaN(val)) {
                    return val.map(v => dateMapping[v]).join(' - ');
                }
                else {
                    return dateMapping[val];
                }
            }
        });

        var breweries = breweryData[0];
        var breweryTemplate = $('#brewery-template').html();
        var breweryFilter = $('#brewery-filter');
        $.each(breweries, function(brewery, data) {
            var fieldId = 'brewery-' + brewery;
            var field = $(breweryTemplate);
            field.find('input').attr('value', brewery).attr('id', fieldId);
            field.find('label').attr('for', fieldId).attr('title', data['name']).find('img').attr('src', 'img/' + data['logo']);
            breweryFilter.append(field);
            field.find('label').tooltip();
        });
        $('#brewery-all').change(function() {
            if ($(this).is(':checked')) {
                $('#brewery-filter .form-check input').prop('checked', 'checked');
            } else {
                $('#brewery-filter .form-check input').prop('checked', null);
            }
        });

        var styles = styleData[0];
        $.each(styles, function(idx, style) {
            style.styleTree = {};
            $.each(style.styles.map(s => s.split(' - ')), function(idx, splitStyle) {
                if (splitStyle.length > 1) {
                    if (!style.styleTree[splitStyle[0]]) {
                        style.styleTree[splitStyle[0]] = [];
                    }
                    style.styleTree[splitStyle[0]].push(splitStyle[1]);
                } else {
                    style.styleTree[splitStyle[0]] = null;
                }
            });
        });
        var styleNames = styles.map(s => s.name);
        var styleCounts = styles.map(s => 0);
        var substyleCounts = [];
        var styleSubstyles = styles.map(s => s.styles);
        var styleColours = styles.map(s => s.colour);
        var scatterChartData = [];
        for (var i = 0; i < 3; i++) {
            scatterChartData[i] = {
                datasets: styles.map(function(style) {
                    return {
                        borderColor: style.colour,
                        backgroundColor: Color(style.colour).alpha(0.5).rgbString(),
                        data: []
                    };
                })
            };
        }
        var stylesChart;
        $('a#styles-tab').on('shown.bs.tab', function() {
            if (!stylesChart) {
                var ctx = $('#styles-chart')[0].getContext('2d');
                stylesChart = new Chart(ctx, {
                    type: 'bar',
                    data: {
                        labels: styleNames,
                        datasets: [{
                            data: styleCounts,
                            backgroundColor: styleColours,
                            borderColor: '#000000',
                            borderWidth: 1
                        }]
                    },
                    options: {
                        legend: {
                            display: false
                        },
                        scales: {
                            yAxes: [{
                                ticks: {
                                    beginAtZero: true
                                }
                            }]
                        },
                        tooltips: {
                            enabled: false,
                            custom: function(tooltip) {
                                var container = $('#chart-tooltip');
                                if (!container.length) {
                                    container = $('<div id="chart-tooltip"></div>');
                                    $('body').append(container);
                                }
                                if (tooltip.body) {
                                    var content = $('<ul>');
                                    $.each(styles[tooltip.dataPoints[0].index].styleTree, function(style, substyles) {
                                        var getItem = function(style, substyle) {
                                            var count = substyleCounts[substyle ? (style + ' - ' + substyle) : style];
                                            if (count) {
                                                var term = $('<dt>');
                                                var value = $('<dd>');
                                                var item = $('<li>').append($('<dl>').append(term).append(value));
                                                term.html(substyle || style);
                                                value.html(count);
                                                return item;
                                            }
                                            return null;
                                        };
                                        if (substyles) {
                                            var sublist = $('<ul>');
                                            var nonZero = false;
                                            $.each(substyles, function(id, substyle) {
                                                var item = getItem(style, substyle);
                                                if (item) {
                                                    sublist.append(item);
                                                    nonZero = true;
                                                }
                                            });
                                            if (nonZero) {
                                                content.append($('<li>').append(style).append(sublist));
                                            }
                                        } else {
                                            content.append(getItem(style));
                                        }
                                    });
                                    var positionY = this._chart.canvas.offsetTop;
			                        var positionX = this._chart.canvas.offsetLeft;
                                    container.html(content).show();
                                    container.css({
                                        'top': Math.min(
                                            positionY + tooltip.caretY,
                                            $('body').height() - container.outerHeight()
                                        ),
                                        'left': Math.min(
                                            positionX + tooltip.caretX,
                                            $('body').width() - container.outerWidth()
                                        )
                                    }).show();
                                } else {
                                    container.html('').hide();
                                }
                            }
                        }
                    }
                });
            }
        });
        var ratingAxis = {
			type: 'logarithmic',
            ticks: {
                min: 15,
                max: Math.ceil(Math.max.apply(null, beers.map(b => b.ratings)) / 1000) * 1000,
                callback: value => value.toLocaleString()
            }
        };
        var averageAxis = {
            ticks: {
                min: Math.floor(Math.min.apply(null, beers.map(b => b.average)) * 2) * 0.5,
                max: 5
            }
        };
        var abvAxis = {
            ticks: {
                beginAtZero: true,
                min: 0,
                max: Math.ceil(Math.max.apply(null, beers.map(b => b.abv)) * 2) * 0.5
            }
        };
        var abvRatingsChart;
        $('a#abv-ratings-tab').on('shown.bs.tab', function() {
            if (!abvRatingsChart) {
                var ctx = $('#abv-ratings-chart')[0].getContext('2d');
                abvRatingsChart = new Chart.Scatter(ctx, {
                    data: scatterChartData[0],
                    options: {
                        scales: {
                            xAxes: [abvAxis],
					        yAxes: [ratingAxis]
                        }
                    }

                });
            }
        });
        var abvAverageChart;
        $('a#abv-average-tab').on('shown.bs.tab', function() {
            if (!abvAverageChart) {
                var ctx = $('#abv-average-chart')[0].getContext('2d');
                abvAverageChart = new Chart.Scatter(ctx, {
                    data: scatterChartData[1],
                    options: {
                        scales: {
                            xAxis: [abvAxis],
                            yAxes: [averageAxis]
                        }
                    }
                });
            }
        });
        var ratingsAverageChart;
        $('a#ratings-average-tab').on('shown.bs.tab', function() {
            if (!ratingsAverageChart) {
                var ctx = $('#ratings-average-chart')[0].getContext('2d');
                ratingsAverageChart = new Chart.Scatter(ctx, {
                    data: scatterChartData[2],
                    options: {
                        scales: {
					        xAxes: [ratingAxis],
                            yAxes: [averageAxis]
                        }
                    }
                });
            }
        });

        var getFilteredBeers = function(beers) {
            var dates = $('#date-filter input[name="date"]').val().split(',').map(d => dateMapping[parseInt(d)]);
            var breweries = $('#brewery-filter input:checked').map(function(i, input) { return input.value; }).toArray();
            return beers.filter(function(beer) {
                var added = beer.year + '-' + ('0' + beer.month).slice(-2);
                return (breweries.indexOf(beer.brewery) > -1)
                    && (added >= dates[0] && added <= dates[1]);
            });
        };

        var updateCharts = function() {
            var filtered = getFilteredBeers(beers);
            var bySubstyle = {};
            for (var style in substyleCounts) {
                substyleCounts[style] = 0;
            }
            for (var beer in filtered) {
                var style = filtered[beer].style.join(' - ');
                if (!substyleCounts[style]) {
                    substyleCounts[style] = 0;
                }
                substyleCounts[style]++;
                if (!bySubstyle[style]) {
                    bySubstyle[style] = [];
                }
                bySubstyle[style].push(filtered[beer]['scatter-data']);
            }
            for (var s in styleCounts) {
                styleCounts[s] = 0;
                for (var i = 0; i < 3; i++) {
                    scatterChartData[i].datasets[s].data = [];
                }
                for (var ss in styleSubstyles[s]) {
                    var substyle = styleSubstyles[s][ss];
                    if (substyleCounts[substyle]) {
                        styleCounts[s] += substyleCounts[substyle];
                        for (var i = 0; i < 3; i++) {
                            scatterChartData[i].datasets[s].data = scatterChartData[i].datasets[s].data.concat(bySubstyle[substyle].map(d => d[i]));
                        }
                    }
                }
            }
            if (stylesChart) {
                stylesChart.update();
            }
            if (abvRatingsChart) {
                abvRatingsChart.update();
            }
            if (abvAverageChart) {
                abvAverageChart.update();
            }
            if (ratingsAverageChart) {
                ratingsAverageChart.update();
            }
        };

        var changeTimeout = null;
        $('#date-filter, #brewery-filter').find('input').change(function() {
            clearTimeout(changeTimeout);
            changeTimeout = setTimeout(updateCharts, 500);
        });

        $('a#styles-tab').trigger('shown.bs.tab');
        updateCharts();
    });

    $('#tab-menu a').on('click', function (e) {
        e.preventDefault()
        $(this).tab('show')
    })
});