profile_graphs.js 6.35 KB
Newer Older
1
<%page args="grade_summary, graph_div_id, **kwargs"/>
2 3 4 5 6 7 8 9 10 11 12
<%!
  import json
%>

$(function () {
  function showTooltip(x, y, contents) {
    $('<div id="tooltip">' + contents + '</div>').css( {
      position: 'absolute',
      display: 'none',
      top: y + 5,
      left: x + 5,
13 14 15 16
      border: '1px solid #000',
      padding: '4px 6px',
      color: '#fff',
      'background-color': '#333',
17 18 19 20
      opacity: 0.90
    }).appendTo("body").fadeIn(200);
  }
      
21
  /* -------------------------------- Grade detail bars -------------------------------- */
22
  
23
  <%
24 25
  colors = ["#b72121", "#600101", "#666666", "#333333"]
  
26 27 28 29
  tickIndex = 1
  sectionSpacer = 0.5
  sectionIndex = 0

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
  series = []
  ticks = [] #These are the indices and x-axis labels for the data
  bottomTicks = [] #Labels on the bottom
  detail_tooltips = {} #This an dictionary mapping from 'section' -> array of detail_tooltips
  droppedScores = [] #These are the datapoints to indicate assignments which aren't factored into the total score
  dropped_score_tooltips = []

  for section in grade_summary:
    if 'subscores' in section: ##This is for sections like labs or homeworks, with several smaller components and a total
        series.append({
            'label' : section['category'],
            'data' : [[i + tickIndex, score['percentage']] for i,score in enumerate(section['subscores'])],
            'color' : colors[sectionIndex]
        })
        
        ticks += [[i + tickIndex, score['label'] ] for i,score in enumerate(section['subscores'])]
        bottomTicks.append( [tickIndex + len(section['subscores'])/2, section['category']] )
        detail_tooltips[ section['category'] ] = [score['summary'] for score in section['subscores']]
        
        droppedScores += [[tickIndex + index, 0.05] for index in section['dropped_indices']]
        
        dropExplanation = "The lowest {0} {1} scores are dropped".format( len(section['dropped_indices']), section['category'] )
        dropped_score_tooltips += [dropExplanation] * len(section['dropped_indices'])
        
        
        tickIndex += len(section['subscores']) + sectionSpacer
        
        
        category_total_label = section['category'] + " Total"
        series.append({
            'label' : category_total_label,
61
            'data' : [ [tickIndex, section['totalscore']] ],
62 63 64 65
            'color' : colors[sectionIndex]
        })
        
        ticks.append( [tickIndex, section['totallabel']] )
66
        detail_tooltips[category_total_label] = [section['totalscore_summary']]
67 68 69
    else:
        series.append({
            'label' : section['category'],
70
            'data' : [ [tickIndex, section['totalscore']] ],
71 72 73 74
            'color' : colors[sectionIndex]
        })
        
        ticks.append( [tickIndex, section['totallabel']] )
75
        detail_tooltips[section['category']] = [section['totalscore_summary']]
76 77 78 79 80 81 82 83
        
    tickIndex += 1 + sectionSpacer
    sectionIndex += 1


  detail_tooltips['Dropped Scores'] = dropped_score_tooltips    
    
  ## ----------------------------- Grade overviewew bar ------------------------- ##
84 85 86
  totalWeight = 0.0
  sectionIndex = 0
  totalScore = 0.0
87
  overviewBarX = tickIndex
88
     
89
  for section in grade_summary:
90
      weighted_score = section['totalscore'] * section['weight']
91
      summary_text = "{0} - {1:.1%} of a possible {2:.0%}".format(section['category'], weighted_score, section['weight'])
92
      
93 94
      weighted_category_label = section['category'] + " - Weighted"
         
95
      if section['totalscore'] > 0:
96 97 98 99 100 101 102 103 104
          series.append({
              'label' : weighted_category_label,
              'data' : [ [overviewBarX, weighted_score] ],
              'color' : colors[sectionIndex]
          })
            
      detail_tooltips[weighted_category_label] = [ summary_text ]
      sectionIndex += 1
      totalWeight += section['weight']
105
      totalScore += section['totalscore'] * section['weight']
106 107 108 109
        
  ticks += [ [overviewBarX, "Total"] ]
  tickIndex += 1 + sectionSpacer
  %>
110
  
111 112 113 114 115
  var series = ${ json.dumps(series) };
  var ticks = ${ json.dumps(ticks) };
  var bottomTicks = ${ json.dumps(bottomTicks) };
  var detail_tooltips = ${ json.dumps(detail_tooltips) };
  var droppedScores = ${ json.dumps(droppedScores) };
116
  
117 118
  //Alwasy be sure that one series has the xaxis set to 2, or the second xaxis labels won't show up
  series.push( {label: 'Dropped Scores', data: droppedScores, points: {symbol: "cross", show: true, radius: 3}, bars: {show: false}, color: "#333"} );
119
  
120
  var options = {
121 122
    series: {stack: true,
              lines: {show: false, steps: false },
123
              bars: {show: true, barWidth: 0.8, align: 'center', lineWidth: 0, fill: .8 },},
124
    xaxis: {tickLength: 0, min: 0.0, max: ${tickIndex - sectionSpacer}, ticks: ticks, labelAngle: 90},
125 126
    yaxis: {ticks: [[1, "100%"], [0.87, "A 87%"], [0.7, "B 70%"], [0.6, "C 60%"], [0, "0%"]], min: 0.0, max: 1.0, labelWidth: 50},
    grid: { hoverable: true, clickable: true, borderWidth: 1,
127 128
      markings: [ {yaxis: {from: 0.87, to: 1 }, color: "#ddd"}, {yaxis: {from: 0.7, to: 0.87 }, color: "#e9e9e9"}, 
                  {yaxis: {from: 0.6, to: 0.7 }, color: "#f3f3f3"}, ] },
129 130
    legend: {show: false},
  };
131
  
132
  var $grade_detail_graph = $("#${graph_div_id}");
133 134 135 136
  if ($grade_detail_graph.length > 0) {
    var plot = $.plot($grade_detail_graph, series, options);
    
    var o = plot.pointOffset({x: ${overviewBarX} , y: ${totalScore}});
Piotr Mitros committed
137
    $grade_detail_graph.append('<div style="position:absolute;left:' + (o.left - 12) + 'px;top:' + (o.top - 20) + 'px">${"{totalscore:.0%}".format(totalscore=totalScore)}</div>');
138
  }
139 140
      
  var previousPoint = null;
141
  $grade_detail_graph.bind("plothover", function (event, pos, item) {
142 143 144
    $("#x").text(pos.x.toFixed(2));
    $("#y").text(pos.y.toFixed(2));
    if (item) {
145 146
      if (previousPoint != (item.dataIndex, item.seriesIndex)) {
        previousPoint = (item.dataIndex, item.seriesIndex);
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
            
        $("#tooltip").remove();
            
        if (item.series.label in detail_tooltips) {
          var series_tooltips = detail_tooltips[item.series.label];
          if (item.dataIndex < series_tooltips.length) {
            var x = item.datapoint[0].toFixed(2), y = item.datapoint[1].toFixed(2);
                
            showTooltip(item.pageX, item.pageY, series_tooltips[item.dataIndex]);
          }
        }

      }
    } else {
      $("#tooltip").remove();
      previousPoint = null;            
    }
  });
});