n@1223: /* n@1223: * Analysis script for WAET n@1223: */ n@1223: n@1284: // Firefox does not have an XMLDocument.prototype.getElementsByName n@1284: // and there is no searchAll style command, this custom function will n@1284: // search all children recusrively for the name. Used for XSD where all n@1284: // element nodes must have a name and therefore can pull the schema node n@1284: XMLDocument.prototype.getAllElementsByName = function(name) n@1284: { n@1284: name = String(name); n@1284: var selected = this.documentElement.getAllElementsByName(name); n@1284: return selected; n@1284: } n@1284: n@1284: Element.prototype.getAllElementsByName = function(name) n@1284: { n@1284: name = String(name); n@1284: var selected = []; n@1284: var node = this.firstElementChild; n@1284: while(node != null) n@1284: { n@1284: if (node.getAttribute('name') == name) n@1284: { n@1284: selected.push(node); n@1284: } n@1284: if (node.childElementCount > 0) n@1284: { n@1284: selected = selected.concat(node.getAllElementsByName(name)); n@1284: } n@1284: node = node.nextElementSibling; n@1284: } n@1284: return selected; n@1284: } n@1284: n@1284: XMLDocument.prototype.getAllElementsByTagName = function(name) n@1284: { n@1284: name = String(name); n@1284: var selected = this.documentElement.getAllElementsByTagName(name); n@1284: return selected; n@1284: } n@1284: n@1284: Element.prototype.getAllElementsByTagName = function(name) n@1284: { n@1284: name = String(name); n@1284: var selected = []; n@1284: var node = this.firstElementChild; n@1284: while(node != null) n@1284: { n@1284: if (node.nodeName == name) n@1284: { n@1284: selected.push(node); n@1284: } n@1284: if (node.childElementCount > 0) n@1284: { n@1284: selected = selected.concat(node.getAllElementsByTagName(name)); n@1284: } n@1284: node = node.nextElementSibling; n@1284: } n@1284: return selected; n@1284: } n@1284: n@1284: // Firefox does not have an XMLDocument.prototype.getElementsByName n@1284: if (typeof XMLDocument.prototype.getElementsByName != "function") { n@1284: XMLDocument.prototype.getElementsByName = function(name) n@1284: { n@1284: name = String(name); n@1284: var node = this.documentElement.firstElementChild; n@1284: var selected = []; n@1284: while(node != null) n@1284: { n@1284: if (node.getAttribute('name') == name) n@1284: { n@1284: selected.push(node); n@1284: } n@1284: node = node.nextElementSibling; n@1284: } n@1284: return selected; n@1284: } n@1284: } n@1284: n@1284: var chartContext, testData; n@1223: window.onload = function() { n@1223: // Load the Visualization API and the corechart package. n@1284: google.charts.load('current', {'packages':['corechart']}); n@1223: chartContext = new Chart(); n@1284: testData = new Data(); n@1284: } n@1284: n@1284: function get(url) { n@1284: // Return a new promise. n@1284: return new Promise(function(resolve, reject) { n@1284: // Do the usual XHR stuff n@1284: var req = new XMLHttpRequest(); n@1284: req.open('GET', url); n@1284: req.onload = function() { n@1284: // This is called even on 404 etc n@1284: // so check the status n@1284: if (req.status == 200) { n@1284: // Resolve the promise with the response text n@1284: resolve(req.response); n@1284: } n@1284: else { n@1284: // Otherwise reject with the status text n@1284: // which will hopefully be a meaningful error n@1284: reject(Error(req.statusText)); n@1284: } n@1284: }; n@1284: n@1284: // Handle network errors n@1284: req.onerror = function() { n@1284: reject(Error("Network Error")); n@1284: }; n@1284: n@1284: // Make the request n@1284: req.send(); n@1284: }); n@1223: } n@1223: n@1227: function arrayMean(values) { n@1227: var mean = 0; n@1227: for (var value of values) { n@1227: mean += value; n@1227: } n@1227: mean /= values.length; n@1227: return mean; n@1227: } n@1227: n@1275: function percentile(values, p) { n@1275: //http://web.stanford.edu/class/archive/anthsci/anthsci192/anthsci192.1064/handouts/calculating%20percentiles.pdf n@1227: values.sort( function(a,b) {return a - b;} ); n@1227: // get ordinal rank n@1275: var index = values.length*p/100; n@1275: var k = Math.floor(index); n@1275: if (k == index) { n@1275: return values[k]; n@1275: } else { n@1275: var f = index-k; n@1275: var x_int = (1-f)*values[k]+f*values[k+1]; n@1275: return x_int; n@1275: } n@1227: } n@1227: n@1227: function arrayMin(array) { n@1227: // Return the minimum value of an array n@1227: var min = array[0]; n@1227: for (var value of array) { n@1227: if (value < min) { n@1227: min = value; n@1227: } n@1227: } n@1227: return min; n@1227: } n@1227: n@1227: function arrayMax(array) { n@1227: // Return the minimum value of an array n@1227: var max = array[0]; n@1227: for (var value of array) { n@1227: if (value > max) { n@1227: max = value; n@1227: } n@1227: } n@1227: return max; n@1227: } n@1227: n@1273: function boxplotRow(array) { n@1273: // Take an array of element values and return array of computed intervals n@1273: var result = { n@1287: median : percentile(array,50), n@1273: pct25 : percentile(array,25), n@1273: pct75 : percentile(array,75), n@1273: IQR : null, n@1273: min: null, n@1273: max: null, n@1273: outliers: new Array() n@1273: } n@1273: result.IQR = result.pct75-result.pct25; n@1273: var rest = []; n@1273: var pct75_IQR = result.pct75+1.5*result.IQR; n@1273: var pct25_IQR = result.pct25-1.5*result.IQR; n@1273: for (var i=0; i pct75_IQR || point < pct25_IQR) { n@1273: result.outliers.push(point); n@1273: } else { n@1273: rest.push(point); n@1273: } n@1273: } n@1273: result.max = arrayMax(rest); n@1273: result.min = arrayMin(rest); n@1273: return result; n@1273: n@1273: } n@1273: n@1227: function arrayHistogram(values,steps,min,max) { n@1227: if (steps == undefined) { n@1227: steps = 0.25; n@1227: console.log("Warning: arrayHistogram called without steps size set, default to 0.25"); n@1227: } n@1227: if (min == undefined) {min = arrayMin(values);} n@1227: if (max == undefined) {max = arrayMax(values);} n@1227: var histogram = []; n@1227: var index = min; n@1227: while(index < max) { n@1227: histogram.push({ n@1227: marker: index, n@1227: lt: index, n@1227: rt: index+steps, n@1227: count: 0 n@1227: }); n@1227: index += steps; n@1227: } n@1227: for (var value of values) { n@1227: for (var entry of histogram) { n@1227: if (value >= entry.lt && value <= entry.rt) { n@1227: entry.count++; n@1227: break; n@1227: } n@1227: } n@1227: } n@1227: return histogram; n@1227: } n@1227: n@1223: function Chart() { n@1284: this.valueData; n@1227: this.charts = []; n@1223: n@1227: this.chartObject = function(name) { n@1227: // Create the charting object n@1227: this.name = name; n@1227: this.root = document.createElement("div"); n@1227: this.root.className = "chart-holder"; n@1227: this.root.setAttribute("name",name); n@1227: this.chartDOM = document.createElement("div"); n@1227: this.tableDOM = document.createElement("div"); n@1227: this.latexDOM = document.createElement("div"); n@1227: this.downloadDOM = document.createElement("div"); n@1227: this.chart = undefined; n@1227: this.data = new google.visualization.DataTable(); n@1227: this.options = {}; n@1227: this.print = document.createElement("button"); n@1228: this.sortDataButton = document.createElement("button"); n@1228: this.sortDataButton.textContent = "Sort by Data"; n@1228: this.sortDataButton.addEventListener("click",this); n@1228: this.sortDataButton.setAttribute("name","sort-data"); n@1228: this.sortNameButton = document.createElement("button"); n@1228: this.sortNameButton.textContent = "Sort by Name"; n@1228: this.sortNameButton.addEventListener("click",this); n@1228: this.sortNameButton.setAttribute("name","sort-name"); n@1228: this.draw = function() { n@1228: if (this.chart == undefined) {return;} n@1228: this.tableDOM.innerHTML = null; n@1228: this.latexDOM.innerHTML = null; n@1228: this.buildTable(); n@1228: this.writeLatex(); n@1228: this.chart.draw(this.data,this.options); n@1228: } n@1228: this.sortData = function() { n@1265: this.data.sort(1); n@1228: } n@1228: this.sortName = function() { n@1265: this.data.sort(0); n@1228: } n@1227: this.handleEvent = function() { n@1227: // Only used to handle the chart.event.addListener(this,'ready') callback n@1228: switch(event.currentTarget.getAttribute("name")) n@1228: { n@1228: case "download": n@1228: window.open(this.chart.getImageURI()); n@1228: break; n@1228: case "sort-data": n@1228: this.sortData(); n@1228: this.draw(); n@1228: break; n@1228: case "sort-name": n@1228: this.sortName(); n@1228: this.draw(); n@1228: break; n@1228: } n@1227: } n@1227: n@1227: this.root.appendChild(this.chartDOM); n@1227: this.root.appendChild(this.tableDOM); n@1227: this.root.appendChild(this.latexDOM); n@1228: this.root.appendChild(this.sortDataButton); n@1228: this.root.appendChild(this.sortNameButton); n@1227: this.root.appendChild(this.print); n@1227: this.print.textContent = "Download"; n@1228: this.print.setAttribute("name","download"); n@1227: this.print.addEventListener("click",this); n@1227: this.root.appendChild(this.downloadDOM); n@1227: this.buildTable = function() { n@1227: var table = document.createElement("table"); n@1227: table.border = "1"; n@1264: var numRows = this.data.getNumberOfRows(); n@1264: var numColumns = this.data.getNumberOfColumns(); n@1264: for (var columnIndex=0; columnIndex"; n@1284: this.rootDOM.className = "filter-entry"; n@1284: this.handleEvent = function(event) { n@1284: switch(this.specification.type) { n@1284: case "number": n@1284: var name = event.currentTarget.name; n@1284: eval("this."+name+" = event.currentTarget.value"); n@1284: break; n@1284: case "checkbox": n@1284: break; n@1284: case "radio": n@1284: break; n@1284: } n@1284: this.parent.getFileCount(); n@1284: } n@1284: this.getFilterPairs = function() { n@1284: var pairs = []; n@1284: switch(this.specification.type) { n@1284: case "number": n@1284: if (this.min != "") { n@1284: pairs.push([specification.id+"-min",this.min]); n@1284: } n@1284: if (this.max != "") { n@1284: pairs.push([specification.id+"-max",this.max]); n@1284: } n@1284: break; n@1284: case "radio": n@1284: case "checkbox": n@1284: for (var i=0; i