-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
678 lines (553 loc) · 18.3 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
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
width: 1300px;
height: 100px;
position: relative;
}
/* stylesheet for your custom graph */
.states {
fill: none;
stroke: #fff;
stroke-linejoin: round;
}
.states-choropleth {
fill: #ccc;
}
#tooltip-container {
position: absolute;
background-color: #fff;
color: #000;
padding: 10px;
border: 1px solid;
display: none;
}
.tooltip_key {
font-weight: bold;
}
.tooltip_value {
margin-left: 20px;
float: right;
}
.column {
float: left;
width: 50%;
}
/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}
.left_column{
padding-left: 50px;
}
.ExtraLegend{
padding-left:30px;
}
#error{
color:red;
}
#head{
padding-left:50px;
}
</style>
<div id='head'>
<h1>Funding, ethnicities and grades of student from the united states</h1>
</div>
<div class="row">
<div class="column">
<div id="tooltip-container"></div>
<div id="canvas-svg"></div>
<div id="my_dataviz"></div>
</div>
<div class="column">
<div class="left_column">
<div id="bargraph"></div>
<div class='ExtraLegend'>
<h4>The ethnicities:</h4>
<ul>
<li>AM (American Indian or Alaska Native)</li>
<li>AS (Asian)</li>
<li>HI (Hispanic/Latino)</li>
<li>BL (Black or African American)</li>
<li>WH (White)</li>
<li>HP (Hawaiian Native/Pacific Islander)</li>
<li>TR (Two or More Races)</li>
</ul>
<h6 id='error'>Incomplete data from: California and Oregon</h6>
<a href="https://www.kaggle.com/noriuk/us-education-datasets-unification-project">Source</a>, made by <a href="https://github.com/FlorisFok/">Floris Fok</a>
</div>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/topojson/1.1.0/topojson.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
function valueFormat(d) {
if (d > 1000000000) {
return Math.round(d / 1000000000 * 10) / 10 + "B";
} else if (d > 1000000) {
return Math.round(d / 1000000 * 10) / 10 + "M";
} else if (d > 1000) {
return Math.round(d / 1000 * 10) / 10 + "K";
} else {
return d;
}
}
d3.csv("states_data.csv", function(err, data) {
var config = {"color1":"#d3e5ff",
"color2":"#08306B",
"stateDataColumn":"states",
"valueDataColumn":"value",
"fed_rev":"fed_r",
"loc_rev":"local_r",
"state_rev":"sta_r",
"instruction_expenditure":"ins_ex",
"support_services_expenditure":"sup_ex",
"other_expenditure":"oth_ex",
"capital_outlay_expenditure":"cap_ex",
}
var WIDTH = 600, HEIGHT = 400;
var COLOR_COUNTS = 9;
var SCALE = 0.7;
function Interpolate(start, end, steps, count) {
var s = start,
e = end,
final = s + (((e - s) / steps) * count);
return Math.floor(final);
}
function Color(_r, _g, _b) {
var r, g, b;
var setColors = function(_r, _g, _b) {
r = _r;
g = _g;
b = _b;
};
setColors(_r, _g, _b);
this.getColors = function() {
var colors = {
r: r,
g: g,
b: b
};
return colors;
};
}
function hexToRgb(hex) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
var COLOR_FIRST = config.color1, COLOR_LAST = config.color2;
var rgb = hexToRgb(COLOR_FIRST);
var COLOR_START = new Color(rgb.r, rgb.g, rgb.b);
rgb = hexToRgb(COLOR_LAST);
var COLOR_END = new Color(rgb.r, rgb.g, rgb.b);
var MAP_STATE = config.stateDataColumn;
var MAP_VALUE = config.valueDataColumn;
var MAP_STAR = config.state_rev;
var MAP_LOCR = config.loc_rev;
var MAP_FEDR = config.fed_rev;
var MAP_INST = config.instruction_expenditure;
var MAP_SERV = config.support_services_expenditure;
var MAP_EXPE = config.other_expenditure;
var MAP_CAPI = config.capital_outlay_expenditure;
var width = WIDTH,
height = HEIGHT;
var valueById = d3.map();
var startColors = COLOR_START.getColors(),
endColors = COLOR_END.getColors();
var colors = [];
for (var i = 0; i < COLOR_COUNTS; i++) {
var r = Interpolate(startColors.r, endColors.r, COLOR_COUNTS, i);
var g = Interpolate(startColors.g, endColors.g, COLOR_COUNTS, i);
var b = Interpolate(startColors.b, endColors.b, COLOR_COUNTS, i);
colors.push(new Color(r, g, b));
}
var quantize = d3.scale.quantize()
.domain([0, 1.0])
.range(d3.range(COLOR_COUNTS).map(function(i) { return i }));
var path = d3.geo.path();
var svg = d3.select("#canvas-svg").append("svg")
.attr("width", width)
.attr("height", height);
d3.tsv("https://s3-us-west-2.amazonaws.com/vida-public/geo/us-state-names.tsv", function(error, names) {
name_id_map = {};
id_name_map = {};
id_pie_data = {};
id_pie_data2 = {};
all_eths = {};
scores = {}
for (var i = 0; i < names.length; i++) {
name_id_map[names[i].name] = names[i].id;
id_name_map[names[i].id] = names[i].name;
}
for (let i = 0; i < data.length; i++){
var id = name_id_map[data[i][MAP_STATE]];
id_pie_data[id] = [data[i][MAP_LOCR],
data[i][MAP_STAR],
data[i][MAP_FEDR]];
id_pie_data2[id] = [data[i][MAP_INST],
data[i][MAP_SERV],
data[i][MAP_CAPI]];
all_eths[id] = [{year:'AM', male:data[i]['maleAM'], female:data[i]['femaleAM']},
{year:'AS', male:data[i]['maleAS'], female:data[i]['femaleAS']},
{year:'HI', male:data[i]['maleHI'], female:data[i]['femaleHI']},
{year:'BL', male:data[i]['maleBL'], female:data[i]['femaleBL']},
{year:'WH', male:data[i]['maleWH'], female:data[i]['femaleWH']},
{year:'HP', male:data[i]['maleHP'], female:data[i]['femaleHP']},
{year:'TR', male:data[i]['maleTR'], female:data[i]['femaleTR']}];
scores[id] = [data[i]['read'],data[i]['math']];
}
data.forEach(function(d) {
var id = name_id_map[d[MAP_STATE]];
valueById.set(id, +d[MAP_VALUE]);
});
quantize.domain([d3.min(data, function(d){ return +d[MAP_VALUE] }),
d3.max(data, function(d){ return +d[MAP_VALUE] })]);
d3.json("https://s3-us-west-2.amazonaws.com/vida-public/geo/us.json", function(error, us) {
svg.append("g")
.attr("class", "states-choropleth")
.selectAll("path")
.data(topojson.feature(us, us.objects.states).features)
.enter().append("path")
.attr("transform", "scale(" + SCALE + ")")
.style("fill", function(d) {
if (valueById.get(d.id)) {
var i = quantize(valueById.get(d.id));
var color = colors[i].getColors();
return "rgb(" + color.r + "," + color.g +
"," + color.b + ")";
} else {
return "";
}
})
.attr("d", path)
.on("click", function(d) {
var html = "";
html += "<div class=\"tooltip_kv\">";
html += "<span class=\"tooltip_key\">";
html += id_name_map[d.id];
html += "</span>";
html += "<span class=\"tooltip_value\">";
html += `<p>Students: ${valueFormat(valueById.get(d.id))}</p>`;
html += `<p>Math Score: ${Math.round((scores[d.id][1]/500)*100)}%</p>`;
html += `<p>Read Score: ${Math.round((scores[d.id][0]/500)*100)}%</p>`;
html += "</span>";
html += "</div>";
update_circle1(id_pie_data[(d.id)])
update_circle2(id_pie_data2[(d.id)])
update_hist(all_eths[d.id])
if ($("#tooltip-container").is(":hidden")){
$("#tooltip-container").html(html);
$(this).attr("fill-opacity", "0.8");
$("#tooltip-container").show();
}
else{
$(this).attr("fill-opacity", "1.0");
$("#tooltip-container").hide();
}
var coordinates = d3.mouse(this);
var map_width = $('.states-choropleth')[0].getBoundingClientRect().width;
if (d3.event.layerX < map_width / 2) {
d3.select("#tooltip-container")
.style("top", (d3.event.layerY + 15) + "px")
.style("left", (d3.event.layerX + 15) + "px");
} else {
var tooltip_width = $("#tooltip-container").width();
d3.select("#tooltip-container")
.style("top", (d3.event.layerY + 15) + "px")
.style("left", (d3.event.layerX - tooltip_width - 30) + "px");
}
});
svg.append("path")
.datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; }))
.attr("class", "states")
.attr("transform", "scale(" + SCALE + ")")
.attr("d", path);
svg.append("text")
.attr("y", 0 )
.attr("x", width / 2)
.style("text-anchor", "middle")
.text("Number of students per state (Click me)")
.attr("class", "title")
.attr("dy", "1em");
});
});
});
var width = 600,
height = 300,
radius = Math.min(width/2, height) / 2,
padding = 50;
height += padding*2
var svg = d3.select("#my_dataviz").append("svg")
.attr("width", width)
.attr("height", height+padding)
.attr('id','circle1')
svg.append("text")
.attr("transform", "translate("+width/4+", 30 )")
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.attr("font-size", "20px" )
.attr('id', 'upd_rev')
.text('Revenue: 0');
svg.append("text")
.attr("transform", "translate("+(width/4)*3+", 30 )")
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.attr("font-size", "20px" )
.attr('id', 'upd_exp')
.text('Expenses: 0');
function update_circle1(data){
var width = 600,
height = 300,
radius = Math.min(width/2, height) / 2,
padding = 50;
height += padding*2;
var sum_of_array = parseInt(data[0])+parseInt(data[1])+parseInt(data[2])
document.querySelector('#upd_rev')
.innerHTML = `Revenue: $${valueFormat(sum_of_array-3)}`
var color = d3.scaleOrdinal()
.range(["#003f5c", "#bc5090", "#ffa600"]);
var arc = d3.arc()
.outerRadius(radius - 10)
.innerRadius(0);
var labelArc = d3.arc()
.outerRadius(radius - 40)
.innerRadius(radius - 40);
var pie = d3.pie()
.sort(null)
.value(function(d) { return d; });
var svg = d3.select('#circle1')
.append("g")
.attr("transform", "translate(" + width / 4 + "," + (height / 2) + ")");
var g = svg.selectAll(".arc")
.data(pie(data))
.enter().append("g")
.attr("class", "arc");
g.append("path")
.attr("d", arc)
.style("fill", function(d) { return color(d.data); });
if (data[0] == 1){
var name = d3.scaleOrdinal()
.range(['No data','No data','No data']);
}
else{
var name = d3.scaleOrdinal()
.range(['Local','State','Federal']);
}
g.append("text")
.attr("transform", function(d) { return "translate(" + labelArc.centroid(d) + ")"; })
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.attr("font-size", "12px" )
.text(function(d) { return name(d.data); });
};
function update_circle2(data){
var width = 600,
height = 300,
radius = Math.min(width/2, height) / 2,
padding = 50;
height += padding*2
var sum_of_array = parseInt(data[0])+parseInt(data[1])+parseInt(data[2])
document.querySelector('#upd_exp')
.innerHTML = `Expenses: $${valueFormat(sum_of_array-3)}`
var color = d3.scaleOrdinal()
.range(["#003f5c", "#bc5090", "#ffa600"]);
var arc = d3.arc()
.outerRadius(radius - 10)
.innerRadius(0);
var labelArc = d3.arc()
.outerRadius(radius - 40)
.innerRadius(radius - 40);
var pie = d3.pie()
.sort(null)
.value(function(d) { return d; });
var svg = d3.select('#circle1')
.append("g")
.attr("transform", "translate(" + (width / 4)*3 + "," + (height / 2) + ")");
var g = svg.selectAll(".arc")
.data(pie(data))
.enter().append("g")
.attr("class", "arc");
g.append("path")
.attr("d", arc)
.style("fill", function(d) { return color(d.data); });
if (data[0] == 1){
var name = d3.scaleOrdinal()
.range(['No data','No data','No data']);
}
else {
var name = d3.scaleOrdinal()
.range(['Instruction','Support','Capital','Other']);
}
g.append("text")
.attr("transform", function(d) { return "translate(" + labelArc.centroid(d) + ")"; })
// .attr("dy", ".35em")
.attr("text-anchor", "middle")
.attr("font-size", "12px" )
.text(function(d) { return name(d.data); });
};
var data = [1, 1, 1];
var title = 'No data';
update_circle1(data);
update_circle2(data);
var margin = {top: 20, right: 160, bottom: 35, left: 30};
var width = 600 - margin.left - margin.right,
height = 450 - margin.top - margin.bottom;
var svg = d3.select("#bargraph")
.append("svg")
.attr('id', 'hist1')
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// Set x, y and colors
var x = d3.scale.ordinal()
.domain(['AM','AS','HI','BL','WH','HP', 'TR'])
.rangeRoundBands([10, width-10], 0.02);
var y = d3.scale.linear()
.domain([0, 25])
.range([height, 0]);
var colors = ["blue", "pink"];
// Define and draw axes
var xAxis = d3.axisBottom().scale(x);
var yAxis = d3.axisLeft().scale(y);
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate("+ margin.left/2 +",0)")
.call(yAxis);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate("+margin.left+"," + height + ")")
.call(xAxis)
svg.append("text")
.attr("transform", "rotate(-90)")
.attr("y", -margin.left/2)
.attr("x",0 - (height / 2))
.style("text-anchor", "middle")
.attr("class", "changey")
.text("Number of student X 100.000");
svg.append("text")
.attr("transform",
"translate(" + (width/2) + " ," +
(height + margin.top + margin.bottom) + ")")
.style("text-anchor", "middle")
.attr("class", "changex")
.text("Ethnicity")
.attr('dy', '-1.5em');
svg.append("text")
.attr("y", 0 )
.attr("x", width / 2)
.style("text-anchor", "middle")
.text("Race and gender distribution")
.attr("class", "title");
// Draw legend
var legend = svg.selectAll(".legend")
.data(colors)
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(30," + i * 19 + ")"; });
legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", function(d, i) {return colors.slice().reverse()[i];});
legend.append("text")
.attr("x", width + 5)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "start")
.text(function(d, i) {
switch (i) {
case 0: return "Male";
case 1: return "Female";
}
});
// Prep the tooltip bits, initial display is hidden
var tooltip = svg.append("g")
.attr("class", "tooltip")
.style("display", "none");
tooltip.append("rect")
.attr("width", 30)
.attr("height", 20)
.attr("fill", "white")
.style("opacity", 0.5);
tooltip.append("text")
.attr("x", 15)
.attr("dy", "1.2em")
.style("text-anchor", "middle")
.attr("font-size", "12px")
.attr("font-weight", "bold");
function update_hist(data){
SCALE = 100000;
d3.selectAll("#remove_me").remove()
var margin = {top: 20, right: 160, bottom: 35, left: 30};
var width = 600 - margin.left - margin.right,
height = 450 - margin.top - margin.bottom;
var svg = d3.select("#hist1")
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");;
var x = d3.scale.ordinal()
.domain(['AM','AS','HI','BL','WH','HP', 'TR'])
.rangeRoundBands([10, width-10], 0.02);
var y = d3.scale.linear()
.domain([0, 25])
.range([height, 0]);
// Transpose the data into layers
var dataset = d3.layout.stack()(["male", "female","white_space"].map(function(gender) {
return data.map(function(d) {
return {x: d.year, y: +d[gender]/SCALE};
});
}));
// Create groups for each series, rects for each segment
var groups = svg.selectAll("g.cost")
.data(dataset)
.enter().append("g")
.attr("class", "cost")
.attr("id", 'remove_me')
.style("fill", function(d, i) { return colors[i]; });
var rect = groups.selectAll("rect")
.data(function(d) { return d; })
.enter()
.append("rect")
.attr("id", 'remove_me')
.attr("x", function(d) { return x(d.x); })
.attr("y", function(d) {if (d.y){return y(d.y0 + d.y)}; })
.attr("height", function(d) {if (d.y){return y(d.y0) - y(d.y0 + d.y)}; })
.attr("width", x.rangeBand())
.on("mouseover", function() { tooltip.style("display", null); })
.on("mouseout", function() { tooltip.style("display", "none"); })
.on("mousemove", function(d) {
var xPosition = d3.mouse(this)[0] - 15;
var yPosition = d3.mouse(this)[1] - 25;
tooltip.attr("transform", "translate(" + xPosition + "," + yPosition + ")");
tooltip.select("text").text(Math.round(d.y*SCALE))
.attr('dx','1em');
});
// Prep the tooltip bits, initial display is hidden
var tooltip = svg.append("g")
.attr("id", 'remove_me')
.attr("class", "tooltip")
.style("display", "none");
tooltip.append("rect")
.attr("width", 60)
.attr("height", 20)
.attr("fill", "white")
.style("opacity", 0.5);
tooltip.append("text")
.attr("x", 15)
.attr("dy", "1.2em")
.style("text-anchor", "middle")
.attr("font-size", "12px")
.attr("font-weight", "bold");
}
</script>