-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwaffle_chart.html
More file actions
469 lines (369 loc) · 12.3 KB
/
waffle_chart.html
File metadata and controls
469 lines (369 loc) · 12.3 KB
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<div id=#titleDiv>
<h1>Explore Nobel Prize Category</h1>
</div>
<head>
<!-- <meta charset="utf-8">
<div id=#titleDiv>
<h1>Explore Nobel Prize Category</h1>
</div> -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="//d3js.org/topojson.v1.min.js"></script>
<style type="text/css">
div.tooltip {
position: absolute;
text-align: left;
min-width: 200px;
max-width: 400px;
min-height: 50px;
max-height: 100px;
padding: 5px;
font: 14px sans-serif;
background: white;
border: rgba(0,0,0,0.3) solid 1px;
pointer-events: none;
opacity: 0.9;
}
</style>
</head>
<body>
<!-- <div class="container"> -->
<div class='btn-toolbar pull-left'>
<div class="btn-group">
<button type="button" class="btn btn-default">Menu</button>
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu dropdown-menu-left" role="menu">
<li><a href="/index.html"><i class="fa fa-home"></i></a></li>
<li><a href="/age.html">Analysis by Age</a></li>
<li><a href="/map.html">Analysis by Country</a></li>
</ul>
</div>
</div>
<style>
.btn-group {
margin-right: 0px;
}
</style>
<!-- <div class="container">
</div>
-->
<style>
.container{
margin-top: 15px;
margin-right: 100px;
margin-left: 80px;
}
</style>
<div class="container">
<div id=#titleDiv>
<input name="genderButton"
type="button"
value="Color by Gender"
onclick="ColorbyGender()" />
<input name="bcountryButton"
type="button"
value="Color by Birth Country"
onclick="ColorbybCountry()" />
<input name="dcountryButton"
type="button"
value="Color by Death Country"
onclick="ColorbydCountry()" />
<input name="ageGroupButton"
type="button"
value="Color by Age"
onclick="ColorbyAge()" />
</div>
</div>
<style>
.container1{
margin-top: 0px;
margin-right: 100px;
margin-left: 1000px;
}
</style>
<div class="container1">
<div id="drop" align=center></div>
<div id="buttonContainer">
<button id="Chronological">Chronological Sort</button>
<button id="Grouped">Group Sort</button>
</div>
</div>
<script>
var w = 1600
var h = 1200
var numCols = 5;
var margin = {
right: 40,
left: 100,
top: 70,
bottom: 40
}
var color_ref = ['40-', '40-50', '50-60', '60-70', '70+', //Age
'United States of America', 'United Kingdom', 'Germany', 'Alive', // Countries
'Male', 'Female', "", // Gender
'Other'] // Catching Remaining Values as 'Other'
var color_list = ["#a6bddb", "#74a9cf", "#3690c0", "#0570b0", "#034e7b", // Age
"#ED7D31", "#00247D", "#FFCE00", "#008000", // Countries
"#ED7D31", "#4472C4", "#cccccc", // Gender
"#cccccc"] // Catching Remaining Values as 'Other'
var colormap = d3.scaleOrdinal()
.domain(color_ref)
.range(color_list);
var width = w - margin.right - margin.left;
var height = h - margin.top - margin.bottom;
var div = d3.select("body")
.append("div")
.attr("class", "tooltip")
.style("opacity", 0)
function ColorbyGender(){
d3.csv("nobel_1pp.csv", pickGender)
function pickGender(error, data){
if (error) return console.warn(error);
data.forEach(function(d){
d.Year = +d.Year;
d.Agg = d.Sex
})
var color_ref_local = ['Male', 'Female', 'Other'] // Catching Remaining Values as 'Other'
var color_list_local = [ "#ED7D31", "#4472C4", "#cccccc"]
ready(data,color_ref_local,color_list_local)
}
}
function ColorbybCountry(){
d3.csv("nobel_1pp.csv", pickbCountry)
function pickbCountry(error, data){
if (error) return console.warn(error);
data.forEach(function(d){
d.Year = +d.Year;
d.Agg = "Other"
if(isInArray(d['Birth Country'], color_ref)){
d.Agg = d['Birth Country']
}else{
d.Agg = "Other"
}
})
var color_ref_local = ['United States of America', 'United Kingdom', 'Germany',
'Other'] // Catching Remaining Values as 'Other'
var color_list_local = ["#ED7D31", "#00247D", "#FFCE00",
"#cccccc"]
ready(data, color_ref_local, color_list_local)
}
}
function ColorbydCountry(){
d3.csv("nobel_1pp.csv", pickdCountry)
function pickdCountry(error, data){
if (error) return console.warn(error);
data.forEach(function(d){
d.Year = +d.Year;
d.Agg = "Other"
if(isInArray(d['Death Country'], color_ref)){
d.Agg = d['Death Country']
}else{
d.Agg = "Other"
}
})
var color_ref_local = ['United States of America', 'United Kingdom', 'Germany', 'Alive',
'Other'] // Catching Remaining Values as 'Other'
var color_list_local = ["#ED7D31", "#00247D", "#FFCE00", "#008000",
"#cccccc"]
ready(data, color_ref_local, color_list_local)
}
}
function ColorbyAge(){
d3.csv("nobel_1pp.csv", pickAge)
function pickAge(error, data){
if (error) return console.warn(error);
data.forEach(function(d){
d.Year = +d.Year;
d.Agg = "Other"
if(isInArray(d['Age Group'], color_ref)){
d.Agg = d['Age Group']
}else{
d.Agg = "Other"
}
})
var color_ref_local = ['40-', '40-50', '50-60', '60-70', '70+',
'Other'] // Catching Remaining Values as 'Other'
var color_list_local = ["#a6bddb", "#74a9cf", "#3690c0", "#0570b0", "#034e7b",
"#cccccc"]
ready(data, color_ref_local, color_list_local)
}
}
function ready(data, color_ref_local, color_list_local){
plotall(data,color_ref_local,color_list_local) // Default selection
d3.select("#Chronological")
.on("click", function() {
var chrodata = data.sort(function(a, b) {
return d3.ascending(a.Year, b.Year)
})
plotall(chrodata,color_ref_local,color_list_local)
})
d3.select("#Grouped")
.on("click", function() {
var grpdata = data.sort(function(a, b) {
return d3.ascending(a.Agg, b.Agg)
})
plotall(grpdata,color_ref_local,color_list_local)
});
}
function isInArray(value, array) {
return array.indexOf(value) > -1;
}
function plotall(data, color_ref_local, color_list_local){
d3.select("svg").remove()
var svg = d3.select("body")
.append("svg")
.attr("id", "chart")
.attr("width", w)
.attr("height", h)
.append("g")
.attr("transform", "translate(0" + margin.left + "," + margin.top + ")");
var category = d3.nest()
.key(function(d){ return d.Category })
.entries(data)
svg.append("text")
.attr("x", 0)
.attr("y", -30)
.text("Chemistry")
.style("font-weight", "bold")
.style("font-size", 20)
.style("font-family", "Helvetica")
svg.append("text")
.attr("x", 210)
.attr("y", -30)
.text("Physics")
.style("font-weight", "bold")
.style("font-size", 20)
.style("font-family", "Helvetica")
svg.append("text")
.attr("x", 410)
.attr("y", -30)
.text("Medicine")
.style("font-weight", "bold")
.style("font-size", 20)
.style("font-family", "Helvetica")
svg.append("text")
.attr("x", 610)
.attr("y", -30)
.text("Literature")
.style("font-weight", "bold")
.style("font-size", 20)
.style("font-family", "Helvetica")
svg.append("text")
.attr("x", 810)
.attr("y", -30)
.text("Economics")
.style("font-weight", "bold")
.style("font-size", 20)
.style("font-family", "Helvetica")
svg.append("text")
.attr("x", 1010)
.attr("y", -30)
.text("Peace")
.style("font-weight", "bold")
.style("font-size", 20)
.style("font-family", "Helvetica")
var plots = svg.selectAll("g")
.data(category)
.enter()
.append("g")
.attr("transform", function(d, i){
if(d.key == "Medicine"){
return "translate(400," + ((i * 0)) + ")"
}
if(d.key == "Literature"){
return "translate(600," + ((i * 0)) + ")"
}
if(d.key == "Physics"){
return "translate(200," + ((i * 0)) + ")"
}
if(d.key == "Economics"){
return "translate(800," + ((i * 0)) + ")"
}
if(d.key == "Peace"){
return "translate(1000," + ((i * 0)) + ")"
}
else {
"translate(0," + ((i * 150)) + ")"
}
})
//
plots.selectAll(".rect")
.data(function(d){ return d.values})
.enter()
.append("rect")
.attr("width", 22)
.attr("height", 22)
.attr("x", function(d, i){
var colIndex = i % numCols
return colIndex * 24
})
.attr("y", function(d, i){
var rowIndex = Math.floor(i/numCols)
return rowIndex * 24
})
.attr("r", 6)
//.style("fill", "#e2c642")
.style("fill", function(d) { return colormap(d.Agg) })
.style("stroke", "none")
.on("mouseover", function(d){
div.transition()
.duration(100)
.style("opacity", 1)
var element = d3.select(this)
element.style("fill", "Black")
div.html("<span style = 'font-weight: bold'>" + d["Full Name"] + " (" + d.Year + ")" + "</span>" + "<br>" + "<span style = 'font-style: italic'>" + d["Motivation"] + "</span>")
.style("font-family", "Helvetica")
div.style("visibility", "visible")
.style("left", (d3.event.pageX - 20) + "px")
.style("top", (d3.event.pageY - 35) + "px")
})
.on("mousemove", function(d){
div.style("left", (d3.event.pageX - 20) + "px")
.style("top", (d3.event.pageY - 65) + "px")
})
.on("mouseout", function(d){
div.transition()
.duration(500)
div.style("visibility", "hidden")
var element = d3.select(this)
//element.style("fill", "#e2c642")
element.style("fill", function(d) { return colormap(d.Agg) })
})
//legend
var legendRectSize = 18;
var legendSpacing = 4;
var legend = svg.selectAll(".legend")
.data(color_list_local).enter()
.append('g')
.attr("class", "legend")
.attr("height", 100)
.attr("width", 100)
.attr('transform', function(d, i) {
var height = legendRectSize + legendSpacing;
var offset = height * color_list_local.length / 2;
var horz = 25 * legendRectSize + 720;
var vert = i * height - offset+50;
return 'translate(' + horz + ',' + vert + ')';
});
legend.append('rect')
.attr('width', legendRectSize)
.attr('height', legendRectSize)
.style('fill', function(d,i){ return color_list_local[i]; })
.style('stroke', color_list_local);
legend.append('text')
.attr('x', legendRectSize + legendSpacing)
.attr('y', legendRectSize - legendSpacing)
.text(function(d,i) { return color_ref_local[i] ; })
.style('font-size','12px');
}
</script>
</body>
</html>