forked from jasonlong/benfords-law
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
131 lines (131 loc) · 4.36 KB
/
app.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
(function() {
var BENFORD_VALUES, MAX_CHART_WIDTH_PERCENTAGE, adjustFooter, drawChart, getDataset, getMultiplierForDataset, getOrdersOfMagnitudeBetween, initChart, observeDatasetOptions, placeBenfordMarkers, populateDatasetOptions, zeroChart;
BENFORD_VALUES = {
1: 30.1,
2: 17.6,
3: 12.5,
4: 9.7,
5: 7.9,
6: 6.7,
7: 5.8,
8: 5.1,
9: 4.6
};
MAX_CHART_WIDTH_PERCENTAGE = 81;
$(document).ready(function() {
initChart();
adjustFooter();
return populateDatasetOptions();
});
$(window).resize(function() {
return adjustFooter();
});
initChart = function() {
return $('ol#chart li').each(function(index) {
$('<span></span>').appendTo($(this));
$('<span class="digit">' + (index + 1) + '</span>').prependTo($(this));
return $('<b>▲</b>').hide().appendTo($(this));
});
};
placeBenfordMarkers = function(multiplier) {
return $('ol#chart li b').each(function(index) {
return $(this).css('left', BENFORD_VALUES[index + 1] * multiplier + '%').fadeIn('fast');
});
};
adjustFooter = function() {
if ($('section').css('float') === "none" && $('body').hasClass('single-column') === false) {
$('footer').appendTo('body').show();
return $('body').addClass('single-column');
} else if ($('section').css('float') !== "none" && $('body').hasClass('single-column')) {
$('footer').appendTo('aside');
return $('body').removeClass('single-column');
}
};
populateDatasetOptions = function() {
return $.getJSON('/js/datasets/index.json', function(data) {
var items;
items = [];
$.each(data, function(key, val) {
return items.push('<option value="' + key + '">' + val + '</option>');
});
$('#dataset-options').html(items.join(''));
getDataset($('#dataset-options option:first').val());
return observeDatasetOptions();
});
};
observeDatasetOptions = function() {
return $('#dataset-options').change(function() {
return zeroChart($(this).val());
});
};
zeroChart = function(nextDataset) {
$('table#stats td:nth-child(2)').fadeOut('fast');
$('#data-source').fadeOut('fast');
return $('ol#chart li').each(function(index) {
$(this).find('.fill').next('span').fadeOut('fast');
$(this).find('b').fadeOut('fast');
return $(this).find('.fill').animate({
width: 0
}, {
duration: 400,
complete: function() {
if (index === 8) {
return getDataset(nextDataset);
}
}
});
});
};
drawChart = function(data, multiplier) {
return $('ol#chart li').each(function(index) {
var value;
value = data.values[index + 1];
return $(this).find('.fill').animate({
width: value * multiplier + '%'
}, {
duration: 400,
complete: function() {
$(this).next('span').html(value + '%');
if (index === 8) {
$('ol#chart li .fill').next('span').fadeIn('fast');
$('table#stats td:nth-child(2)').fadeIn('fast');
$('#data-source').fadeIn('fast');
return placeBenfordMarkers(multiplier);
}
}
});
});
};
getDataset = function(name) {
return $.getJSON('/js/datasets/' + name + '.json', function(data) {
var description, multiplier;
description = $('#dataset-options option:selected').text();
if (description.length === 0) {
description = $('#dataset-options option:first').text();
}
$('#dataset-description').text(description);
multiplier = getMultiplierForDataset(data);
$('#num-records').text(data.num_records);
$('#min-value').text(data.min_value);
$('#max-value').text(data.max_value);
$('#orders-of-magnitude').text(getOrdersOfMagnitudeBetween(data.min_value, data.max_value));
$('#data-source').text(data.source).attr('href', data.source);
return drawChart(data, multiplier);
});
};
getMultiplierForDataset = function(dataset) {
var max;
max = 0;
$.each(dataset.values, function(key, val) {
if (val > max) {
return max = val;
}
});
return MAX_CHART_WIDTH_PERCENTAGE / max;
};
getOrdersOfMagnitudeBetween = function(min, max) {
min = parseInt(min.replace(/,/g, ""));
max = parseInt(max.replace(/,/g, ""));
return Math.floor(Math.LOG10E * Math.log(max - min));
};
}).call(this);