+ function showdate(utime) {
+ var dt = new Date(utime*1000);
+ return dt.toLocaleDateString() + " " + dt.toLocaleTimeString();
+ }
+
+ function px(x) {
+ return xzero + ((x - tmin) * tfact);
+ }
+
+ function py(y) { return wh - yzero - (y * hfact);
+ }
+
+ const dow = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
+ const mn = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
+ "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
+
+ function getcombx(tmin, tmax) {
+ var comb = [], lb = [];
+ var trange = tmax - tmin;
+ var inc, inc2, base, base2, t, dt, zoff;
+ var label = function(t) { return "<<" + t.toFixed(1) + ">>"; }
+ function tohour(t) { var dt = new Date(t*1000); return dt.getHours(); }
+ function todow(t) { var dt = new Date(t*1000); return dow[dt.getDay()]; }
+ function todom(t) { var dt = new Date(t*1000); return dt.getDate(); }
+ function tomonth(t) { var dt = new Date(t*1000); return mn[dt.getMonth()]; }
+
+ if (trange < 172800) { /* two days -> one hour */
+ inc = 3600;
+ inc2 = 21600;
+ label = tohour;
+ }
+ else if (trange < 864000) { /* 10 days -> six hours */
+ inc = 21600;
+ inc2 = 86400;
+ label = todow;
+ }
+ else if (trange < 2678400) { /* 31 days -> 1 day */
+ inc = 86400;
+ inc2 = 86400;
+ label = todom;
+ }
+ else { /* ~ one month */
+ /* TODO: make this a separate case with loop over months rather than
+ fixed number of seconds. */
+ inc = 86400;
+ inc2 = 2592000;
+ label = tomonth;
+ }
+
+ dt = new Date(tmin*1000);
+ zoff = 60 * dt.getTimezoneOffset();
+ base = (Math.floor((tmin - zoff - 1) / inc) + 1) * inc + zoff;
+ for (t = base; t < tmax; t += inc)
+ comb.push(t);
+ base2 = (Math.floor((tmin - zoff - 1) / inc2) + 1) * inc2 + zoff;
+ for (t = base2; t < tmax; t += inc2)
+ lb.push([t, label(t)]);
+ //dbg.innerHTML = "inc=" + inc + "<br>inc2=" + inc2 + "<br>"
+ // + "tmin=" + tmin + " tmax=" + tmax + "<br>"
+ // + "base=" + base + " base2=" + base2 + "<br>"
+ // + comb + "<br>" + lb;
+ return [comb, lb];
+ }
+
+ function xaxis() {
+ var comb = getcombx(tmin, tmax);
+ var i;
+
+ ctx.beginPath();
+ for (i = 0; comb[0][i]; i++) {
+ ctx.moveTo(px(comb[0][i]), py(0) + 5);
+ ctx.lineTo(px(comb[0][i]), py(0));
+ }
+ ctx.strokeStyle = "gray";
+ ctx.stroke();
+
+ ctx.beginPath();
+ ctx.moveTo(px(tmin), py(0));
+ ctx.lineTo(px(tmax), py(0));
+ ctx.strokeStyle = "black";
+ ctx.stroke();
+
+ ctx.fillStyle = "black";
+ ctx.font = "bold 16px Courier";
+ ctx.textAlign = "center";
+ ctx.beginPath();
+ for (i = 0; comb[1][i]; i++) {
+ ctx.fillText(comb[1][i][1], px(comb[1][i][0]), py(0) + 20);
+ ctx.moveTo(px(comb[1][i][0]), py(0) + 5);
+ ctx.lineTo(px(comb[1][i][0]), py(0));
+ }
+ ctx.strokeStyle = "black";
+ ctx.stroke();
+
+ ctx.fillStyle = "black";
+ ctx.textAlign = "left";
+ ctx.fillText(showdate(tmin), px(tmin), py(0) + 40);
+ ctx.textAlign = "right";
+ ctx.fillText(showdate(tmax), px(tmax), py(0) + 40);
+ }
+
+ function getcomby(lo, hi) {