| 1 |
var DATETIME_RE = /^(\d\d\d\d)-(\d\d)-(\d\d)[T ](\d\d):(\d\d):(\d\d.?\d*)$/; |
| 2 |
var DATE_RE = /^(\d\d\d\d)-(\d\d)-(\d\d)$/; |
| 3 |
var CURRENT_PLOT; |
| 4 |
|
| 5 |
function _getValue(s) { |
| 6 |
// tries to make some kind of number from a string s |
| 7 |
// if s looks like a datetime, return a julian year |
| 8 |
// TODO: do something with hours/sexagesimal angles |
| 9 |
// if s looks like a float number, return a float |
| 10 |
// else return null. |
| 11 |
var dm = DATETIME_RE.exec(s); |
| 12 |
if (dm!=null) { |
| 13 |
var dt = new Date(parseFloat(dm[1]), parseFloat(dm[2]), parseFloat(dm[3]), |
| 14 |
parseFloat(dm[4]), parseFloat(dm[5]), parseFloat(dm[6])); |
| 15 |
return getJYear(dt); |
| 16 |
} |
| 17 |
var dm = DATE_RE.exec(s); |
| 18 |
if (dm!=null) { |
| 19 |
var dt = new Date(parseFloat(dm[1]), parseFloat(dm[2]), parseFloat(dm[3])); |
| 20 |
return getJYear(dt); |
| 21 |
} |
| 22 |
|
| 23 |
var num = parseFloat(s); |
| 24 |
if (num==num) { // not NaN |
| 25 |
return num; |
| 26 |
} |
| 27 |
return null; |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
function _getFlotSeries(table, xInd, yInd, style, options) { |
| 32 |
// returns a flot series object for plotting a line/point plot |
| 33 |
// of xInd vs. yInd from table. |
| 34 |
// |
| 35 |
// options, if given, must be a dict pre-filled with series options. |
| 36 |
// It will be modified and returned. |
| 37 |
|
| 38 |
var data = new Array(); |
| 39 |
|
| 40 |
if (options===undefined) { |
| 41 |
options = {}; |
| 42 |
} |
| 43 |
|
| 44 |
table.find('tr.data').each(function(index, row) { |
| 45 |
var tds = $(row).children(); |
| 46 |
var x = _getValue(tds[xInd].firstChild.data); |
| 47 |
var y = _getValue(tds[yInd].firstChild.data); |
| 48 |
if (x!=null && y!=null) { |
| 49 |
data.push([x, y]); |
| 50 |
} |
| 51 |
}); |
| 52 |
options["data"] = data; |
| 53 |
|
| 54 |
if (style=="Lines") { |
| 55 |
options["lines"] = {'show': true, |
| 56 |
'lineWidth': 1}; |
| 57 |
data.sort(function(a,b){return a[0]-b[0]}); |
| 58 |
} else { |
| 59 |
options["points"] = {'show': true}; |
| 60 |
} |
| 61 |
|
| 62 |
return options; |
| 63 |
} |
| 64 |
|
| 65 |
function _makeHistogram(data, numBins) { |
| 66 |
if (data.length<2) { |
| 67 |
return new Array(); |
| 68 |
} |
| 69 |
data.sort(function(a,b){return a-b}); |
| 70 |
var zp = data[0]; |
| 71 |
var binSize = (data[data.length-1]-zp)/numBins; |
| 72 |
if (binSize==0) { |
| 73 |
binSize = 1; |
| 74 |
} |
| 75 |
|
| 76 |
var histo = new Array(); |
| 77 |
for (var i=0; i<numBins; i++) { |
| 78 |
histo.push(0); |
| 79 |
} |
| 80 |
for (index in data) { |
| 81 |
histo[Math.floor((data[index]-zp)/binSize)]++; |
| 82 |
} |
| 83 |
|
| 84 |
data = new Array(); |
| 85 |
for (index in histo) { |
| 86 |
data.push([index*binSize+zp, histo[index]]); |
| 87 |
} |
| 88 |
return data; |
| 89 |
} |
| 90 |
|
| 91 |
function _doHistogramPlot(table, colInd) { |
| 92 |
var data = new Array(); |
| 93 |
table.find('tr.data').each(function(index, row) { |
| 94 |
var val = _getValue($(row).children()[colInd].firstChild.data); |
| 95 |
if (val!=null) { |
| 96 |
data.push(val); |
| 97 |
} |
| 98 |
}); |
| 99 |
|
| 100 |
var histo = _makeHistogram(data, 20); |
| 101 |
CURRENT_PLOT = jQuery.plot(jQuery('#plotarea'), [{ |
| 102 |
bars: { |
| 103 |
align: "center", |
| 104 |
show: true, |
| 105 |
barWidth: 1}, |
| 106 |
data: histo}]); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
function _doFlotPlot(table, xsel, ysel, usingSel, |
| 111 |
xIndex2, yIndex2, style2) { |
| 112 |
var xIndex = xsel.val(); |
| 113 |
var yIndex = ysel.val(); |
| 114 |
var style = usingSel.val(); |
| 115 |
|
| 116 |
if (yIndex=='Histogram') { |
| 117 |
_doHistogramPlot(table, xIndex); |
| 118 |
} else { |
| 119 |
var toPlot = []; |
| 120 |
|
| 121 |
if (style2!=undefined) { |
| 122 |
var options = { |
| 123 |
"color": "#99aa00",}; |
| 124 |
toPlot.push(_getFlotSeries(table, xIndex2, yIndex2, style2, options)); |
| 125 |
} |
| 126 |
|
| 127 |
var options = { |
| 128 |
"color": "#444444",}; |
| 129 |
toPlot.push(_getFlotSeries(table, xIndex, yIndex, style, options)); |
| 130 |
|
| 131 |
CURRENT_PLOT = jQuery.plot(jQuery('#plotarea'), toPlot, {}); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
function _plotUsingFlot(table, options) { |
| 137 |
// allows simple plotting of HTML tables. This only works from |
| 138 |
// within openFlotPlot since it uses javascript that's not loaded |
| 139 |
// by default. |
| 140 |
// In options, have xselIndex, yselIndex (column indices of columns to |
| 141 |
// plot), style ('Points' or 'Lines'), plotContainer (if |
| 142 |
// given, becomes the parent of the plot; note that right now it MUST |
| 143 |
// contain an element with id plotarea with nonzero size). |
| 144 |
// |
| 145 |
// For overplotting, there's now also xselIndex2, yselIndex2, style2. |
| 146 |
// No UI to manipulate that exists right now. |
| 147 |
|
| 148 |
// create the plot element |
| 149 |
if (options.plotContainer) { |
| 150 |
var plotElement = $(options.plotContainer); |
| 151 |
} else { |
| 152 |
var plotElement = $('<div id="plotcontainer" style="position:fixed;' |
| 153 |
+'z-index:3000;background:white;padding-left:3px;padding-right:3px;' |
| 154 |
+'padding-bottom:3px;border:2px solid gray"><p class="innerTitle">' |
| 155 |
+'<span class="closer">x </span></p>' |
| 156 |
+'<div id="plotarea" style="width:700px;height:400px;"/></div>'); |
| 157 |
plotElement.draggable(); |
| 158 |
plotElement.find(".closer").bind("click", function(){ |
| 159 |
plotElement.remove()}); |
| 160 |
} |
| 161 |
|
| 162 |
var controlPara = $('<p class="flotControl"></p>'); |
| 163 |
plotElement.append(controlPara); |
| 164 |
|
| 165 |
// Make column selectors from table headings |
| 166 |
var xsel = $('<select/>'); |
| 167 |
$(table.find('tr')[0]).find('th').each(function(index, head) { |
| 168 |
xsel.append($('<option value="'+index+'">'+$(head).text()+'</option>')); |
| 169 |
}); |
| 170 |
controlPara.append(xsel); |
| 171 |
controlPara.append(' vs. '); |
| 172 |
var ysel = xsel.clone(); |
| 173 |
ysel.append($( |
| 174 |
'<option value="Histogram">Histogram</option>')); |
| 175 |
controlPara.append(ysel); |
| 176 |
controlPara.append(' using '); |
| 177 |
var usingSel = $('<select><option selected="selected">Points</option><option>Lines</option></select>'); |
| 178 |
controlPara.append(usingSel); |
| 179 |
|
| 180 |
// Set default plot features from options if given there |
| 181 |
var xselIndex = options.xselIndex || 0; |
| 182 |
var yselIndex = options.yselIndex || ysel.children().length-1; |
| 183 |
var style = options.style || 'Points'; |
| 184 |
|
| 185 |
xsel.val(xselIndex).trigger('chosen:updated'); |
| 186 |
ysel.val(yselIndex).trigger('chosen:updated'); |
| 187 |
usingSel.val(style).trigger('chosen:updated'); |
| 188 |
|
| 189 |
// the callback any form items controlling the plot |
| 190 |
var updatePlot = function() { |
| 191 |
try { |
| 192 |
_doFlotPlot(table, xsel, ysel, usingSel, |
| 193 |
options.xselIndex2, options.yselIndex2, options.style2); |
| 194 |
} catch (e) { |
| 195 |
CURRENT_PLOT = jQuery.plot( |
| 196 |
jQuery('#plotarea'), [{data:[], label: "unplottable"}]); |
| 197 |
throw e; |
| 198 |
} |
| 199 |
} |
| 200 |
xsel.change(updatePlot); |
| 201 |
ysel.change(updatePlot); |
| 202 |
usingSel.change(updatePlot); |
| 203 |
|
| 204 |
if (! options.plotContainer) { |
| 205 |
$("body").prepend(plotElement); |
| 206 |
} |
| 207 |
updatePlot(); |
| 208 |
} |
| 209 |
|
| 210 |
function openFlotPlot(tableElement, options) { |
| 211 |
// opens a div that lets you plot some columns of tableElement |
| 212 |
if (options==undefined) { |
| 213 |
options = {}; |
| 214 |
} |
| 215 |
$.getScript("/static/js/jquery.flot.js", |
| 216 |
function() {_plotUsingFlot(tableElement, options)}); |
| 217 |
} |