/
courses-preference-association.js
282 lines (214 loc) · 8.65 KB
/
courses-preference-association.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
* Relationship between university courses based on students' application preferences when applying to college, Portugal, 2017
*
* Remixed from Michael Currie, @ https://bl.ocks.org/MichaelCurrie/5e2da378a53ea624082cb55e78fdfa05
*
*/
var char_dim_width,
height_shift;
if (screen.width < 800) {
char_dim_width = screen.width;
height_shift = 75;
} else {
char_dim_width = 688;
height_shift = 25;
}
var PARAM = {
"data_location": "/wp-content/themes/goncalovf-theme/content/post-data/courses-preference-association.csv",
"report_title": "Associação de cursos nas preferências dos alunos, 1ª fase, 2017",
"footer_text": "Fonte: DGES.",
"margins": {
"top": 20,
"right": 30,
"bottom": 50,
"left": 50
},
"chart_dim": {
"width": char_dim_width,
"height": 520,
"height_shift": height_shift
},
"node_attr": {
"id": "rel",
"radius": "points",
"fill_color": "rel_bagg"
},
"radiusScale_range": {
"min": 2,
"max": 75
},
"force_strength": 15
};
function createBubbleChart() {
var ref,
width,
height,
chart,
radiusScale,
fillColorScale,
data,
filtered_data,
node,
nodes,
bubbles;
var tooltip = d3.select("body").append("div")
.attr("class", "tooltip")
.style('opacity', 0);
// Setup Select2
var courseSelectJquery = jQuery('#menu select[name=ref]')
.select2()
.on("change", changeRef);
function createCanvas() {
width = PARAM.chart_dim.width - PARAM.margins.left - PARAM.margins.right;
height = PARAM.chart_dim.height - PARAM.margins.top - PARAM.margins.bottom;
chart = d3.select(".chart")
.attr("width", PARAM.chart_dim.width)
.attr("height", PARAM.chart_dim.height);
}
function createNodes(filtered_data) {
var myNodes = filtered_data.map(function (d) {
node = {
ref: d.ref,
id: d[PARAM.node_attr.id],
scaled_radius: radiusScale(+d[PARAM.node_attr.radius]),
actual_radius: +d[PARAM.node_attr.radius],
fill_color_group: d[PARAM.node_attr.fill_color],
course: d.rel_name
};
return node;
});
return myNodes;
}
var bubbleChart = function bubbleChart( rawData ) {
data = rawData;
d3.select("#menu select[name=ref]").selectAll("option")
.data(d3.map(rawData, function(d){return d.ref_name;}).keys()) // this filters the courses to only show unique values
.enter().append("option")
.attr("value", function (d) { return d; })
.text(function(d) { return d; });
// Default course to show
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"];
ref = availableCourses[Math.floor(Math.random() * availableCourses.length)];
courseSelectJquery.val(ref);
fillColorScale = d3.scaleOrdinal(d3.schemeCategory10)
.domain( d3.map(rawData, function(d){return d[PARAM.node_attr.fill_color];}).keys() );
createCanvas();
redraw();
};
function redraw() {
filtered_data = filterData();
addLegend( filtered_data );
var maxRadius = d3.extent(filtered_data, function (d) { return +d[PARAM.node_attr.radius] })[1];
radiusScale = d3.scaleLinear()
.range([PARAM.radiusScale_range.min, PARAM.radiusScale_range.max])
.domain([0, maxRadius]);
nodes = createNodes(filtered_data);
// Create the bubbles and the force holding them apart
createBubbles();
}
function filterData() {
var filtered_data = data.filter(function(d) { return d.ref_name === ref });
filtered_data.sort(function(a, b) {
return parseFloat(b.points) - parseFloat(a.points);
});
return filtered_data.slice(0, 30);
}
function addLegend( filtered_data ) {
var legend_data = d3.map(filtered_data, function(d){return d[PARAM.node_attr.fill_color] }).keys();
var rect = chart.selectAll(".legend_rect")
.data( legend_data );
rect.exit().remove();
rect.enter()
.append("rect")
.attr("class", "legend_rect")
.attr("x", (PARAM.margins.left + width - 18))
.attr("transform", function(d, i) { return "translate(0," + (i * 20 + PARAM.margins.top) + ")" })
.attr("width", 18)
.attr("height", 18)
.merge(rect)
.style("fill", function(d) { return fillColorScale(d) });
var text = chart.selectAll(".legend_text")
.data( legend_data );
text.exit().remove();
text.enter()
.append("text")
.attr("class", "legend_text")
.attr("x", (PARAM.margins.left + width - 24))
.attr("transform", function(d, i) { return "translate(0," + (i * 20 + PARAM.margins.top) + ")" })
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.merge(text)
.text( function(d) { return d; } );
}
function createBubbles() {
if (forceSim) {
forceSim.stop(); // Stop any forces currently in progress
}
bubbles = chart.selectAll('.bubble')
.data(nodes);
var bubblesEnter = bubbles.enter()
.append('circle')
.attr('class', "bubble")
.attr('stroke-width', 1)
.on("mouseover", function(d) {
d3.select(this).attr('stroke-width', 2);
tooltip.transition()
.duration(200)
.style('opacity', 1);
tooltip.html(d.course + '<br><a href="http://www.dges.gov.pt/guias/indcurso.asp?curso=' + d.id + '" 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 + 5) + "px")
})
.on("mouseout", function() {
d3.select(this).attr('stroke-width', 1);
d3.selectAll("div.tooltip")
.transition()
.delay(1500)
.duration(200)
.style('opacity', 0);
});
bubbles = bubbles.merge(bubblesEnter)
.attr('r', 0) // Initially, their radius (r attribute) will be 0.
.attr('fill', function (d) {
return fillColorScale(d.fill_color_group)
})
.attr('stroke', function (d) {
return d3.rgb(fillColorScale(d.fill_color_group)).darker()
});
bubbles
.exit()
.remove();
bubbles.transition()
.duration(2000)
.attr('r', function (d) { return d.scaled_radius });
var forceSim = d3.forceSimulation()
.nodes(nodes)
.velocityDecay(0.3)
.on("tick", tick)
.force('charge', d3.forceManyBody().strength(+PARAM.force_strength))
.force('center', d3.forceCenter( width / 2, height / 2 + PARAM.chart_dim.height_shift))
.force('collision', d3.forceCollide().radius(function(d) {
return d.scaled_radius
}));
}
function tick() {
bubbles.each(function (node) {})
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
}
function changeRef() {
ref = courseSelectJquery.val() || [];
redraw();
}
return bubbleChart;
}
window.onload = function() {
// Create a new bubble chart instance
var myBubbleChart = createBubbleChart();
// Load data
d3.csv( PARAM.data_location ).then(function( rawData ) {
myBubbleChart(rawData);
});
};