-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathworking_time_graphs.js
More file actions
296 lines (248 loc) · 10 KB
/
working_time_graphs.js
File metadata and controls
296 lines (248 loc) · 10 KB
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
$(document).on('turbo-migration:load', function() {
// /38/statistics good for testing
if ($.isController('exercises') && $('.working-time-graphs').isPresent()) {
var working_times = $('#data').data('working-time');
function get_minutes (timestamp){
try{
hours = timestamp.split(":")[0];
minutes = timestamp.split(":")[1];
seconds = timestamp.split(":")[2];
return parseFloat(hours * 60) + parseInt(minutes);
} catch (err){
return 0;
}
}
// GET ALL THE DATA ------------------------------------------------------------------------------
minutes_array = _.map(working_times, function(item){return get_minutes(item)});
minutes_array_length = minutes_array.length;
if (minutes_array_length === 0){
return;
}
maximum_minutes = _.max(minutes_array) + 1; // We need to respect the last minute as well
const minutes_count = new Array(maximum_minutes).fill(0);
for (var i = 0; i < minutes_array_length; i++){
var studentTime = minutes_array[i];
for (var j = 0; j < studentTime; j++){
minutes_count[j] += 1;
}
}
function getWidth() {
if (self.innerWidth) {
return self.innerWidth;
}
if (document.documentElement && document.documentElement.clientWidth) {
return document.documentElement.clientWidth;
}
if (document.body) {
return document.body.clientWidth;
}
}
// DRAW THE LINE GRAPH ------------------------------------------------------------------------------
function drawLineGraph() {
var width_ratio = .8;
if (getWidth() * width_ratio > 1000){
width_ratio = 1000 / getWidth();
}
var height_ratio = .7;
var margin = {top: 100, right: 20, bottom: 70, left: 70},//30,50
width = (getWidth() * width_ratio) - margin.left - margin.right,
height = (width * height_ratio) - margin.top - margin.bottom;
//var formatDate = d3.time.format("%M");
var x = d3.scaleLinear()
.range([0, width]);
var y = d3.scaleLinear()
.range([height, 0]); // - (height/20
var xAxis = d3.axisBottom(x).ticks(20);
var yAxis = d3.axisLeft(y)
.ticks(20)
.tickSizeInner(-width)
.tickSizeOuter(0);
var line = d3.line()
.x(function (d, i) {
return x(i);
})
.y(function (d) {
return y(d / minutes_count[0] * 100);
});
var svg = d3.select("#chart_1").append("svg") //PLACEMENT GOES HERE <---------------
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
x.domain(d3.extent(minutes_count, function (d, i) {
return (i);
}));
y.domain(d3.extent(minutes_count, function (d) {
return (d / minutes_count[0] * 100);
}));
svg.append("g") //x axis
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("text")// x axis label
.attr("class", "x axis")
.attr("text-anchor", "middle")
.attr("x", width / 2)
.attr("y", height)
.attr("dy", ((height / 20) + 20) + 'px')
.text("Time Spent on Assignment (Minutes)")
.style('font-size', 14);
svg.append("g") // y axis
.attr("class", "y axis")
.call(yAxis);
svg.append("text") // y axis label
.attr("class", "y axis")
.attr("transform", "rotate(-90)")
.attr("x", -height / 2)
.attr("dy", "-3em")
.style("text-anchor", "middle")
.text("Students (%)")
.style('font-size', 14);
svg.append("text")// Title
.attr("class", "x axis")
.attr("text-anchor", "middle")
.attr("x", (width / 2))//+300)
.attr("y", 0)
.attr("dy", '-1.5em')
.text("Time Spent by Students on Exercise")
.style('font-size', 20)
.style('text-decoration', 'underline');
svg.append("path")
.datum(minutes_count)
.attr("class", "line")
.attr('id', 'myPath')
.attr("stroke", "var(--bs-warning)")
.attr("stroke-width", 5)
.attr("fill", "none")
.attr("d", line);
}
drawLineGraph();
// DRAW THE SECOND GRAPH ------------------------------------------------------------------------------
function drawBarGraph() {
var groupWidth = 5;
var groupRanges = 0;
var workingTimeGroups = [];
do {
var clusterCount = 0;
for (var i = 0; i < minutes_array.length; i++) {
if ((minutes_array[i] >= groupRanges) && (minutes_array[i] < (groupRanges + groupWidth))) {
clusterCount++;
}
}
workingTimeGroups.push(clusterCount);
groupRanges += groupWidth;
}
while (groupRanges < maximum_minutes);
var clusterCount = 0,
sum = 0,
maxVal = 0;
for (var i = 0; i < minutes_array.length; i++) {
if (minutes_array[i] > maximum_minutes) {
currentValue = minutes_array[i];
sum += currentValue;
if (currentValue > maxVal) {
maxVal = currentValue;
}
clusterCount++;
}
}
// ToDo: Take care of x axis description if this is added
// workingTimeGroups.push(clusterCount);
var maxStudentsInGroup = Math.max.apply(Math, workingTimeGroups);
var width_ratio = .8;
// Scale width to fit into bootsrap container
if (getWidth() * width_ratio > 1000){
width_ratio = 1000 / getWidth();
}
var height_ratio = .7;
var margin = {top: 100, right: 20, bottom: 70, left: 70},
width = (getWidth() * width_ratio) - margin.left - margin.right,
height = (width * height_ratio) - margin.top - margin.bottom;
var x = d3.scaleBand()
.rangeRound([0, width])
.paddingInner(0.1)
.domain(workingTimeGroups.map(function (d, i) {
return i * groupWidth;
}));
var xAxis = d3.axisBottom(x)
.ticks(10)
.tickValues(x.domain().filter(function(d, i){
return (d % 10) === 0
}))
.tickFormat(function(d) { return d + "-" + (d + groupWidth) });
var y = d3.scaleLinear()
.domain([0, maxStudentsInGroup])
.range([height, 0]);
var yAxis = d3.axisLeft(y)
.ticks(10);
var tip = d3.tip()
.attr('class', 'd3-tip')
.offset([-10, 0])
.html(function(_event, d) {
return "<strong>Students: </strong><span style='color:var(--bs-warning)'>" + d + "</span>";
});
var svg = d3.select("#chart_2").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 + ")");
svg.call(tip);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", ".15em")
.attr("transform", function(d) {
return "rotate(-45)"
});
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em");
svg.append("text")
.attr("class", "y axis")
.attr("transform", "rotate(-90)")
.attr("x", -height / 2)
.attr("dy", "-3em")
.style("text-anchor", "middle")
.text("Students")
.style('font-size', 14);
svg.append("text")
.attr("class", "x axis")
.attr("text-anchor", "middle")
.attr("x", width / 2)
.attr("y", height)
.attr("dy", ((height / 20) + 40) + 'px')
.text("Working Time (Minutes)")
.style('font-size', 14);
svg.selectAll(".bar")
.data(workingTimeGroups)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d, i) {
return x(i * groupWidth);
})
.attr("width", x.bandwidth())
.attr("y", function(d) { return y(d); })
.attr("height", function(d) { return height - y(d); })
.on('mouseenter', tip.show)
.on('mouseout', tip.hide);
svg.append("text")
.attr("class", "x axis")
.attr("text-anchor", "middle")
.attr("x", (width / 2))
.attr("y", 0)
.attr("dy", '-1.5em')
.text("Distribution of Time Spent by Students")
.style('font-size', 20)
.style('text-decoration', 'underline');
}
drawBarGraph();
}
});