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@571: this.handleEvent = function() {
n@571: // Only used to handle the chart.event.addListener(this,'ready') callback
n@571: this.downloadDOM.innerHTML = 'Download';
n@571: }
n@571:
n@571: this.root.appendChild(this.chartDOM);
n@571: this.root.appendChild(this.tableDOM);
n@571: this.root.appendChild(this.latexDOM);
n@571: this.root.appendChild(this.print);
n@571: this.print.textContent = "Download";
n@571: this.print.addEventListener("click",this);
n@571: this.root.appendChild(this.downloadDOM);
n@571: this.buildTable = function() {
n@571: var table = document.createElement("table");
n@571: table.border = "1";
n@571: for (var rowIndex=0; rowIndex