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