/
courses-college-admission-grade.js
267 lines (210 loc) · 9.6 KB
/
courses-college-admission-grade.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
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
window.onload = function() {
// Setup chart dimensions
var margin = {top: 20, right: 30, bottom: 50, left: 60},
width = 688 - margin.left - margin.right,
height = 520 - margin.top - margin.bottom;
var chart = d3.select(".chart")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom);
var data,
courses,
gradeToShow,
gradeToShowTitleText;
// Setup scales and axes
var x = d3.scale.ordinal()
.rangeBands([0, width], 0.1, 0.1);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
chart.append("g")
.attr("class", "x axis")
.attr("transform", "translate(" + margin.left + ", " + (height + margin.top) + ")")
.call(xAxis)
.append("text")
.attr("dy", "-.71em")
.attr("x", width)
.attr("dx", -6)
.style("text-anchor", "end")
.text("Cursos");
var y = d3.scale.linear()
.range([height, 0])
.domain([95, 200]);
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
chart.append("g")
.attr("class", "y axis")
.call(yAxis)
.attr("transform", "translate(" + margin.left + ", " + margin.top + ")")
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Média de ingresso");
var color = d3.scale.category10();
// Setup select2
var courseSelectJquery = jQuery('#menu select[name=course]');
courseSelectJquery.select2();
courseSelectJquery.on("change", changeCourses);
var gradeSelectJquery = jQuery('#menu select[name=grade]');
gradeSelectJquery.on("change", changeGradeMetric);
// Define the div for the tooltip
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style('opacity', 0);
//Add source
chart.append('text')
.attr('class', 'source')
.attr("x", ( margin.left + width ))
.attr("y", ( margin.top + height + margin.bottom))
.attr("text-anchor", "end")
.style("font-size", "10px")
.text("Fonte: DGES");
d3.csv("/wp-content/themes/goncalovf-theme/content/post-data/courses-college-admission-grade.csv", type, function(loadedData) {
data = loadedData;
data.forEach(function(d) {
d.color = color(d.Academic_level);
});
var courseSelectD3 = d3.select("#menu select");
courseSelectD3.selectAll("option")
.data(d3.map(data, function(d){return d.Course;}).keys()) // this filters the courses to only show unique values
.enter().append("option")
.text(function(d) { return d; });
// Add legend
var legend = chart.selectAll(".legend")
.data(color.domain())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + (i * 20 + margin.top) + ")"; });
legend.append("rect")
.attr("x", (margin.left + width - 18))
.attr("width", 18)
.attr("height", 18)
.style("fill", color);
legend.append("text")
.attr("x", (margin.left + width - 24))
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d; });
var availableCourses = ["Arquitetura", "Biologia", "Bioquímica", "Biotecnologia", "Ciências Biomédicas Laboratoriais", "Ciências da Comunicação", "Ciências do Desporto", "Contabilidade", "Economia", "Educação Básica", "Educação Social", "Enfermagem", "Engenharia Biomédica", "Engenharia Civil", "Engenharia do Ambiente", "Engenharia e Gestão Industrial", "Engenharia Eletrotécnica e de Computadores", "Engenharia Física", "Engenharia Informática", "Engenharia Mecânica", "Engenharia Química", "Farmácia", "Fisioterapia", "Geologia", "Gestão", "Gestão de Empresas", "História", "Imagem Médica e Radioterapia", "Línguas, Literaturas e Culturas", "Marketing", "Matemática", "Medicina", "Psicologia", "Serviço Social", "Sociologia", "Solicitadoria", "Turismo"];
var shuffledAvailableCourses = availableCourses.sort(() => .5 - Math.random()); // shuffle
courses = shuffledAvailableCourses.slice(0,5).sort() ; //get sub-array of first n elements AFTER shuffle
courseSelectJquery.val(courses);
gradeToShow = 'Admission_grade_2017';
gradeToShowTitleText = '2017';
// Update chart title
chart.append("text")
.attr('class', 'title')
.attr("x", ( margin.left + width / 2))
.attr("y", margin.top)
.attr("text-anchor", "middle")
.style("font-size", "16px")
.style("font-weight", "bold")
.text("Média de ingresso por curso em cada instituição pública, 1ª fase, " + gradeToShowTitleText);
redraw()
});
function type(d) {
d.Admission_grade_2017 = +d.Admission_grade_2017; // coerce to number
d.Admission_grade_avg = +d.Admission_grade_avg; // coerce to number
return d;
}
function changeCourses() {
courses = courseSelectJquery.val() || [];
redraw();
}
function changeGradeMetric() {
gradeToShow = gradeSelectJquery.val();
gradeToShowTitleText = ( gradeToShow === 'Admission_grade_2017' ) ? '2017' : 'média entre 2017-2015';
chart.select('.title')
.text("Média de ingresso por curso em cada instituição pública, 1ª fase, " + gradeToShowTitleText);
redraw();
}
function redraw() {
// Update xAxis
x.domain(courses);
d3.transition(chart).select(".x.axis")
.call(xAxis);
var bandWidth = x.rangeBand();
var circle = chart.selectAll(".dot")
.data(data.filter(function(d) { return courses.includes(d.Course) }))
.attr("cx", function(d) { return x(d.Course) })
.attr("cy", function(d) { return y(d[gradeToShow]) })
.style("fill", function(d) { return d.color; })
.attr("transform", "translate(" + (margin.left + bandWidth / 2)+ ", " + margin.top + ")");
circle.exit().remove();
circle.enter().append("circle")
.attr("class", "dot")
.attr("r", 4)
.attr("cx", function(d) { return x(d.Course) })
.attr("cy", function(d) { return y(d[gradeToShow]) })
.style("fill", function(d) { return d.color; })
.attr("transform", "translate(" + (margin.left + bandWidth / 2)+ ", " + margin.top + ")")
.on("mouseover", function(d) {
div.transition()
.duration(200)
.style('opacity', 1);
div.html(d.Inst_fullname + '<br>Nota de ingresso 2017: ' + d.Admission_grade_2017 + '<br>Nota de ingresso (média 2017-2015): ' + d.Admission_grade_avg + '<br><a href="http://www.dges.gov.pt/guias/detcursopi.asp?codc=' + d.Course_code + '&code=' + d.Inst_code + '" target="_blank">Ver no guia de cursos</a> <i class="fas fa-external-link-alt"></i>')
.attr("class", "tooltip")
.style("left", (d3.event.pageX + 5) + "px")
.style("top", (d3.event.pageY - 28) + "px")
})
.on("mouseout", function() {
d3.selectAll("div.tooltip")
.transition()
.delay(1500)
.duration(200)
.style('opacity', 0);
});
// Prevent xAxis labels from overlapping
chart.selectAll('.tick text')
.call(wrap, bandWidth);
// Add force to prevent circles from overlapping
var force = d3.layout.force()
.nodes(data.filter(function(d) { return courses.includes(d.Course) }))
.size([width, height])
.on("tick", tick)
.charge(-1)
.gravity(0)
.chargeDistance(20);
force.start();
function tick(e) {
d3.selectAll('.dot').each(moveTowardDataPosition(e.alpha));
d3.selectAll('.dot').attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
}
function moveTowardDataPosition(alpha) {
return function(d) {
d.x += (x(d.Course) - d.x) * 0.1 * alpha;
d.y += (y(d[gradeToShow]) - d.y) * 0.1 * alpha;
};
}
// Make y axis labels well positioned, before the ticks
chart.selectAll('.y.axis tspan')
.attr('x', -10);
}
function wrap(text, bandWidth) {
text.each(function() {
var text = d3.select(this),
words = text.text().split(/\s+/).reverse(),
word,
line = [],
lineNumber = 0,
lineHeight = 1.1, // ems
y = text.attr("y"),
dy = parseFloat(text.attr("dy")),
tspan = text.text(null).append("tspan").attr("x", 0).attr("y", y).attr("dy", dy + "em");
while (word = words.pop()) {
line.push(word);
tspan.text(line.join(" "));
if (tspan.node().getComputedTextLength() > bandWidth) {
line.pop();
tspan.text(line.join(" "));
line = [word];
tspan = text.append("tspan").attr("x", 0).attr("y", y).attr("dy", ++lineNumber * lineHeight + dy + "em").text(word);
}
}
});
}
};