-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
147 lines (119 loc) · 4.21 KB
/
script.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
var moduleNames = {
"COMP101P": "Principles of Programming",
"COMP102P": "Theory I",
"COMP103P": "Object-Oriented Programming",
"COMP104P": "Theory II",
"COMP105P": "Robotics Programming",
"ENGS101P": "Integrated Engineering",
"ENGS102P": "Design & Professional Skills",
"MATH6301": "Discrete Mathematics for Computer Scientists"
};
var modules = _.keys(moduleNames);
function tryParseFloats(obj) {
for (var key in obj) {
var value = parseFloat(obj[key]);
if (!isNaN(value)) obj[key] = value;
}
return obj;
}
function removeEmptyStrings(obj) {
for (var key in obj) {
if (obj[key] === "") delete obj[key];
}
return obj;
}
function substringMatcher(strs) {
return function findMatches(q, cb) {
var matches, substringRegex;
// an array that will be populated with substring matches
matches = [];
// regex used to determine if a string contains the substring `q`
substrRegex = new RegExp(q, 'i');
// iterate through the pool of strings and for any string that
// contains the substring `q`, add it to the `matches` array
$.each(strs, function(i, str) {
if (substrRegex.test(str)) {
matches.push(str);
}
});
cb(matches);
};
}
d3.csv("data/bsc.csv", function(err, BSc) {
d3.csv("data/meng.csv", function(err, MEng) {
var data = BSc.concat(MEng);
data.forEach(removeEmptyStrings);
data.forEach(tryParseFloats);
var formatCount = d3.format(",.0f");
var margin = {top: 10, right: 10, bottom: 50, left: 10},
width = 500 - margin.left - margin.right,
height = 200 - margin.top - margin.bottom;
var root = d3.select("#content");
var x = d3.scale.linear()
.domain([0, 100])
.range([0, width]);
var histogram = d3.layout.histogram()
.bins(x.ticks(10));
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var moduleSVGs = root.selectAll("div")
.data(modules)
.enter()
.append("div")
.attr("class", "module")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
moduleSVGs.append("text")
.attr("class", "module-code")
.attr("dy", "1.5em")
.attr("y", height + 17)
.attr("x", width / 2)
.attr("text-anchor", "middle")
.text(function(d) { return d + ": " + moduleNames[d].toUpperCase(); });
var bar = moduleSVGs.selectAll(".bar")
.data(function(module) {
return histogram(_(data).pluck(module).compact().value())
})
.enter().append("g");
var allData = bar.map(function(bars) { return d3.selectAll(bars).data() });
y.domain([0, d3.max(allData, function(d) { return d3.max(d, function(d) { return d.y }); })]);
bar.attr("class", "bar")
.attr("transform", function(d) { return "translate(" + x(d.x) + "," + y(d.y) + ")"; });
bar.append("rect")
.attr("x", 1)
.attr("width", function(data) { return x(data.dx) - 1; })
.attr("height", function(d) { return height - y(d.y); });
bar.append("text")
.attr("dy", ".375em")
.attr("y", function(d) {
if (y(d.y) > height - 15) return -9;
else return 9;
})
.style("fill", function(d) {
if (y(d.y) > height - 15) return "black";
else return "white";
})
.attr("x", function(d) { return x(d.dx) / 2 })
.attr("text-anchor", "middle")
.text(function(d) { return formatCount(d.y); });
moduleSVGs.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
// $('#search .typeahead').typeahead({
// hint: true,
// highlight: true,
// minLength: 1
// },
// {
// name: 'candidate-number',
// source: substringMatcher(_.pluck(data, "Student Candidate Number"))
// });
});
});