n@566: /* n@566: * Analysis script for WAET n@566: */ n@566: n@566: var chartContext; n@566: window.onload = function() { n@566: // Load the Visualization API and the corechart package. n@566: google.charts.load('current', {'packages':['corechart']}); n@566: chartContext = new Chart(); n@566: } n@566: n@571: function arrayMean(values) { n@571: var mean = 0; n@571: for (var value of values) { n@571: mean += value; n@571: } n@571: mean /= values.length; n@571: return mean; n@571: } n@571: n@571: function percentile(values, n) { n@571: values.sort( function(a,b) {return a - b;} ); n@571: // get ordinal rank n@571: var rank = Math.min(Math.floor(values.length*n/100), values.length-1); n@571: return values[rank]; n@571: } n@571: n@571: function arrayMin(array) { n@571: // Return the minimum value of an array n@571: var min = array[0]; n@571: for (var value of array) { n@571: if (value < min) { n@571: min = value; n@571: } n@571: } n@571: return min; n@571: } n@571: n@571: function arrayMax(array) { n@571: // Return the minimum value of an array n@571: var max = array[0]; n@571: for (var value of array) { n@571: if (value > max) { n@571: max = value; n@571: } n@571: } n@571: return max; n@571: } n@571: n@571: function arrayHistogram(values,steps,min,max) { n@571: if (steps == undefined) { n@571: steps = 0.25; n@571: console.log("Warning: arrayHistogram called without steps size set, default to 0.25"); n@571: } n@571: if (min == undefined) {min = arrayMin(values);} n@571: if (max == undefined) {max = arrayMax(values);} n@571: var histogram = []; n@571: var index = min; n@571: while(index < max) { n@571: histogram.push({ n@571: marker: index, n@571: lt: index, n@571: rt: index+steps, n@571: count: 0 n@571: }); n@571: index += steps; n@571: } n@571: for (var value of values) { n@571: for (var entry of histogram) { n@571: if (value >= entry.lt && value <= entry.rt) { n@571: entry.count++; n@571: break; n@571: } n@571: } n@571: } n@571: return histogram; n@571: } n@571: n@566: function Chart() { n@569: this.valueData = null; n@569: this.commentData = null; n@569: this.loadStatus = 0; n@571: this.charts = []; n@566: n@569: var XMLHttp = new XMLHttpRequest(); n@569: XMLHttp.parent = this; n@569: XMLHttp.open("GET","../scripts/score_parser.php?format=JSON",true); n@569: XMLHttp.onload = function() { n@569: // Now we have the JSON data, extract n@569: this.parent.valueData = JSON.parse(this.responseText); n@569: this.parent.loadStatus++; n@566: } n@569: XMLHttp.send(); n@569: var XMLHttp2 = new XMLHttpRequest(); n@569: XMLHttp2.parent = this; n@569: XMLHttp2.open("GET","../scripts/comment_parser.php?format=JSON",true); n@569: XMLHttp2.onload = function() { n@569: // Now we have the JSON data, extract n@569: this.parent.commentData = JSON.parse(this.responseText); n@569: this.parent.loadStatus++; n@569: } n@569: XMLHttp2.send(); n@566: n@571: this.chartObject = function(name) { n@571: // Create the charting object n@571: this.name = name; n@571: this.root = document.createElement("div"); n@571: this.root.className = "chart-holder"; n@571: this.root.setAttribute("name",name); n@571: this.chartDOM = document.createElement("div"); n@571: this.tableDOM = document.createElement("div"); n@571: this.latexDOM = document.createElement("div"); n@571: this.downloadDOM = document.createElement("div"); n@571: this.chart = undefined; n@571: this.data = new google.visualization.DataTable(); n@571: this.options = {}; n@571: this.print = document.createElement("button"); n@572: this.sortDataButton = document.createElement("button"); n@572: this.sortDataButton.textContent = "Sort by Data"; n@572: this.sortDataButton.addEventListener("click",this); n@572: this.sortDataButton.setAttribute("name","sort-data"); n@572: this.sortNameButton = document.createElement("button"); n@572: this.sortNameButton.textContent = "Sort by Name"; n@572: this.sortNameButton.addEventListener("click",this); n@572: this.sortNameButton.setAttribute("name","sort-name"); n@572: this.draw = function() { n@572: if (this.chart == undefined) {return;} n@572: this.tableDOM.innerHTML = null; n@572: this.latexDOM.innerHTML = null; n@572: this.buildTable(); n@572: this.writeLatex(); n@572: this.chart.draw(this.data,this.options); n@572: } n@572: this.sortData = function() { n@572: n@572: var map = this.data.Jf.map(function(el,i){ n@572: return {index: i, value: el.c[1].v}; n@572: }); n@572: n@572: map.sort(function(a,b){ n@572: if (a.value > b.value) {return -1;} n@572: if (a.value < b.value) {return 1;} n@572: return 0; n@572: }) n@572: n@572: var Jf = []; n@572: var cc = []; n@572: for (var i=0; i b.value) {return 1;} n@572: return 0; n@572: }) n@572: n@572: var Jf = []; n@572: var cc = []; n@572: for (var i=0; i