-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprogress-donut-chart.js
397 lines (346 loc) · 12.7 KB
/
progress-donut-chart.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
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
(function() {
// build up skeleton for rendering
var selector = d3.select("#progress"),
svgSide = selector[0][0].offsetWidth,
r = svgSide * 4 / 9,
bandWidth = r / 4;
// init responsive svg
var rootG = selector.append("svg")
.attr("width", "100%")
.attr("height", "100%")
.attr("viewBox", "0 0 " + svgSide + " " + svgSide)
.attr("preserveAspectRatio", "xMinYMin")
.append("g")
.attr("transform", "translate(" + svgSide / 2 + "," + svgSide / 2 + ")");
// init an equally segmented pie layout
var pie = d3.layout.pie()
.value(function() {
return 1;
});
// color scale for peer comparison
var color = d3.scale.linear()
.domain([-1, 0, 1])
.range(["#D9321D", "#FFF", "#45D91D"]);
// init default report group
var defaultReportG = rootG.append("g")
.attr("class", "default-report");
// clickable area and help text for going back
var backG = rootG.append("g")
.attr("class", "go-back");
backG.append("circle")
.attr("r", r - bandWidth)
.attr("opacity", 0);
var helpText = backG.append("text")
.attr("transform", "translate(0," + (-bandWidth * 2.5) + ")")
.attr("opacity", 0)
.attr("class", "back-button")
.text("<");
d3.json("data/structure.json", function(structure) {
d3.json("data/students.json", function(data) {
// add some helper methods for data objects
structure.getParent = function(label) {
return this[category].parent[label];
};
structure.getChildren = function(label) {
return this[category].children[label];
};
structure.checkThenRun = function(label) {
// if label is a possible level, then run nextstep function, else then do nothing
if (label in this[category].children) {
return function(nextstep) {
return nextstep(label);
};
} else {
return function(nextstep) {};
}
};
data.getIDs = function(filter) {
return Object.keys(this).filter(filter);
};
data.getPeerData = function(peerType) {
return this[peerType][category];
};
data.getStudentData = function(id) {
return this[id][category];
};
// init selectbox for peer type
d3.select("#select-peer")
.selectAll("option")
.data(data.getIDs(isPeerType).sort())
.enter()
.append("option")
.attr("value", function(d) {
return d;
})
.text(function(d) {
return peerTypeToTitleText(d);
});
var category = d3.select("#select-category").property("value");
var studentID = d3.select("#select-student").property("value");
var peerType = d3.select("#select-peer").property("value");
var studentData = data.getStudentData(studentID);
var peerData = data.getPeerData(peerType);
// rendering starts from 'overall' level
render("overall");
function render(label, isFGNotAnimated) {
var donut = studentData.donut[label];
var donutAvg = peerData.donut[label];
var report = studentData.report[label];
var reportAvg = peerData.report[label];
var durationNormal = 1000;
var durationShort = durationNormal / 2;
// if donut is array then generate default report, else then clear chart
if (donut && donut.constructor === Array) {
// remove old report
defaultReportG.selectAll("text").remove();
// default report - title
defaultReportG.append("text")
.attr("transform", "translate(0," + (-bandWidth * 1.5) + ")")
.attr("class", "report-title")
.text(label.toUpperCase());
// default report - percentage (for both exercise and video)
defaultReportG.append("text")
.attr("class", "percentage")
.classed("category-" + category, true)
.call(showPercentage, report, isFGNotAnimated);
// default report - peer comparison
var diff = report - reportAvg;
defaultReportG.append("text")
.attr("class", "report-comparison")
.text(generateComparisonText(diff))
.attr("fill", color(darkerRange(diff)))
.call(slideOut, "0," + bandWidth * 1.5);
} else {
donut = [];
}
// init arc groups
var arcs = rootG.selectAll(".arc")
.data(pie(donut)); // get angles from pie layout
arcs.enter()
.append("g")
.attr("class", "arc");
// background arcs
var arcBG = d3.svg.arc()
.innerRadius(r - bandWidth)
.outerRadius(r);
arcs.append("path")
.attr("class", "arc-bg")
.attr("d", arcBG);
// foreground arcs for visualize current progress
var arcFG = d3.svg.arc()
.innerRadius(function(d) {
return r - d.data * bandWidth;
})
.outerRadius(r);
arcs.append("path")
.attr("class", "arc-fg")
.classed("category-" + category, true)
.call(showArcsFG, isFGNotAnimated);
// colored outer arcs for peer comparison
var arcComparison = d3.svg.arc()
.innerRadius(r)
.outerRadius(r + bandWidth * 0.1);
arcs.append("path")
.attr("class", "arc-comparison")
.attr("opacity", 0)
.attr("fill", function(d, i) {
return color(darkerRange(d.data - donutAvg[i]));
})
.attr("d", arcComparison);
// arc report
arcs.append("text")
.call(hideToArcCentroid)
.attr("class", "report-title")
.text(function(d, i) {
return structure.getChildren(label)[i].toUpperCase();
});
// arc report - percentage
arcs.append("text")
.call(hideToArcCentroid) //"translate(0,0)"
.attr("class", "percentage")
.classed("category-" + category, true)
.text(function(d) {
return valToPercentString(d.data);
});
// arc report - peer comparison
arcs.append("text")
.call(hideToArcCentroid)
.attr("class", "report-comparison")
.attr("fill", function(d, i) {
return color(darkerRange(d.data - donutAvg[i]));
})
.text(function(d, i) {
return generateComparisonText(d.data - donutAvg[i]);
});
// clear old data
arcs.exit().remove();
// rerender when chart options change
d3.select("#select-category").on("change", function() {
category = this.value;
studentData = data.getStudentData(studentID);
update(label);
});
d3.select("#select-student").on("change", function() {
studentID = this.value;
studentData = data.getStudentData(studentID);
update(label);
});
d3.select("#select-peer").on("change", function() {
peerType = this.value;
peerData = data.getPeerData(peerType);
update(label, true);
});
// show corresponding report & highlight hovered arc when hovering an arc *path*
arcs
.on("mouseover", function() {
arcHover(this, 0.7, 0, 1);
})
.on("mouseout", function() {
arcHover(this, 1, 1, 0);
});
// postpone mouseover events after default-report transition
arcs.attr('pointer-events', 'none')
.transition()
.duration(durationNormal + durationShort * 1.5)
.transition()
.attr('pointer-events', 'auto');
// zoom in when clicking an arc
arcs.on("click", function(d, i) {
structure.checkThenRun(structure.getChildren(label)[i])(update);
});
// show help text for going back when hovering inner circle and being able to go back
backG.on("mouseover", function() {
structure.checkThenRun(structure.getParent(label))(showHelpText);
})
.on("mouseout", hideHelpText);
// zoom out when clicking inner circle
backG.on("click", function() {
structure.checkThenRun(structure.getParent(label))(update);
});
// animation helpers
function showPercentage(selection, val, isNotAnimated) {
if (isNotAnimated) {
selection.text(valToPercentString(val));
} else {
selection.text("0%")
.transition()
.duration(durationNormal)
.tween("text", function() {
var i = d3.interpolate(0, val);
return function(t) {
this.textContent = valToPercentString(i(t));
};
});
}
}
function showArcsFG(selection, isNotAnimated) {
if (isNotAnimated) {
selection.attr("d", arcFG);
} else { // foreground arcs transite from outerRadius to innerRadius
selection.attr("d", d3.svg.arc()
.innerRadius(r)
.outerRadius(r))
.transition()
.duration(durationNormal)
.attrTween("d", function(d) {
var dStart = JSON.parse(JSON.stringify(d));
d.data = 0;
var i = d3.interpolateObject(d, dStart);
return function(t) {
return arcFG(i(t));
};
});
}
}
function slideOut(selection, translation) {
selection.attr("opacity", 0)
.transition()
.delay(isFGNotAnimated ? 0 : durationNormal)
.duration(durationShort)
.attr("transform", "translate(" + translation + ")")
.attr("opacity", 1);
}
function hideToArcCentroid(selection) {
selection.attr("transform", function(d) {
var xy = arcBG.centroid(d);
return "translate(" + xy[0] + "," + xy[1] + ") " + "scale(0.1)";
})
.attr("opacity", 0);
}
function hideToArcCentroidAnimated(selection) {
selection.transition()
.duration(durationShort)
.call(hideToArcCentroid);
}
function showArcReport(selection, translation) {
selection.transition()
.duration(durationShort)
.attr("transform", "translate(" + translation + ") " + "scale(1)")
.attr("opacity", 1);
}
function arcHover(arcNode, opacityArc, opacityDefaultReport, opacityArcReport) {
var currentSelector = d3.select(arcNode).attr("opacity", opacityArc);
currentSelector.select(".arc-comparison").attr("opacity", opacityArcReport);
defaultReportG.selectAll("text")
.transition()
.duration(durationShort)
.attr("opacity", opacityDefaultReport);
if (opacityArcReport === 1) {
currentSelector.select(".report-title")
.call(showArcReport, "0," + (bandWidth * -1.5));
currentSelector.select(".report-comparison")
.call(showArcReport, "0," + (bandWidth * 1.5));
currentSelector.select(".percentage")
.call(showArcReport, "0,0");
} else {
currentSelector.selectAll("text")
.call(hideToArcCentroidAnimated);
}
}
}
// helpers
function update(label, isFGNotAnimated) {
render(null); // clean previous data to avoid data collision
render(label, isFGNotAnimated);
if (label === "overall") {
hideHelpText();
}
}
function hideHelpText() {
return helpText.attr("opacity", 0);
}
function showHelpText() {
return helpText.attr("opacity", 0.2);
}
function valToPercentString(val) {
return Math.abs(Math.floor(val * 100)) + "%";
}
function generateComparisonText(val) {
return val >= 0 ? valToPercentString(val) + " ahead of peers" : valToPercentString(val) + " behind peers";
}
function darkerRange(val) {
return val >= 0 ? val / 2 + 0.5 : val / 2 - 0.5;
}
function isStudentID(str) {
return !isNaN(str);
}
function isPeerType(str) {
return isNaN(str) && str.indexOf('get') < 0;
}
function peerTypeToTitleText(peerType) {
switch (peerType) {
case "avg":
return "all students";
case "top10_problem":
return "10 students completed most problems";
case "top10_video":
return "10 students completed most videos";
case "top10_active":
return "10 most active students";
case "top10_timespent":
return "10 students spent most time";
}
}
});
});
})();