-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
66 lines (57 loc) · 1.45 KB
/
main.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
function get_random_color() {
var letters = '0123456789ABCDEF'.split('');
var color = '#';
for (var i = 0; i < 6; i++ ) {
color += letters[Math.round(Math.random() * 15)];
}
return color;
}
function extract_attribute(data,attr){
/*
Returns the array of a particular attribute of a JSON array
*/
res = []
for(var i = 0;i<data.length;i++){
res.push(data[i][attr]);
}
return res;
}
function filter_data(data,fn){
/*
Returns the array of JSON objects that passes through a particular filter
*/
res = []
for(var i =0;i<data.length;i++){
if(fn(data[i])){
res.push(data[i]);
}
}
return res;
}
function count_categories(data,attr){
var counts = {};
for(var i = 0; i< data.length; i++) {
var elem = data[i][attr];
counts[elem] = counts[elem] ? counts[elem]+1 : 1;
}
var res = [];
for(var key in counts){
res.push({"label":key,"value":counts[key]});
}
return res;
}
function shuffle(array) {
var counter = array.length, temp, index;
// While there are elements in the array
while (counter > 0) {
// Pick a random index
index = Math.floor(Math.random() * counter);
// Decrease counter by 1
counter--;
// And swap the last element with it
temp = array[counter];
array[counter] = array[index];
array[index] = temp;
}
return array;
}