/
index.html
408 lines (407 loc) · 15.9 KB
/
index.html
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<!doctype HTML>
<html>
<head>
<title>Salary of Every BART Employee</title>
<meta name="viewport" content="width=800, user-scalable=0">
<script src="js/d3.js" charset="utf-8"></script>
<script src="js/fisheye.js"></script>
<script src="js/jquery.js"></script>
<style>
body{ margin: 0; font-family: helvetica; }
button, select { outline: none;}
.node { fill: black; }
#controls{
text-align: center;
font-size: 20px;
font-weight: bold;
}
#source{
position: absolute;
right: 10px;
bottom: 10px;
}
#title{
width: 100%;
}
.vis{
position: absolute;
left: 0px;
top: 0px;
z-index: -10;
}
#title h1{
text-align: center;
margin: 10px;
}
circle { stroke: none; }
.node.active circle {
stroke-width: 1;
stroke: white;
}
.node.highlighted circle {
stroke: black;
stroke-width: 15;
stroke-opacity: 0.5;
}
.tooltip{ display: none; }
.tooltip .bg{ opacity: 0.7; }
.tooltip text{
fill: white;
}
active.tooltip{
display: inherit;
}
.node text{
text-anchor: middle;
fill: black;
}
select{ font-size: 20px; }
.sort-by{
/* border: 1px solid #999;
background-color: green;
-webkit-appearance: none;
padding: 5px;
border-radius: 0px;*/
}
.share-left{ margin-top: 20px; position: absolute; left: 20px;}
.share-right{ margin-top: 20px; position: absolute; right: 10px;}
.fb-share-right{ margin-top: 50px; position: absolute; right: 35px;}
@media (max-width: 900px) {
h1, #controls{
float: left;
margin-left: 20px;
}
.share-left{
left: inherit;
right: 20px;
top: 60px;
}
}
@media(max-width: 400px){
.share-left, .share-right, .fb-share-right{
display: none;
}
h1{
display: none;
}
#controls{
font-size: 10px;
position: absolute;
top: inherit;
bottom: 5px;
}
#controls select{ width: 100px;}
#source{
display: none;
}
}
</style>
</head>
<body>
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=385171041519781";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<div id="title">
<div class="share-left">
<a href="https://twitter.com/vicapow" class="twitter-follow-button" data-show-count="false">Follow @vicapow</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
</div>
<div class="share-right">
<a href="https://twitter.com/share" class="twitter-share-button" data-via="vicapow" data-hashtags="hackthebart">Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
</div>
<div class="fb-share-right">
<div class="fb-like" data-href="http://blog.vctr.me/bart/" data-width="The pixel width of the plugin" data-height="The pixel height of the plugin" data-colorscheme="light" data-layout="button_count" data-action="recommend" data-show-faces="true" data-send="true"></div>
</div>
<h1>Salary of Every BART Employee</h1>
</div>
<div id="controls">
<p>color by:
<select class="color">
<option value="Union"> Union </option>
<option value="Title"> Title </option>
</select>
sort by:
<select class="sort-by">
<option value="TCOE">Total cost of employee</option>
<option value="Base">Base pay</option>
<option value="OT">Overtime pay</option>
<option value="Other">Other</option>
<option value="OT to Base">Overtime to base pay</option>
<option value="OT + Base + Other">Base + OT + Other</option>
<option value="MDV">Medical, dental, vision</option>
<option value="ER">Employer contributions to pension</option>
<option value="DC">401(k)</option>
<option value="Misc">Miscellaneous</option>
<option value="Title">Title</option>
<option value="Union">Union</option>
</select>
</p>
</div>
<div id="source">
<a href="https://github.com/enjalot/bart#data-links">source data</a> |
<a href="https://github.com/vicapow/bart-employees-in-d3">and code</a> |
created by: <a href="http://vctr.me"> vicapow </a>
</div>
<div id="fork">
</div>
<script>
var width = window.innerWidth, height = window.innerHeight
, vis = d3.select('body').append('svg').attr('class', 'vis')
.style({ width: width + 'px', height: height + 'px' })
, node, max, margin = 30, max_area = 300, tooltip
, margin_top = (width > 450) ? 120 : 20
, margin_bottom = 50, steps_x = 100, steps_y = 28
, color = d3.scale.category20()
, calcBestArea = function(){
var r1 = (width / steps_x) / 1.4
, r2 = (height / steps_y) / 1.4
, r = r1 < r2 ? r1 : r2
return Math.PI * r * r
}
, max_area = calcBestArea()
, areaScale = d3.scale.linear().range([0, max_area])
, areaToRadius = function(area){ return Math.sqrt(area / Math.PI) }
, fisheye = d3.fisheye.circular().radius(30).distortion(2)
, colorMetric = 'Union'
, radius = function(d){
var metric = sortMetric
if(metric === 'Title' || metric === 'Union') metric = 'TCOE'
return areaToRadius(areaScale(d[metric]))
}
, x = d3.scale.linear().domain([0, steps_x]).range([margin, width - margin])
, y = d3.scale.linear().domain([0, steps_y]).range([margin_top, height - margin_bottom])
, dataKey = function(d){ return d.id }
, sortBy = function(by){ return function(a, b){
if(a[by] === b[by]) return 0; else if(a[by] > b[by]) return -1;
return 1;
}}, money = d3.format('$,04d')
, id = 0, order = 0
, sortMetric = $('.sort-by').val()
, format = function(d){
// Name,Title,Base,OT,Other,MDV,ER,EE,DC,Misc,TCOE,,Average,
var numKeys = ['Base', 'OT', 'Other', 'MDV', 'ER', 'EE', 'DC', 'Misc', 'TCOE'];
numKeys.forEach(function(key){ d[key] = Number(d[key]) })
d.id = id++
d.order = order++
d['OT to Base'] = d.OT / d.Base
d['OT + Base + Other'] = d.OT + d.Base + d.Other
return d
}, topNode = null
if ( window.self !== window.top ){
// we're in an iframe! oh on! hide the twitter follow button
$('.share-left').hide()
}
d3.csv('bart-comp-all.csv', format, function(err, rows){
if(err) throw err
employees = rows
gotEmployees(employees)
fisheyeEffect(vis)
})
function updateAreaScale(sortMetric){
var metric = sortMetric
if(metric === 'Title' || metric === 'Union') metric = 'TCOE'
areaScale.domain([0, d3.max(employees, function(d){ return d[metric] })])
}
function gotEmployees(employees){
updateAreaScale(sortMetric)
node = vis.selectAll('.node').data(employees, dataKey)
.enter().append('g')
.attr('class', 'node')
.call(updateColor)
node.append('circle').attr('r', radius)
node.call(randomize).call(updatePos).call(sortNodesByMetric.bind(null, sortMetric))
node.on('click', function(){
var node // = d3.select('.node.highlighted').classed('highlighted', false).node()
, sel = d3.select(this)
if(sel.node() !== node) sel.classed('highlighted', !d3.select(this).classed('highlighted'))
})
vis.call(createTooltip)
}
function createTooltip(vis){
tooltip = vis.append('g').attr('class', 'tooltip')
tooltip.append('rect').attr({ width: 130, height: 140, rx: 5, ry: 5, class: 'bg' })
var desc = tooltip.append('g').attr('class', 'desc')
desc.append('text').attr('class', 'name').text('name: ').attr('transform', 'translate(5,15)')
desc.append('text').attr('class', 'title').text('title: ').attr('transform', 'translate(5,35)')
desc.append('text').attr('class','union').text('union: ').attr('transform', 'translate(5,55)')
desc.append('text').attr('class','main').text('total: ').attr('transform', 'translate(5,75)')
return tooltip
}
function posTooltip(node){
if(node.empty()) return
var d = node.datum(), text, x = d.fisheye.x, y = d.fisheye.y
if(sortMetric === 'OT to Base') text = 'OT to Base' + ': '
+ d3.format('%')(d[sortMetric], 2) + ', ' + 'OT: ' + money(d.OT)
+ ' Base: ' + money(d.Base)
else if(sortMetric === 'Title' || sortMetric === 'Union') text = d.Title
else text = sortMetric + ': ' + money(d[sortMetric])
if(text === d.Title || text === d.Union) text = 'Base: ' + money(d.Base) + ' OT: ' + money(d.OT)
tooltip.select('.main').text(text)
tooltip.select('.title').text(d.Title)
tooltip.select('.name').text(d.Name)
tooltip.select('.union').text('Union: ' + d.Union)
var box = tooltip.select('.desc').node().getBBox()
box.x -= 10, box.y -= 10, box.width += 20, box.height += 20
tooltip.select('rect').attr(box)
var offset = radius(d) * d.fisheye.z
if( x > width / 2 ) x -= box.width + offset; else x+= offset
if( y > height / 2 ) y -= box.height + offset; else y+= offset
tooltip.attr('transform', 'translate(' + x + ',' + y + ')')
}
function updatePos(node){
node.attr('transform', function(d){
return 'translate(' + d.x + ',' + d.y + ')'
})
updateAreaScale(sortMetric)
node.select('circle').attr('r', radius)
return node
}
function updateColor(node){
node.style('fill', function(d){ return color(d[colorMetric]) })
}
function setFisheyePos(node){
node.attr('transform', function(d, i){
return 'translate(' + d.fisheye.x + ', ' + d.fisheye.y + ')'
})
node.select('circle').attr('transform', function(d){
var z = d.fisheye && d.fisheye.z || 1
return 'scale(' + z + ')'
})
return node
}
function randomize(node){
node.each(function(d){
d.x = Math.random() * width
d.y = Math.random() * height
})
}
function grid(node){
return node.data().forEach(function(d){
d.x = x(d.order % x.domain()[1])
d.y = y(Math.floor(d.order / x.domain()[1]))
})
}
function sortNodesByMetric(metric, node){
var data = node.data().sort(sortBy(metric))
data.forEach(function(d, i){ d.order = i })
node.data(data, dataKey)
return node.call(grid).transition().duration(2000).call(updatePos)
}
function sortByTitle(node){
var nest = d3.nest()
.key(function(d){ return d.Title })
.sortKeys(d3.ascending)
.sortValues(d3.ascending)
.key(function(d){ return d.TCOE })
.entries(node.data())
var data = []
nest.forEach(function(leaf){
leaf.values.forEach(function(leaf){ data = data.concat(leaf.values) })
})
data.forEach(function(d, i){ d.order = i })
return node.data(data, dataKey).call(grid)
.transition().duration(2000).call(updatePos)
}
function sortByUnion(node){
var nest = d3.nest()
.key(function(d){ return d.Union })
.sortKeys(d3.ascending)
.sortValues(d3.ascending)
.key(function(d){ return d.TCOE })
.entries(node.data())
var data = []
nest.forEach(function(leaf){
leaf.values.forEach(function(leaf){ data = data.concat(leaf.values) })
})
data.forEach(function(d, i){ d.order = i })
return node.data(data, dataKey).call(grid)
.transition().duration(2000).call(updatePos)
}
// listen for sort button clicks
$('.sort-by').on('change', function(){
var newMetric = $(this).val()
if(sortMetric === newMetric) return
sortMetric = newMetric
if(sortMetric === 'Title') sortByTitle(node)
else if(sortMetric === 'Union') sortByUnion(node)
else sortNodesByMetric(sortMetric, node)
})
$('.color').on('change', function(){
var newMetric = $(this).val()
console.log("newMetric", newMetric)
if(newMetric === colorMetric) return
colorMetric = newMetric
node.transition().duration(2000).call(updateColor)
})
$(window).resize(function(){
width = window.innerWidth
height = window.innerHeight
if(width < 450) margin_top = 20
else margin_top = 120
vis.style({ width: width + 'px', height: height + 'px' })
x.range([margin, width - margin])
y.range([margin_top, height - margin_bottom])
node.call(grid).call(updatePos)
updateMaxArea()
node.select('circle').attr('r', radius)
})
function fisheyeEffect(vis){
return vis.on('mouseover', function(d){
if(!node) return
d3.select('.tooltip').style('display', 'inherit')
//node.each(function(d, i){ $(this).removeClass('animated') })
}).on("mousemove", function(d){
var m = d3.mouse(this)
fisheye.focus(m)
if(!node) return
node.each(function(d, i){
var prev_z = d.fisheye && d.fisheye.z || 1
d.fisheye = fisheye(d)
d.fisheye.prev_z = prev_z
})
.filter(function(d){ return d.fisheye.z !== d.fisheye.prev_z })
.sort(function(a, b){ return a.fisheye.z > b.fisheye.z ? 1 : -1 })
.call(setFisheyePos)
.call(function(node){
var max, maxNode
node.each(function(d){
if( !max || d.fisheye.z > max.fisheye.z) { max = d; maxNode = this }
})
if(topNode !== maxNode) updateTopNode(maxNode)
})
}).on('mouseleave', function(d){
d3.select('.tooltip').style('display', 'none')
node.each(function(d, i){ d.fisheye = {x: d.x, y: d.y, z: 1} })
.filter(function(d){ return d.fisheye.z !== d.fisheye.prev_z })
.call(setFisheyePos)
})
}
function updateMaxArea(){
max_area = calcBestArea()
areaScale.range([0, max_area])
}
function updateTopNode(maxNode){
if(topNode) topNode.classed('active', false)
topNode = d3.select(maxNode).classed('active', true)
topNode.call(posTooltip)
}
</script>
<!-- google analytics -->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-39250901-2']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</body>
</html>