-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex_final.html
538 lines (515 loc) · 19.6 KB
/
index_final.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet">
<script src="https://d3js.org/d3.v4.min.js"></script>
<title>How well can math score be predicted by social-economic status in different countries? --Education and Equity</title>
<style>
body {
font-family: 'Nunito', sans-serif;
}
h2, h4 {
text-align: center;
}
main {
display: block;
position: relative;
width: 100%;
}
#grid {
position: absolute;
width: 260px;
left: 0;
top: 300px;
}
div.left {
position: absolute;
display: block;
left: 0;
width: calc(9 / 16 * 100vw);
}
#map_container svg {
border: 1px solid black;
}
div.right {
position: absolute;
left: calc(9.5 / 16 * 100vw);
display: block;
}
#barchart_container:after {
clear: both;
}
#barchart_container .select_buttons button {
border: 1px solid gray;
background-color: #cccccc;
padding: 5px 10px;
border-radius: 4px;
margin-bottom: 5px;
}
.tooltip {
position: absolute;
text-align: left;
width: 250px;
height: 100px;
padding: 2px;
font-size: 14px;
background: #ffffff;
border: 1px solid gray;
border-radius: 8px;
pointer-events: none;
opacity: 0.4;
}
.line {
fill: none;
stroke: black;
stroke-width: 5;
}
.axis text {
font-size: 12px;
}
circle {
opacity: 0.9;
}
</style>
</head>
<body>
<h2>How well can math score be predicted by social-economic status in different countries?</h2>
<h4>-- Using PISA 2012 to Explore Education and Equity</h4>
<main>
<div class="left">
<div id="grid"></div>
<div id="map_container"></div>
<div id="conclusion"></div>
<div id="scatter_container"></div>
</div>
<div class="right">
<div id="barchart_container"></div>
</div>
</main>
<script>
//reference: https://stackoverflow.com/questions/16265123/resize-svg-when-window-is-resized-in-d3-js
var w = window,
d = document,
e = d.documentElement,
g = d.getElementsByTagName('body')[0],
window_x = w.innerWidth || e.clientWidth || g.clientWidth,
window_y = w.innerHeight|| e.clientHeight|| g.clientHeight;
// var color_array = [["#9ad6b1","#8cd480", "#3c9a55"],
// ["#bed0f4", "#8ba6e9", "#0f61a0"],
// // ["#fba165", "#f65c40","#bd2828"]];
const color_array = [["rgb(233, 0, 90)", "rgb(161, 0, 89)", "rgb(0, 0, 89)"],
// this first line is for high math performance
["rgb(236, 165, 190)", "rgb(160, 165, 190)", "rgb(0, 166, 189)"],
["rgb(223, 223, 223)", "rgb(161, 228, 223)", "rgb(0, 219, 188)"]]//the last line is for low math performance
var generate_grid_data = function() {
var data = new Array();
var xpos = 40,
ypos = 30,
width = 40,
height = 40,
n = 3;
for(var i = 0; i < n; i++) {
data[i] = new Array();
for(var j = 0; j < n; j++) {
data[i][j] = {
x: xpos,
y: ypos,
width: width,
height: height,
color: color_array[i][j]
};
xpos += width;
}
xpos = 40;
ypos += height;
}
return data;
};
var draw_color_matrix = function() {
var position_data = generate_grid_data();
var width = 200;
var grid = d3.select("#grid")
.append("svg")
.attr("width", width)
.attr("height", width);
var row = grid.selectAll('.row')
.data(position_data)
.enter().append("g")
.attr("class", "row");
var column = row.selectAll('.cell')
.data(function(d) { return d;})
.enter().append('rect')
.attr("class", "cell")
.attr("x", function(d) { return d.x;})
.attr("y", function(d) { return d.y; })
.attr("width", function(d) { return d.width;})
.attr("height", function(d){ return d.height;})
.style("fill", function(d) { return d.color;});
var marker = grid.append("defs").append("marker")
.attr("id", "arrow")
.attr("refX", 2)
.attr("refY", 6)
.attr("markerWidth", 13)
.attr("markerHeight", 13)
.attr("orient", "auto")
.append("path")
.attr("d", "M2,2 L2,11 L10,6 L2,2")
.style("stroke", "black");
// this explanation is for math performance
grid.append("text")
.attr("x", -180)
.attr('y', 20)
.text("higher math score")
.attr("transform", "rotate(-90)");
grid.append("line")
.attr("x1", 30 )
.attr("y1", 180 )
.attr("x2", 30 )
.attr("y2", 35)
.attr("stroke", "gray")
.attr("marker-end", "url(#arrow)");
// this explanation is for relationship strength
grid.append("text")
.attr("x", 75)
.attr("y", 175)
.text("greater equity*");
grid.append("line")
.attr("x1", 50)
.attr("y1", 180)
.attr("x2", 180)
.attr("y2", 180 )
.attr("stroke", "gray")
.attr("marker-end", "url(#arrow)");
}
var draw = function(geo_data) {
"use strict";
draw_color_matrix();
var map_width = 9 / 16 * window_x,
map_height = 500;
var svg = d3.select('#map_container')
.append('svg')
.attr("width", map_width)
.attr("height", map_height)
.call(d3.zoom().on("zoom", function () {
svg.attr("transform", d3.event.transform)
}))
.append('g')
.attr("class", "map");
// can use other projections as well!!
var projection = d3.geoMercator()
.scale(120)
.translate([map_width / 2, map_height / 1.4]);
var path = d3.geoPath().projection(projection);
var map = svg.selectAll("path")
.data(geo_data.features)
.enter()
.append("path")
.attr("d", path)
.attr("fill", "orange")
.attr("stroke", "gray")
.attr("stroke-width", 0.5);
var fill_countries = function(data) {
var switch_bar_chart = function(data, variable) {
var chart_width = 6 / 16 * window_x,
barheight = 20;
//need to sort data according to the given variable
data.sort(function(a, b) {
if(variable == "MATH_MEAN") {
return -(a[variable] - b[variable]);
// large math score appear first
} else if(variable == "strength") {
return a[variable] - b[variable];
//small strength(greater equity) appear first
}
});
var x = d3.scaleLinear()
.domain([0, d3.max(data, function(d) {
return +d[variable];
})])
.range([0, chart_width - 100]);
var barchart = d3.select(".barchart");
barchart.html("");
var bar = barchart.selectAll("g")
.data(data)
.enter().append("g")
.attr("transform", function(d, i) {
return "translate(100," + (i + 2) * barheight + ")";
});
bar.append("rect")
.attr("height", barheight - 2.5)
.attr("width", function(d) { return x(d[variable]); })
.attr("fill", function(d) {
if(d["CNT"] == "AVERAGE") {
return "#00a6bd";
}
return "#c9a288"
});
// this text is for labeling countries
bar.append("text")
.attr("x", -100)
.attr("y", barheight / 2)
.attr("dy", "0.35em")
.text(function(d) { return d["CNT"]; })
.style("font-size", "12px")
.style("text-anchor", "right")
.style("fill", function(d) {
if(d["CNT"] == "AVERAGE") {
return "#00a6bd";
}
return "black";
})
.style("font-weight", function(d) {
if(d["CNT"] == "AVERAGE") {
return "bold";
}
return "normal";
});
// this text is for math mean score
bar.append("text")
.attr("x", function(d) { return x(d[variable]) - 50;})
.attr("y", barheight / 2)
.attr("dy", "0.35em")
.text(function(d){ return Number(d[variable]).toFixed(2);})
.style("font-size", "12px")
.style("color", "white");
// this is for the title of this barchart
barchart.append("text")
.attr("x", 20)
.attr("y", 20)
.text(function(d) {
if(variable == "MATH_MEAN"){
return "Mean Math Score in Each Country";
} else if(variable == "strength") {
return "Math Variance Explained By Social-economic Status in Each Country"
}
})
.style("font-size", "14px");
}
var draw_bar_chart = function(data, variable) {
var chart_width = 6 / 16 * window_x,
barheight = 20;
var barchart = d3.select("#barchart_container")
.append("svg")
.attr("width", chart_width)
.attr("height", barheight * (data.length + 2))
.append("g")
.attr("class", "barchart");
switch_bar_chart(data, variable);
}
// buttons control which barchart to show
var button_values = ["mean math score", "math variance explained by social-economic status"];
var buttons = d3.select("#barchart_container")
.append("div")
.attr("class", "select_buttons")
.selectAll("button")
.data(button_values)
.enter().append("button")
.html(function(d) { return d; });
buttons.on("click", function(d) {
d3.selectAll(".select_buttons button")
.transition()
.duration(200)
.style("background-color", "#cccccc");
d3.select(this)
.transition()
.duration(200)
.style("background-color", "#fbb339");
if(d == button_values[0]) {
switch_bar_chart(data, "MATH_MEAN");
} else if(d == button_values[1]) {
debugger;
switch_bar_chart(data, "strength");
}
});
//initial state: draw math mean
draw_bar_chart(data, "MATH_MEAN");
d3.select(".select_buttons button")
.transition()
.duration(200)
.style("background-color", "#fbb339");
var countries = data.map(function(d) {
return d["CNT"];
});
// var invalid_countries = [];
// var valid_countries = [];
var path = svg.selectAll("path")
.attr("stroke", "gray")
.attr("stroke-width", 0.5)
.attr("fill", function(d) {
var index = countries.indexOf(d.properties.name);
if(index == -1 && d.properties.name != "Chinese Taipei") {
return "white";
} else {
if(d.properties.name == "Chinese Taipei") {
index = countries.indexOf("China");
}
var country_pisa = data[index];
var color_math_index = 3 - parseInt(country_pisa["MATH_INDEX"]);
// here is a little confusing, since in the .csv file, 3 means better math performance and 1 means poor math performances
//but here the first array in 2D-color-array represents better math performance
var color_strength_index = parseInt(country_pisa["RELATION_INDEX"]) - 1;
//in .csv file, 1 means strong relation between math performance and social-economic status, that is "less equity"
// this is consistent with the 2D-color-array, because the left(small index) means less equity
// valid_countries.push(d.properties.name);
var color = color_array[color_math_index][color_strength_index];
return color;
}
});
// for(var i = 0; i < countries.length; i++) {
// if(valid_countries.indexOf(countries[i]) == -1) {
// invalid_countries.push(countries[i]);
// }
// }
// console.log("invalid: ",invalid_countries);
var tooltip = d3.select("body")
.append("div")
.attr("class", "tooltip")
.style("opacity", 0);
path.on("mouseover", function(d) {
var index = countries.indexOf(d.properties.name);
if(index != -1) {
var country_pisa = data[index];
tooltip.transition()
.duration(200)
.style("opacity", 0.9);
tooltip.html("<strong>Country:</strong> " + country_pisa["CNT"] + "<br>\
<strong>Average math score:</strong> " + Number(country_pisa["MATH_MEAN"]).toFixed(2) + "<br>\
<strong>Math gender gap:</strong> " + Number(country_pisa["MATH_GENDER_GAP"]).toFixed(2) + "<br>\
<strong>Variance explained by social-economic status(%):</strong> " + Number(country_pisa["strength"]).toFixed(2))
.style("left", (d3.event.pageX - 50) + "px")
.style("top", (d3.event.pageY - 20) + "px");
path.attr("stroke", function(d2) {
if(d == d2) {
return "black";
} else {
return "gray";
}
})
.attr("stroke-width", function(d2) {
if(d == d2) {
return 2;
} else {
return 0.5;
}
});
}
});
path.on("mouseout", function(d) {
tooltip.transition()
.duration(200)
.style("opacity", 0);
path.attr("stroke", "gray")
});
};
var draw_scatter = function(america_data) {
var scatter_width = 7 / 16 * window_x,
scatter_height = 400;
var margin = {
top: 20,
right: 20,
bottom: 30,
left: 40
};
var svg = d3.select('#scatter_container')
.append("svg")
.attr("width", scatter_width + margin.left + margin.right)
.attr("height", scatter_height + margin.top + margin.bottom)
.append("g")
.attr("class", "scatter")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var x = d3.scaleLinear()
.range([0, scatter_width])
.domain([-4, 4]);
var y = d3.scaleLinear()
.range([scatter_height, 0])
.domain([200, 900]);
var xAxis = d3.axisBottom()
.scale(x);
var yAxis = d3.axisLeft()
.scale(y);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + scatter_height + ")")
.call(xAxis)
.append("text")
.attr("class", "label")
.text("index of social-economic status")
.attr("x", scatter_width)
.attr("y", -6)
.style("text-anchor", "end")
.attr("fill", "black");
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("class", "label")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", "0.7em")
.style("text-anchor", "end")
.text("math score")
.attr("fill", "black");
var line = d3.line()
.x(function(d) {
return x(d["ESCS"]);
})
.y(function(d) {
return y(d["predict"]);
});
//add data points on the svg
svg.selectAll("circle")
.data(america_data)
.enter()
.append("circle")
.attr("cx", function(d) {
return x(d["ESCS"]);
})
.attr("r", 3.5)
.attr("cy", function(d) {
return y(d["MATH"]);
})
.attr("fill", "lightBlue")
.attr("stroke", "gray")
.attr("stroke-width", 0.7);
// add regression line on the svg
svg.append('path')
.datum(america_data)
.attr("class", "line")
.attr("d", line);
//title
svg.append("text")
.attr("x", margin.left * 2)
.attr("y", 20)
.text("Relationship between math score and social-economic status in USA*")
.style("font-size", "16px")
.style("font-weight", "bold");
// add explaination for this scatterplot
d3.select("#scatter_container")
.append("div")
.html('*This scatterplot(using 500 samples of USA dataset) explains how the "equity" was calculated. We could get a linear model, predicting math score using index of social-economic status. And the strength of these two variables are represented by the r-squared value, i.e., the math score variance explained by social-economic status. <strong>If the relationship is strong, then we would think there was less equity.</strong>');
};
d3.csv("INFO_PISA2012.csv", fill_countries);
d3.csv("america_pisa_sample.csv", draw_scatter);
var story = [
"*Important: <strong>here greater equity meant less math variance explained by social-economic status.</strong>",
"PISA assesses the extent to which 15-year-old students have acquired key knowledge and skills that are essential for full participation in modern societies. And we mainly explore the strength of relationship between math score and social-economic status in more than 60 countries. <strong>Countries in white color didn't participate.</strong>",
"The countries are divided into 9 groups due to their math score and strength of relationship",
"1. The mean math score of Latin America was not good.",
"2. China/South Korea/Japan/Canda all had good math performance and great equity in education.",
"3. Many European countries had good math performance, but the equity was not that great.",
"4. Two states of US, Connecticut and Massachusetts had better math score than the whole country, but less equity",
"source: <a link='http://www.oecd.org/pisa/pisaproducts/datavisualizationcontest.htm'>PISA 2012</a>",
"*There were four main cities separately tested in China, including Shanghai/Hong Kong/Macao/Taipei. Since the cities were hardly seen on the map, so I averaged the four cities to represent China.",
"<hr>"
]
d3.select('#conclusion')
.selectAll("p")
.data(story).enter()
.append("p")
.html(function(d) { return d;});
};
d3.json("world_countries.json", draw);
</script>
</body>
</html>