-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrade-distr.js
1369 lines (1160 loc) · 55.1 KB
/
grade-distr.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var gradeDistr = (function() {
var exports = {};
function UpdateHandler() {
var handlers = {};
/*
creates a new listener request
event = event to listen to
callback = function to call in the case of the event
*/
function on(event, callback) {
var callbacks = handlers[event];
if (callbacks === undefined) {
callbacks = [];
}
callbacks.push(callback);
handlers[event] = callbacks;
}
/*
calls all functions that are listeners
event = event that occured
data = data to pass to the callback functions.
*/
function trigger(event, data) {
var callbacks = handlers[event];
if (callbacks !== undefined) {
for (var i = 0; i < callbacks.length; i += 1)
callbacks[i](data);
}
}
return {on: on, trigger: trigger};
}
function Model(){
var handler = UpdateHandler();
var courseware_studentmodule;
// comment out the ajax call if using local host
$.ajax({
url: 'dummymodule.json',
async: false,
dataType: 'json',
success: function (response) {
courseware_studentmodule = response;
}
});
//////////////////////////////////////////////////////
////////////////Need to Manually Change///////////////
//////////////////////////////////////////////////////
//the student module file
// courseware_studentmodule = dummymodule; //Uncomment this line if using local host
//course_id you're interested in
var course_id = "6.813";
//an array of module_types you're interested in visualizing
//possible module_types are "problem", "selfassessment", and "combinedopenended".
var module_type_array = ["problem"];
//////////////////////////////////////////////////////
/////////////////////////END//////////////////////////
//////////////////////////////////////////////////////
var quizzes = edxStudentModuleToMyFormat(courseware_studentmodule, course_id, module_type_array);
//define peopleData
var peopleData = {};
for (var quiz in quizzes){
var sheet = quizzes[quiz];
for (var i = 0; i < sheet.length; i++) {
var row = sheet[i];
var name = row["student_id"];
if (name in peopleData) {
peopleData[name][quiz] = row;
}
else {
peopleData[name]= {};
peopleData[name][quiz] = row;
}
}
}
function getPeopleData(){
return peopleData;
}
function getQuizData(quizname) {
}
function getQuizzesArray() {
var quizzesArray = [];
for (var quiz in quizzes){
quizzesArray.push(quiz);
}
return quizzesArray.sort();
}
/**
Function that takes in the assignment name and
returns an array of the assignment's [total number of people, average, st. dev.]
**/
function getBasicInfo(assignment) {
//calc total num of students who completed the assignment
var numPeople = 0;
var sum = 0;
for (var key in peopleData) {
if (assignment in peopleData[key]) {
numPeople++;
sum += peopleData[key][assignment]["grade"] / peopleData[key][assignment]["max_grade"] * 100;
}
}
//calc average grade
var avr = sum/numPeople;
//calc std dev
var sqDiffSum = 0;
for (var key in peopleData) {
if (assignment in peopleData[key]) {
sqDiffSum += Math.pow(parseFloat(peopleData[key][assignment]["grade"]) / peopleData[key][assignment]["max_grade"] * 100 -avr,2);
}
}
var sd = Math.sqrt(sqDiffSum / numPeople);
return [numPeople, avr, sd];
}
/**
Function that returns an array of
[mean of all students' "avr" attribute, sd of all students' "avr" attributes]
**/
function getInfoOverAll() {
var sum = 0; var numPeople = 0;
for (var person in peopleData) {
sum += peopleData[person]["avr"];
numPeople += 1;
}
var avr = sum / numPeople;
//sd
var sqDiffSum = 0;
for (var person in peopleData) {
sqDiffSum += Math.pow(peopleData[person]["avr"]-avr,2);
}
var sd = Math.sqrt(sqDiffSum / numPeople);
return [avr, sd];
}
/**
Function that returns data needed for d3 stacked bar graph.
assignment the assignment name in String.
bottomPct, topPct in percentage(%).
returns an array of three arrays that represent each layer of the stacked bar graph.
**/
function groupPeopleByAvr(assignment, lowPct, highPct) {
var peopleArray = [];
var bottomArray = [];
var middleArray = [];
var topArray = [];
for (var key in peopleData) {
if (assignment in peopleData[key]) {
peopleArray.push(peopleData[key]);
}
}
//setup sorting function
function orderByAvrAscending(a,b) {
return a["avr"] - b["avr"];
}
peopleArray.sort(orderByAvrAscending);
var totalNumPeople = peopleArray.length;
for (var i = 0; i < totalNumPeople; i++){
if (i < Math.round((totalNumPeople*lowPct)/100)){bottomArray.push(peopleArray[i]);}
else if (i < Math.round((totalNumPeople*highPct)/100)){middleArray.push(peopleArray[i]);}
else {topArray.push(peopleArray[i]);}
}
var threeArrays = [bottomArray,middleArray,topArray];
var newThreeArrays = [[],[],[]];
for (var index = 0; index < threeArrays.length; index++) {
var thisArray = threeArrays[index];
var counter = [];
var groupedPeople = [];
for (var i = 0; i < 10; i++) { //10 is number of bins. There will be 10 bars.
counter.push(0);
groupedPeople.push([]);
}
for (var i = 0; i < thisArray.length; i++){
var grade = Math.floor(thisArray[i][assignment]["grade"] / thisArray[i][assignment]["max_grade"] * 10);
if (grade == 10) {
counter[9] += 1;
groupedPeople[9].push(thisArray[i]);
}
else {
counter[grade] += 1;
groupedPeople[grade].push(thisArray[i]);
}
}
for (var i = 0; i < 10; i++){
newThreeArrays[index].push({"y":counter[i], "people": groupedPeople[i]});
}
}
return newThreeArrays;
}
/**
Function that calculates each student's average grade, and store it as each student's "avr" attribute;
the average grade is averaged over the quizzes in quizzesOfInterest (in this case, all the quizzes).
If a student didn't take the quiz, give him a zero and calculate average.
**/
function calcAverage(quizzesOfInterest) {
quizzesOfInterest = getQuizzesArray();
for (var person in peopleData){
var personData = peopleData[person];
personData["avr"] = 0;
var sum = 0;
for (var i = 0; i < quizzesOfInterest.length; i++){
var quizname = quizzesOfInterest[i];
if (quizname in personData) {
sum += personData[quizname]["grade"] / personData[quizname]["max_grade"] * 100;
}
}
if (quizzesOfInterest.length != 0){
var avr = sum / quizzesOfInterest.length;
personData["avr"] = avr;
}
}
}
/**
Function that calculates each student's average grade, and store it as each student's "avr" attribute;
the average grade is averaged over only the quizzes the students took.
**/
function calcAverage_nozeros() {
var quizzesOfInterest = getQuizzesArray();
for (var person in peopleData){
var personData = peopleData[person];
personData["avr"] = 0;
var sum = 0;
var numQuizzes = 0;
for (var i = 0; i < quizzesOfInterest.length; i++){
var quizname = quizzesOfInterest[i];
if (quizname in personData) {
numQuizzes += 1;
sum += personData[quizname]["grade"] / personData[quizname]["max_grade"] * 100;
}
}
var avr = sum / numQuizzes;
personData["avr"] = avr;
}
}
function updateAvrScatterPlot(quizname, parentDiv, outerWidth, outerHeight, margin, isSmall) {
handler.trigger('avr mode', [quizname,parentDiv,outerWidth,outerHeight,margin,isSmall]);
}
function drawAllBarGraphs() {
handler.trigger('view all bar');
}
function drawAllRankScatterPlots() {
handler.trigger('view all rank scatter');
}
function drawAllAvrScatterPlots() {
handler.trigger('view all avr scatter');
}
return {getInfoOverAll: getInfoOverAll, updateAvrScatterPlot: updateAvrScatterPlot, getPeopleData: getPeopleData, getQuizData: getQuizData, getQuizzesArray: getQuizzesArray, getBasicInfo: getBasicInfo, calcAverage: calcAverage, calcAverage_nozeros: calcAverage_nozeros, groupPeopleByAvr: groupPeopleByAvr, drawAllBarGraphs: drawAllBarGraphs, drawAllAvrScatterPlots: drawAllAvrScatterPlots, drawAllRankScatterPlots: drawAllRankScatterPlots, on: handler.on};
}
function Controller(model){
function drawAllBarGraphs() {
model.drawAllBarGraphs();
}
function updateAvrScatterPlot(quizname, parentDiv, outerWidth, outerHeight, margin, isSmall) {
model.updateAvrScatterPlot(quizname, parentDiv, outerWidth, outerHeight, margin, isSmall);
}
function drawAllAvrScatterPlots() {
model.drawAllAvrScatterPlots();
}
function drawAllRankScatterPlots() {
model.drawAllRankScatterPlots();
}
function getPeopleData() {
model.getPeopleData();
}
function getQuizData() {
model.getQuizData();
}
function getQuizzesArray() {
return model.getQuizzesArray();
}
function getBasicInfo(assignment) {
return model.getBasicInfo(assignment);
}
function calcAverage(selectedQuizzes) {
model.calcAverage(selectedQuizzes);
}
function groupPeopleByAvr(assignment, lowPct, highPct) {
return model.groupPeopleByAvr(assignment, lowPct, highPct);
}
return {drawAllBarGraphs: drawAllBarGraphs, updateAvrScatterPlot: updateAvrScatterPlot, drawAllAvrScatterPlots: drawAllAvrScatterPlots, drawAllRankScatterPlots: drawAllRankScatterPlots, getPeopleData: getPeopleData, getQuizData: getQuizData, getQuizzesArray: getQuizzesArray, getBasicInfo: getBasicInfo, calcAverage: calcAverage, groupPeopleByAvr: groupPeopleByAvr};
//updateRankScatterPlot: updateRankScatterPlot,
}
function View(div, model, controller){
div.append(
'<div class="container">'
+ '<div class = "assignment-row">'
+ '<div class = "btn-group">'
+ '<button type = "button" class="btn btn-default btn-l"><span class="glyphicon glyphicon-chevron-left"></span></button>'
+ '<div class = "btn-group">'
+ '<button id="asgn-nav" type = "button" class="btn btn-default dropdown-toggle" data-toggle = "dropdown">Quiz 21<span class="caret"></span></button>'
+ '<ul class="dropdown-menu">'
+ '</ul>'
+ '</div>'
+ '<button type = "button" class="btn btn-default btn-r"><span class="glyphicon glyphicon-chevron-right"></span></button>'
+ '</div>'
+'</div>'
+'<div class = "body-content">'
+ '<div class = "row">'
+ '<div class="col-lg-8" id="column1">'
// + '<div class="chart-container"></div>'
+ '</div>'
+ '<div class="col-lg-4" id="column2"></div>'
+ '</div>'
+'</div>'
);
// COLUMN 2
//slider
var sliderDiv = $('<div id="sliderbg">');
var sliderObj = $('<div id="slider">');
//basic info
var legend = $('<div id="legend"></div>');
var totalLabel = $('<div id="total"><div>Number of Students: </div><p></p></div>');
var averageLabel = $('<div id="average"><div style="display: inline;">Average Grade: </div><p></p></div>');
var sdLabel = $('<div id="sd"><div style="display: inline;">Standard Deviation of Grades: </div><p></p></div>');
legend.append(totalLabel, averageLabel, sdLabel);
var lowerLabel = $('<div id="lowerLabel"></div>');
var upperLabel = $('<div id="upperLabel"></div>');
$('#column2').append(sliderDiv, legend); // legend, checkboxes
$('#sliderbg').append(sliderObj,lowerLabel, upperLabel);
$('ui-slider-range ui-widget-header ui-corner-all').css("background", "darkgray");
$("#lowerLabel").text(25);
$("#lowerLabel").css('top',"-10px");
$("#lowerLabel").css('left', String(0.01 * 25 * parseFloat($('#slider').css("width")) - 0.5 * parseFloat($("#lowerLabel").css("width")))+"px");
$("#upperLabel").text(75);
$("#upperLabel").css('top',"-10px");
$("#upperLabel").css('left', String(0.01 * 75 * parseFloat($('#slider').css("width")) - parseFloat($("#lowerLabel").css("width")) - 0.5 * parseFloat($("#upperLabel").css("width")))+"px");
function updateColors(values) {
var colorstops = colors[0] + ", "; // start left with the first color
for (var i=0; i< values.length; i++) {
colorstops += colors[i] + " " + values[i] + "%,";
colorstops += colors[i+1] + " " + values[i] + "%,";
}
// end with the last color to the right
colorstops += colors[colors.length-1];
/* Safari 5.1, Chrome 10+ */
var css = '-webkit-linear-gradient(left,' + colorstops + ')';
var firefoxCss = '-moz-linear-gradient(left,' + colorstops + ')';
var ieCss = '-ms-linear-gradient(left,' + colorstops + ')';
$('#slider').css('background-image', firefoxCss);
if(internetExplorer) {
console.log("IE? ",internetExplorer);
$('#slider').css('background-image', ieCss);
}
sliderObj.css('background-image', css);
}
var handlers = [25, 75]
var colors = ["lightpink", "darkgray", "lightblue"];
updateColors(handlers);
sliderObj.slider({
range: true,
min: 0,
max: 100,
values: handlers,
slide: function( event, ui ) {
var min = 0;
var max = 100;
var bottom = Math.abs(ui.values[0] - min);
var mid = Math.abs(ui.values[1]-ui.values[1]);
var top = Math.abs(max - ui.values[1]);
$("#lowerLabel").text(bottom);
$("#lowerLabel").css('top',"-10px");
$("#lowerLabel").css('left', String(0.01 * bottom * parseFloat($('#slider').css("width")) - 0.5 * parseFloat($("#lowerLabel").css("width")))+"px");
$("#upperLabel").text(max-top);
$("#upperLabel").css('top',"-10px");
$("#upperLabel").css('left', String(0.01 * (max-top) * parseFloat($('#slider').css("width")) - parseFloat($("#lowerLabel").css("width")) - 0.5 * parseFloat($("#upperLabel").css("width")))+"px");
updateColors(ui.values);
var currentQuiz = $('#asgn-nav').text();
if (currentQuiz != "ViewAll") {updateBarGraph([controller.groupPeopleByAvr($('#asgn-nav').text(), bottom, max-top),$('#asgn-nav').text()]);}
else { //when clicks View All
controller.drawAllBarGraphs();
}
}
}).slider('pips', {
first: 'label',
last: 'label',
rest: false,
});
var bottom = Math.abs(sliderObj.slider("values", 0) - 0);
var middle = Math.abs(sliderObj.slider("values", 1) - sliderObj.slider("values", 0));
var top = Math.abs(100 - sliderObj.slider("values", 1));
function displayBasicInfo(assignment) {
var info = controller.getBasicInfo(assignment);
$('#total p').text(info[0]);
$('#average p').text(info[1].toFixed(1));
$('#sd p').text(info[2].toFixed(1));
}
displayBasicInfo("Quiz 21");
//MODE BUTTONS
var modes = $('<ul class="nav nav-tabs"></ul>');
var leftButton = $('<li class="active"><a href="#">Percentiles</a></li>');
var rightButton = $('<li><a href="#">Normalized</a></li>');
modes.append(leftButton, rightButton);
modes.css('padding', 'auto auto');
$('#column2').prepend(modes);
var modeBools = [true, false, false];
leftButton.on('click', function() {
leftButton.attr("class","active");
rightButton.attr("class","");
modeBools[0] = true;
modeBools[1] = false;
modeBools[2] = false;
sliderDiv.show();
var quizname = $('#asgn-nav').text();
if (quizname == "ViewAll") {
controller.drawAllBarGraphs();
hoverInfos();
}
else {
controller.calcAverage();
bottom = Math.abs(sliderObj.slider("values", 0) - 0);
top = Math.abs(100 - sliderObj.slider("values", 1));
controller.groupPeopleByAvr(quizname, bottom, 100-top);
displayBasicInfo(quizname);
drawInitialGraph(quizname, "#column1", bar_outerWidth, bar_outerHeight, bar_margin, 0, false);
}
});
rightButton.on('click', function() {
rightButton.attr("class","active");
leftButton.attr("class","");
modeBools[0] = false;
modeBools[1] = false;
modeBools[2] = true;
sliderDiv.hide();
var quizname = $('#asgn-nav').text();
if (quizname == "ViewAll") {
controller.drawAllAvrScatterPlots();
hoverInfos();
}
else {
displayBasicInfo(quizname);
controller.updateAvrScatterPlot(quizname,'#column1',avr_outerWidth,avr_outerHeight,avr_margin,false);
}
})
//NAVIGATION
var dropdown = $('.dropdown-menu');
for (var i = 0; i < model.getQuizzesArray().length; i++) {
var key = model.getQuizzesArray()[i];
var link = $('<li id="' + key + '"><a>' + key + '</a></li>');
dropdown.append(link);
}
dropdown.append("<li id='ViewAll'><a>View All</a></li>");
////////
//setup variables for Graph
////////
//variables for bar graph
var bar_outerWidth = parseInt($('#column1').css("width"))-parseInt($('#column1').css("padding-left"))-parseInt($('#column1').css("padding-right"));
var bar_outerHeight = $(document).height()-$('.assignment-row').height()-parseInt($('.container').css("margin-top"))-parseInt($('#column1').css("padding-top"));
var bar_margin = { top: 20, right: 20, bottom: 40, left: 40 };
var bar_chartWidth = bar_outerWidth - bar_margin.left - bar_margin.right;
var bar_chartHeight = bar_outerHeight - bar_margin.top - bar_margin.bottom;
var bar_color_scale = d3.scale.ordinal().range(["lightpink", "darkgray", "lightblue"]);
//variables for avr scatter
var avr_outerWidth = parseInt($('#column1').css("width"))-parseInt($('#column1').css("padding-left"))-parseInt($('#column1').css("padding-right"));
var avr_outerHeight = $(document).height()-$('.assignment-row').height()-parseInt($('.container').css("margin-top"))-parseInt($('#column1').css("padding-top"));
var avr_margin = { top: 20, right: 20, bottom: 60, left: 50 };
//"#column1", bar_outerWidth, bar_outerHeight, bar_margin
function drawInitialGraph(quizname, parentDiv, outerWidth, outerHeight, margin, maxY, isSmall) { //assignment, quizzesOfInterest, lowPct, highPct,
var chartWidth = outerWidth - margin.left - margin.right;
var chartHeight = outerHeight - margin.top - margin.bottom;
if (!isSmall) {
$('#column1').children().remove();
$('#legend').show();
}
//INITIALIZE THE CHART
var chart = d3.select(parentDiv)
.append("svg")
.attr("class", "chart")
.attr("height", outerHeight)
.attr("width", outerWidth)
.append("g")
.attr("class", "innerChart")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
//Y-AXIS LABEL
if (!isSmall) {
chart.append("text")
.attr("class", "yaxis-label")
.attr("x",-chartHeight/2)
.attr("y", 0)
.attr("transform", function(d) {return "rotate(-90)" })
.attr("dy", -margin.left*0.75)
.attr("font-weight", "bold")
.attr("text-anchor", "middle")
.text("Number of Students");
}
controller.calcAverage(quizname);
var data = model.groupPeopleByAvr(quizname, bottom, 100-top); //need to ask question
// console.log('data: ', data);
var stack = d3.layout.stack();
var stackedData = stack(data);
var yStackMax = d3.max(stackedData, function(layer) {
return d3.max(layer, function(d) { return d.y + d.y0; })
});
var xScale = d3.scale.ordinal()
.domain(d3.range(data[0].length)).rangeBands([0, chartWidth]);
if (isSmall){yStackMax = maxY;}
var yScale = d3.scale.linear()
.domain([0, yStackMax]).range([chartHeight, 0]);
// Y AXIS GRID LINES
chart.selectAll("line").data(yScale.ticks(10))
.enter().append("line")
.attr("x1", 0)
.attr("x2", chartWidth)
.attr("y1", yScale)
.attr("y2", yScale);
//Y TICK MARKS
var yscale_label = chart.selectAll(".yscale-label").data(yScale.ticks(10))
.enter().append("text")
.attr("x", 0)
.attr("y", yScale)
.attr("dx", -margin.left/8)
.attr("text-anchor", "end")
.attr("dy", "0.3em")
.text(String);
if (!isSmall) {
yscale_label.attr("class", "yscale-label")
}else {
yscale_label.attr("class", "small-yscale-label")
}
//X TICK MARKS
var xscale_label = chart.selectAll(".xscale-label").data(xScale.domain())
.enter().append("text")
.attr("x", function(d){return xScale(d);})
.attr("y", chartHeight)
.attr("text-anchor", "center")
// .attr("dx", function(d){return xScale.rangeBand()/2;})
.attr("dy", margin.bottom*0.4)
.text(function(d){return String(d*10);});
if (!isSmall) {
xscale_label.attr("class", "xscale-label");
}else {
xscale_label.attr("class", "small-xscale-label");
}
//X-AXIS LABEL
if (!isSmall) {
chart.append("text")
.attr("class", "xaxis-label")
.attr("x", chartWidth/2)
.attr("y", chartHeight)
.attr("dy", margin.bottom*0.85)
.attr("font-weight", "bold")
.attr("text-anchor", "middle")
.text("Grade for " + quizname + " (%)");
}
//grabs all the layers and forms groups out of them
var layerGroups = chart.selectAll(".layer").data(stackedData)
.enter().append("g")
.attr("class", "layer")
.style("fill", function(d,i){return bar_color_scale(i);});
//THE BARS (FOR STACKED BAR CHARTS)
var rects = layerGroups.selectAll("rect").data(function(d) { return d; })
.enter().append("rect")
.attr("x", function(d, i) { return xScale(i); })
.attr("y", function(d) { return yScale(d.y + d.y0) })
.attr("width", xScale.rangeBand())
.attr("height", function(d) { return yScale(d.y0) - yScale(d.y0 + d.y) });
// Title
if (isSmall){
chart.append("text")
.attr("class","title")
.attr("x", chartWidth/2)
.attr("y", chartHeight*0.03)
.text(quizname);
}
}
drawInitialGraph($('#asgn-nav').text(),"#column1", bar_outerWidth, bar_outerHeight, bar_margin, 0, false);
function updateBarGraph(dataArray){
var quizname = dataArray[1];
var data = dataArray[0];
var stack = d3.layout.stack();
var stackedData = stack(data);
var yGroupMax = d3.max(stackedData, function(layer) {
return d3.max(layer, function(d) { return d.y; })
});
var yStackMax = d3.max(stackedData, function(layer) {
return d3.max(layer, function(d) { return d.y + d.y0; })
});
var xScale = d3.scale.ordinal()
.domain(d3.range(data[0].length)).rangeBands([0, bar_chartWidth]);
var yScale = d3.scale.linear()
.domain([0, yStackMax]).range([bar_chartHeight, 0]);
// Y AXIS GRID LINES
var chart = d3.select('g');
if (chart.selectAll("line")[0].length > yScale.ticks(10).length){
chart.selectAll("line").data(yScale.ticks(10))
.exit()
.remove();
chart.selectAll("line").data(yScale.ticks(10))
.transition()
.attr("x1", 0)
.attr("x2", bar_chartWidth)
.attr("y1", yScale)
.attr("y2", yScale)
}
else {
chart.selectAll("line").data(yScale.ticks(10))
.enter().append("line");
chart.selectAll("line")
.transition()
.attr("x1", 0)
.attr("x2", bar_chartWidth)
.attr("y1", yScale)
.attr("y2", yScale)
}
//Y TICK MARKS
chart.selectAll(".yscale-label").remove();
chart.selectAll(".yscale-label").data(yScale.ticks(10))
.enter().append("text")
.attr("class", "yscale-label")
.attr("x", 0)
.attr("y", yScale)
.attr("dx", -bar_margin.left/8)
.attr("text-anchor", "end")
.attr("dy", "0.3em")
.text(String);
//X TICK MARKS
chart.selectAll(".xscale-label").remove();
chart.selectAll(".xscale-label").data(xScale.domain())
.enter().append("text")
.attr("class", "xscale-label")
.attr("x", function(d){return xScale(d);})
.attr("y", bar_chartHeight)
.attr("text-anchor", "center")
// .attr("dx", function(d){return xScale.rangeBand()/2;})
.attr("dy", bar_margin.bottom*0.4)
.text(function(d){return String(d*10);});
//X-AXIS LABEL
$('.xaxis-label').remove();
chart.append("text")
.attr("class", "xaxis-label")
.attr("x",bar_chartWidth/2)
.attr("y", bar_chartHeight)
.attr("dy", bar_margin.bottom*0.85)
.attr("font-weight", "bold")
.attr("text-anchor", "middle")
.text("Grade for " + quizname + " (%)");
//grabs all the layers and forms groups out of them
var layerGroups = chart.selectAll(".layer").data(stackedData)
.style("fill", function(d,i){return bar_color_scale(i);});
//THE BARS (FOR STACKED BAR CHARTS)
var rects = layerGroups.selectAll("rect").data(function(d) { return d; })
.transition().duration(500)
.attr("x", function(d, i) { return xScale(i); })
.attr("y", function(d) { return yScale(d.y + d.y0) })
.attr("width", xScale.rangeBand())
.attr("height", function(d) { return yScale(d.y0) - yScale(d.y0 + d.y) });
}
function drawAllBarGraphs(){
$('#legend').hide();
$('#column1').children().remove();
var quizzesArray = controller.getQuizzesArray();
var numCols = 3;
var numRows = Math.ceil(quizzesArray.length/numCols);
for (var i = 0; i < numRows; i++) {
var thisrow = $("<div class='bar-row-"+i+"'></div>");
for (var j=0; j < numCols; j++) {
if (i*numCols+j < quizzesArray.length) {
thisrow.append("<div class='col-lg-4 bar-col-"+j+"'></div>");
}
}
$('#column1').append(thisrow);
}
//calculate absolute max y value of all plots
var overallMaxYStack = 0;
for (var i = 0; i < numRows; i++) {
for (var j=0; j < numCols; j++) {
if (i*numCols+j < quizzesArray.length) {
var thisQuiz = quizzesArray[i*numCols+j];
controller.calcAverage(thisQuiz);
bottom = Math.abs(sliderObj.slider("values", 0) - 0);
top = Math.abs(100 - sliderObj.slider("values", 1));
var data = model.groupPeopleByAvr(thisQuiz, bottom, 100-top); //need to ask question
var stack = d3.layout.stack();
var stackedData = stack(data);
var yStackMax = d3.max(stackedData, function(layer) {
return d3.max(layer, function(d) { return d.y + d.y0; })
});
if (yStackMax > overallMaxYStack) {overallMaxYStack = yStackMax;}
}
}
}
//iterate through each grid div and append a bar graph to each
for (var i = 0; i < numRows; i++) {
for (var j=0; j < numCols; j++) {
if (i*numCols+j < quizzesArray.length) {
var quizname = quizzesArray[i*numCols+j];
var parentDiv = ".bar-row-"+i+" .bar-col-"+j;
var outerWidth = parseInt($('.bar-row-'+i+' .bar-col-'+j).css("width"));
var outerHeight = $(document).height()/3;
var margin = { top: 10, right: 5, bottom: 20, left: 10};
drawInitialGraph(quizname, parentDiv, outerWidth, outerHeight, margin, overallMaxYStack, true);
}
}
}
//x and y axes labels
var allbars_xaxislabel = d3.select('#column1').append("div")
.attr("class", "viewall-xaxislabel")
.text("Grade for Each Quiz (%)");
// allbars_xaxislabel.attr();
d3.select('#column1').append("div")
.attr("class", "viewall-yaxislabel")
.text("Number of Students");
}
function updateAvrScatterPlot(quizname, parentDiv, outerWidth, outerHeight, margin, isSmall) {
var chartWidth = outerWidth - margin.left - margin.right;
var chartHeight = outerHeight - margin.top - margin.bottom;
if (!isSmall) {
$('#column1').children().remove();
$('#legend').show();
} //remove previous existing svg if drawing big plot
model.calcAverage_nozeros();
var dataset = [];
var dataDict = model.getPeopleData();
for (var person in dataDict) {
if (quizname in dataDict[person]){
dataset.push({"student_id": person ,"grade": dataDict[person][quizname]["grade"]/dataDict[person][quizname]["max_grade"]*100, "avr": dataDict[person]["avr"]});
}
}
var tooltip = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
var info = model.getBasicInfo(quizname);
var infoOverall = model.getInfoOverAll();
var avrOverall = infoOverall[0];
var sdOverall = infoOverall[1];
var maxgradezscore = d3.max(dataset, function(d){return Math.abs(d["grade"]-info[1])/info[2];});
var maxavrzscore = d3.max(dataset, function(d){return Math.abs(d["avr"]-avrOverall)/sdOverall;});
var maxzscore = Math.max(maxavrzscore, maxgradezscore);
var xScale = d3.scale.linear() //scale is a function!!!!!
.domain([(avrOverall-maxzscore*sdOverall),(avrOverall+maxzscore*sdOverall)])
.range([margin.left,outerWidth-margin.right])
.clamp(true);
var yScale = d3.scale.linear() //scale is a function!!!!!
//.domain([Math.max(0,(info[1]-d3.max(dataset, function(d){return Math.abs(info[1]-d["grade"]);}))),Math.min(100,10*(info[1]+d3.max(dataset, function(d){return Math.abs(info[1]-d["grade"]);})))])
.domain([info[1]-maxzscore*info[2],info[1]+maxzscore*info[2]])
.range([outerHeight-margin.bottom,margin.top])
.clamp(true);
var xtoyScale = d3.scale.linear()
.domain([(avrOverall-3*sdOverall),(avrOverall+3*sdOverall)])
.range([(info[1]-3*info[2]),(info[1]+3*info[2])]);
var xaxisData = [];
var yaxisData = [];
for (var i = -2; i <= 2; i++) {
xaxisData.push(Math.max(Math.min((avrOverall + i * sdOverall),100),0));
//yaxisData.push(Math.max(Math.min((info[1] + i * info[2])*10,100),0));
yaxisData.push((info[1] + i * info[2]));
}
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom")
.tickValues(xaxisData)
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left")
.tickValues(yaxisData)
var svg = d3.select(parentDiv).append("svg")
.attr("class", "chart")
.attr("width",outerWidth)
.attr("height",outerHeight);
//diagonal line
var diag = svg.append("line")
.attr("class","diagonal")
.attr("id", "diagonal");
var xmin = xScale.domain()[0];
var xmax = xScale.domain()[1];
var ymin = yScale.domain()[0];
var ymax = yScale.domain()[1];
if (xtoyScale(xmin) < ymin) {
diag.attr("x1", xScale(xtoyScale.invert(ymin)));
diag.attr("y1", yScale(ymin));
}
else {
diag.attr("x1", xScale(xmin));
diag.attr("y1", yScale(xtoyScale(xmin)));
}
if (xtoyScale(xmax) < ymax) {
diag.attr("x2", xScale(xmax));
diag.attr("y2", yScale(xtoyScale(xmax)));
}
else {
diag.attr("x2", xScale(xtoyScale.invert(ymax)));
diag.attr("y2", yScale(ymax));
}
//help text only when drawing big plot
if (!isSmall) {
//help text
var helptext = svg.append("g")
.attr("class", "helptext");
var x = $('.diagonal').attr("x2");
var y = $('.diagonal').attr("y2");
helptext.append("text")
.attr("x", x)
.attr("y", y)
.attr("dx", -chartWidth/10)
.attr("dy", chartHeight/30)
.text("better than")
.append("tspan")
.text("usual")
.attr("x",x)
.attr("dx", -chartWidth/10)
.attr("dy", 12);
helptext.append("text")
.attr("x", x)
.attr("y", y)
.attr("dx", -0.5 * $('text').width())
.attr("dy", 0.15*chartHeight)
.text("worse than")
.append("tspan")
.text("usual")
.attr("x", x)
.attr("dx", -0.5 * $('text').width())
.attr("dy", 12);
}
//max line
svg.append("line")
.attr("class","maxline")
.attr("x1",xScale.range()[0])
.attr("y1",yScale(100+0.5))
.attr("x2",xScale.range()[1])
.attr("y2",yScale(100+0.5));
//max label
var maxlabel = svg.append("text")
.attr("x",xScale.range()[0])
.attr("y",yScale(100+0.5))
.attr("dx",5)
.attr("dy",-5)
.text("max possible grade")
if (!isSmall) {
maxlabel.attr("class", "maxlabel")
}else {
maxlabel.attr("class","small-maxlabel")
}
//x and y axes for big plot
if (!isSmall) {
//xaxis
svg.append("g")
.attr("class","axis")
.attr("transform", "translate(0,"+(outerHeight-margin.bottom)+")")
.call(xAxis);
//yaxis
svg.append("g")
.attr("class","axis")
.attr("transform", "translate("+margin.left+",0)")
.call(yAxis);
}
//x and y axes for mini plots
else {
//xaxis
svg.append("g")
.attr("class","small-axis")
.attr("transform", "translate(0,"+(outerHeight-margin.bottom)+")")
.call(xAxis);
//yaxis
svg.append("g")