forked from rafaveguim/vis-passwords-time
-
Notifications
You must be signed in to change notification settings - Fork 0
/
century_ball.js
executable file
·220 lines (195 loc) · 6.93 KB
/
century_ball.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
/**
* Draws a radial representation for years.
* It consists of concentric 'orbits', where years, represented
* by small ellipses, lie over.
* Each orbit corresponds to a decade.
*/
function drawBall(){
var w = width('ball'), h = height('ball');
var years = yearsFreq(),
decades = decades_(d3.keys(years).sort());
var color = d3.scale.quantile()
.domain(d3.values(years))
.range(d3.range(9));
var svg = d3.select("#ball").append("svg")
.attr("width", w)
.attr("height", h)
.attr("class", "YlGnBu"); // refer to ColorBrewer CSS
// a base layer to capture 'outside' clicks
svg.append('rect')
.attr('class', 'invisible')
.attr('width' , w)
.attr('height', h)
.on('click', function(d,i){
selection.attr('display','none');
ringSelected.style('display', 'none');
clearDim();
drawAggregateCalendar();
drawWordleForYears(d3.keys(tree));
});
// everything is drawn in this shifted g
svg = svg.append("g")
.attr('transform','translate('+w/2+','+h/2+')');
// appending 'ring lanes'
svg.append("g")
.attr("class", "ring")
.selectAll("circle")
.data(d3.range(decades.length))
.enter().append("circle")
.attr("r", radius);
// an invisible 'base ring' to broaden the clicking area of rings
svg.select('g.ring')
.selectAll("circle.mask")
.data(d3.range(decades.length))
.enter().append("circle")
.attr('class', 'mask')
.style('stroke-width', 6)
.attr("r", radius)
.on('mouseover', function(d){
ringHover.style('display', null);
ringHover.attr('r', radius(d));
})
.on('mouseout', function(){
ringHover.style('display', 'none');
})
.on('click', function(d, i){
dimDecadesExcept(i);
ringSelected.style('display', null);
ringSelected.attr('r', radius(d));
selection.attr('display', 'none');
var years = d3.range(i*10+1900, i*10+1910);
drawAggregateCalendar(years);
drawWordleForYears(years);
});
var ringSelected = svg.select('g.ring')
.append('circle')
.attr('class', 'selection decade_selection'),
ringHover = svg.select('g.ring')
.append('circle')
.attr('class', 'hover decade_hover')
.style('pointer-events', 'none');
// appending 'internal labels' (1940s, 1990s..)
svg.append('g')
.attr('class', 'internal_label')
.selectAll('text')
.data(decades.map(function(a){return a[0];}))
.enter().append('text')
.text(function(d,i){return i<3 ? '' : d+'s';})
.attr('transform', function(d,i){return 'rotate(0)'
+ 'translate(0,3)'
+ 'translate(0,'+ radius(i) +') ';})
.attr('text-anchor', 'middle');
// 'external labels' (1, 2, 3... 10)
svg.append("g")
.selectAll('text')
.data(d3.range(10))
.enter().append('text')
.attr('class', 'external_label')
.attr("x", function(d,i){
return radius(decades.length)*Math.cos(angle(i));
})
.attr("y", function(d,i){
var y = radius(decades.length)*Math.sin(angle(i));
return y + d3.select(this).style('font-size').replace(/\D+/,'')/2;
})
.text(String)
.attr('cursor', 'hand')
.on('click', function(d){
var years = svg.selectAll('circle.year')
.classed('dimmed', true)
.classed('unclickable', true)
.filter(function(y){return (y.year+'')[3]==d})
.classed('dimmed', false)
.classed('unclickable', false)
.data()
.map(function(d){return +d.year});
drawAggregateCalendar(years);
ringSelected.style('display', 'none');
drawWordleForYears(years);
});
var hover = svg.append('circle')
.attr('class','year_hover')
.attr('display','none')
.attr('r',7);
// two-levels of data-binding here. decades -> svg:g; years -> svg:circle
svg.selectAll("g.decade")
.data(decades)
.enter()
.append("g")
.attr("class", "decade")
.selectAll("circle")
.data(function(a,i){ // cross decades with years
return a.map(function(d,j){
return {year:+d, decade:i, freq: years[+d]}
})
})
.enter()
.append("circle")
.attr("cx", function(d,i){ return radius(d.decade)*Math.cos(angle(i)); })
.attr("cy", function(d,i){ return radius(d.decade)*Math.sin(angle(i)); })
.attr("r", 4)
.attr("class", function(d) { return "q" + color(d.freq) + "-9"; })
.classed("year", true)
.on("click", function(d){
drawCalendar(d.year);
align(selection, this);
selection.style('fill', d3.select(this).style('fill'));
drawWordleForYears([d.year]);
})
.on("mouseover", function(){ align(hover, this); })
.on("mouseout", function(){ hover.attr('display','none') })
.append("title")
.text(function(d){return d.year+': '+d.freq;});
var selection = svg.append('circle')
.attr('class','selection year_selection')
.attr('display','none')
.attr('r',7);
}
function reloadBall(){
var years = yearsFreq();
var color = d3.scale.quantile()
.domain(d3.values(years))
.range(d3.range(9));
var data = d3.select("#ball")
.selectAll('circle.year')
.data();
data.forEach(function(d){d.freq = years[d.year]});
d3.select("#ball")
.selectAll('circle.year')
.attr("class", function(d) { return "q" + color(d.freq) + "-9"; });
}
/**
* Returns a map with the frequency of every year.
* Takes into consideration the filter stack.
*/
function yearsFreq(){
// if there's no filter, call the old, fast function
if (filterStack.length==0) return oldYearsFreq();
var years = {};
d3.keys(tree).forEach(function(k){
var pwds = d3.merge(d3.values(tree[k]));
pwds = filter(pwds, filterStack);
var sum = 0;
pwds.forEach(function(p){sum += +p.PWD_FREQUENCY});
years[k] = sum;
});
return years;
}
/**
* This function doesn't account for filters,
* so it's way faster. This is mainly because it
* doesn't count at password level, but at date level.
*/
function oldYearsFreq(){
var years = {};
d3.keys(tree).forEach(function(k){
years[k] = d3.sum(d3.values(tree[k]).map(function(u){
return +u[0].DATE_FREQUENCY;
}))
});
return years;
}
// computes radius for an orbit o
function radius(o){return o*17+17;};
//computes the angle for an index i [0..9] in radians
function angle(i) {return 36*i*Math.PI/180};