forked from dandanbang/D3_Ranking_College
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
419 lines (383 loc) · 14.5 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.5.0/pure-min.css">
<link rel="stylesheet" href="src/radar-chart.css">
<link rel="stylesheet" href="src/radar-main.css">
<link rel="stylesheet" href="src/pc-style.css">
<link rel="shortcut icon" href=http://www.freshdesignweb.com/wp-content/themes/fv28/images/icon.ico />
<link rel="stylesheet" type="text/css" href="style.css" media="all" />
<script type="text/javascript" src="src/radar-chart.js"></script>
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
<script src="http://d3js.org/d3.v3.js"></script>
<script src="https://rawgit.com/tpreusse/radar-chart-d3/master/src/radar-chart.js"></script>
</head>
<body class='backdrop'>
<h1>Global University Ranking Comparisons</h1>
<p width='960px'>Not all universities were created equal. We want to show on how the world's top 200 universities, dispersed
across each continents, performed on key metrics compared with each others. Through the use
of a parallel coordinates chart and the affordance to browse through different years, we hope you will
learn something about the relative strength of top schools and how they evolved. </p>
<p>
We then use a radar chart for you to compare top universities from each country. This time the scores are averaged across
the years, so you can compare how each country's top universities compare with each other generally.
</p>
<h1>Parallel Coordinates Chart</h1>
<p> Click on buttons to change the year of data shown.</p>
<p> The average score of schools in each country from 2011 - 2016 will display</p><br /><br />
<div class="year_button">
<button type="button active" id="2011">2011</button>
<button type="button" id="2012">2012</button>
<button type="button" id="2013">2013</button>
<button type="button" id="2014">2014</button>
<button type="button" id="2015">2015</button>
<button type="button" id="2016">2016</button>
</div><br /><br />
<script>
var margin = {
top: 30,
right: 10,
bottom: 10,
left: 10
},
width = 1440 - margin.left - margin.right,
height = 600 - margin.top - margin.bottom;
var x = d3.scale.ordinal().rangePoints([0, width], 1),
y = {},
dragging = {};
var line = d3.svg.line(),
axis = d3.svg.axis().orient("left"),
background,
foreground;
var svg1 = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g").attr('class', 'pc')
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var year = '2011';
var dataHolder = {
'2011': [],
'2012': [],
'2013': [],
'2014': [],
'2015': [],
'2016': []
};
d3.csv("PC_timesData.csv", function(error, universities) {
// Extract the list of dimensions and create a scale for each.
x.domain(dimensions = d3.keys(universities[0]).filter(function(d) {
return d != "university_name" && d != "country" && d != "female_male_ratio" && d != "year" && (y[d] = d3.scale.linear()
.domain(d3.extent(universities, function(p) {
return +p[d];
}))
.range([height, 0]));
}));
universities.forEach(function(d) {
var x = {};
if (d.country == 'United States of America' || d.country == 'Canada') {
x.continent = 'NA';
}
else if (d.country == 'Brazil') {
x.continent = 'SA';
}
else if (d.country == 'Sweden' || d.country == 'Finland' || d.country == 'Norway' || d.country == 'Denmark' || d.country == 'Republic of Ireland'
|| d.country == 'United Kingdom' || d.country == 'Netherlands' || d.country == 'Belgium' || d.country == 'Germany' || d.country == 'Luxembourg'
|| d.country == 'France' || d.country == 'Switzerland' || d.country == 'Austria' || d.country == 'Italy' || d.country == 'Spain'
|| d.country == 'Russia' || d.country == 'Turkey') {
x.continent = 'EU';
}
else if (d.country == 'Egypt' || d.country == 'Israel' || d.country == 'South Africa') {
x.continent = 'AF';
}
else if (d.country == 'China' || d.country == 'South Korea' || d.country == 'Japan' || d.country == 'Hong Kong' || d.country == 'Taiwan' || d.country == 'Singapore') {
x.continent = 'AA';
}
else if (d.country == 'Australia' || d.country == 'New Zealand') {
x.continent = 'OC';
}
x.world_rank = +d.world_rank;
x.teaching = +d.teaching;
x.international = +d.international;
x.research = +d.research;
x.citations = +d.citations;
x.income = +d.income;
x.total_score = +d.total_score;
x.year = d.year;
switch (x.year) {
case '2016':
dataHolder['2016'].push(x);
break;
case '2015':
dataHolder['2015'].push(x);
break;
case '2014':
dataHolder['2014'].push(x);
break;
case '2013':
dataHolder['2013'].push(x);
break;
case '2012':
dataHolder['2012'].push(x);
break;
default:
dataHolder['2011'].push(x);
}
})
populate(year, dataHolder);
// Add a group element for each dimension.
var g = svg1.selectAll(".dimension")
.data(dimensions)
.enter().append("g")
.attr("class", "dimension")
.attr("transform", function(d) {
return "translate(" + x(d) + ")";
})
.call(d3.behavior.drag()
.origin(function(d) {
return {
x: x(d)
};
})
.on("dragstart", function(d) {
dragging[d] = x(d);
background.attr("visibility", "hidden");
})
.on("drag", function(d) {
dragging[d] = Math.min(width, Math.max(0, d3.event.x));
foreground.attr("d", path);
dimensions.sort(function(a, b) {
return position(a) - position(b);
});
x.domain(dimensions);
g.attr("transform", function(d) {
return "translate(" + position(d) + ")";
})
})
.on("dragend", function(d) {
delete dragging[d];
transition(d3.select(this)).attr("transform", "translate(" + x(d) + ")");
transition(foreground).attr("d", path);
background
.attr("d", path)
.transition()
.delay(500)
.duration(0)
.attr("visibility", null);
}));
// Add an axis and title.
g.append("g")
.attr("class", "axis")
.each(function(d) {
d3.select(this).call(axis.scale(y[d]));
})
.append("text")
.style("text-anchor", "middle")
.attr("y", -9)
.text(function(d) {
return d;
});
// Add and store a brush for each axis.
g.append("g")
.attr("class", "brush")
.each(function(d) {
d3.select(this).call(y[d].brush = d3.svg.brush().y(y[d]).on("brushstart", brushstart).on("brush", brush));
})
.selectAll("rect")
.attr("x", -8)
.attr("width", 16);
});
function populate(year, dataHolder) {
// Add grey background lines for context.
background = svg1.append("g")
.attr("class", "background")
.selectAll("path")
.data(dataHolder[year])
.enter().append("path")
.attr("d", path);
// Add blue foreground lines for focus.
foreground = svg1.append("g")
.attr("class", "foreground")
.selectAll("path")
.data(dataHolder[year])
.enter().append("path")
.attr("d", path)
.attr("class", function(d) {
if (d['continent'] == 'NA') {
return 'NA';
} else if (d['continent'] == 'SA') {
return 'SA';
} else if (d['continent'] == 'EU') {
return 'EU';
} else if (d['continent'] == 'AA') {
return 'AA';
} else if (d['continent'] == 'OC') {
return 'OC';
} else if (d['continent'] == 'AF') {
return 'AF';
}
});
}
$('button').on('click', function() {
d3.selectAll('.background').remove();
d3.selectAll('.foreground').remove();
populate($(this).text(), dataHolder);
})
function position(d) {
var v = dragging[d];
return v == null ? x(d) : v;
}
function transition(g) {
return g.transition().duration(500);
}
// Returns the path for a given data point.
function path(d) {
return line(dimensions.map(function(p) {
return [position(p), y[p](d[p])];
}));
}
function brushstart() {
d3.event.sourceEvent.stopPropagation();
}
// Handles a brush event, toggling the display of foreground lines.
function brush() {
var actives = dimensions.filter(function(p) {
return !y[p].brush.empty();
}),
extents = actives.map(function(p) {
return y[p].brush.extent();
});
foreground.style("display", function(d) {
return actives.every(function(p, i) {
return extents[i][0] <= d[p] && d[p] <= extents[i][1];
}) ? null : "none";
});
}
</script>
<div>
<div style="background-color:orange"></div><div>North America</div>
<div style="background-color:lime"></div><div>South America</div>
<div style="background-color:skyblue"></div><div>Europe</div>
<div style="background-color:yellow"></div><div>Africa</div>
<div style="background-color:red"></div><div>Asia</div>
<div style="background-color:purple"></div><div>Oceania</div>
</div><br /><br />
<h1>Radar Chart</h1>
<p> Click on buttons to add country onto the Radar Chart.</p>
<p> The average score of schools in each country from 2011 - 2016 will display</p>
<p> Please remove countries in the same order to avoid color confusion. Also please use Chrome Browser.</p>
<!-- for radar chart -->
<div class='chart-container'></div></br></br>
<!-- display all buttons here -->
<div id="buttons"><li id="refresh"><a data-country= data-action="1" class="tag" id="button">Refresh</a></li><br></div>
</body>
<script>
// setting the default configuration for radar
RadarChart.defaultConfig.color = function(i) { c = colorArray ; return c[i];}
//var colorScale = d3.scale.category20();
//RadarChart.defaultConfig.colors = colorScale;
RadarChart.defaultConfig.radius = 3;
RadarChart.defaultConfig.w = 450;
RadarChart.defaultConfig.h = 450;
RadarChart.defaultConfig.levels = 10;
// declaring needed variables
var colorArray = [];
var colorDict = {};
var radar_data = [];
var dict = {};
var finalData = [];
var chart = RadarChart.chart();
var cfg = chart.config(); // retrieve default config
var svg = d3.select('body').append('svg').classed('svg', 1).attr('width', cfg.w + 50).attr('height', cfg.h + cfg.h / 4);
var colorIndex = 0;
// read the file when document is ready
$(document).ready(function() {
$.ajax({
type: "GET",
url: "Radar_Country_Score.csv",
dataType: "text",
success: function(data) {extractData(data);}
});
});
// generate 50 random color
$(document).ready(function() {
for (i=0; i<50; i++)
colorArray.push("#" + ((1 << 24) * Math.random() | 0).toString(16))
});
//function to process csv data and prepare the data structure
function extractData(csv) {
var lines = csv.split(/\r\n|\n|\r/); //spliting lines
for (var i=0; i<lines.length - 1; i++) {
var data = lines[i].split(","); //splitting lines into elements
displayData(String(data[0]));
dict[data[0]] = i; //storing key value pair of country name and index for searches
var _object = {};
_object.className = String(data[0]);
axes_list = []
axes_list.push({ axis: "teaching", value: parseFloat(data[1])});
axes_list.push({ axis: "total_score", value: parseFloat(data[2])});
axes_list.push({ axis: "research", value: parseFloat(data[3])});
axes_list.push({ axis: "citations", value: parseFloat(data[4])});
axes_list.push({ axis: "income", value: parseFloat(data[5])});
_object.axes = axes_list
radar_data.push(_object)
}
}
//take action when user clicks on buttons
$("#buttons").on('click', '#button', function() {
if (this.getAttribute("data-action") == "1") {
$(this).css('color', 'white');
addRadar(this.getAttribute("data-country"));
// colorDict[$(this).text()] = colorIndex;
//colorArray.push(String($("polygon")[finalData.length - 1 ].getAttribute('style').substring(8, 25)));
// index = colorDict[$(this).text()]
$(this).css('background-color', colorArray[colorIndex]);
colorIndex++;
this.setAttribute("data-action", "2");
} else {
$(this).css('background-color', '#f2f2f3');
$(this).css('color', '#778187');
// $("a").each(function(){
// alert($(this).children().css('background-color)');
// $(this).children().css('background-color', colorArray[$(this).text()]);
// });
removeRadar(this.getAttribute("data-country"));
//colorArray.pop();
this.setAttribute('data-action', "1");
colorIndex--;
}
});
// refresh buttons for user to refresh
$("#refresh").on('click', '#button', function() {
location.reload();
});
// function for adding a new data onto radar chart
function addRadar(country) {
index = dict[country];
finalData.push(radar_data[index])
displayRadar();
}
// function for removing a new data onto radar chart
function removeRadar(country) {
for (i=0; i< finalData.length; i++) {
if (finalData[i].className == country) {
finalData.splice(i, 1);
}
displayRadar();
}}
// function for dsiplayng a new data onto radar chart
function displayRadar(){
if (finalData == 0) {
//colorArray.pop();
colorIndex--;
}
svg.selectAll("g").remove();
svg.append('g').datum(finalData).call(chart);
}
// display the buttons
function displayData(data) {
var button = $('<li id="buttonlist"><a data-country="' + data + '" data-action="1" class="tag" id="button">' + data + '</a></li>');
button.appendTo('#buttons');
}
</script>
</html>