+ var dbg, errordiv;
+ var canvas, ctx;
+ var ww, wh;
+ var hmax, hfact;
+ var tmin, tmax, tfact;
+ var xzero = 60, yzero = 48;
+ var cold_d = [], hot_d = [];
+
+ 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) {
+ var comb = [], lb = [];
+ var d = hi - lo;
+ var ord = Math.pow(10, Math.floor(Math.log10(d)));
+ var scl = Math.floor(d / ord);
+ var inc, inc2, first, x, lb;
+
+ if (scl < 2) { inc = 0.1; inc2 = 0.2; }
+ else if (scl < 5) { inc = 0.1; inc2 = 0.5; }
+ else { inc = 0.5; inc2 = 1; }
+ inc *= ord;
+ inc2 *= ord;
+ first = (Math.floor(lo / inc) + 1) * inc;
+ for (x = 0; x < (d / inc) - 1.2; x++)
+ comb.push(first + inc * x);
+ first = (Math.floor(lo / inc2) + 1) * inc2;
+ for (x = 0; x < (d / inc2) - 1.2; x++)
+ lb.push(first + inc2 * x);
+ //dbg.innerHTML = "ord=" + ord + "<br>inc=" + inc + "<br>"
+ // + comb + "<br>" + lb;
+ return [comb, lb];
+ }
+
+ function yaxis() {
+ var comb = getcomby(0, hmax);
+ var i;
+
+ ctx.beginPath();
+ for (i = 0; comb[0][i]; i++) {
+ ctx.moveTo(px(tmin) - 5, py(comb[0][i]));
+ ctx.lineTo(px(tmax), py(comb[0][i]));
+ }
+ ctx.strokeStyle = "lightgray";
+ ctx.stroke();
+
+ ctx.beginPath();
+ ctx.moveTo(px(tmin), py(0));
+ ctx.lineTo(px(tmin), py(hmax));
+ ctx.strokeStyle = "black";
+ ctx.stroke();
+
+ ctx.fillStyle = "black";
+ ctx.font = "bold 16px Courier";
+ ctx.textAlign = "right";
+ ctx.fillText(0, px(tmin) - 6, py(0));
+ ctx.beginPath();
+ for (i = 0; comb[1][i]; i++) {
+ ctx.fillText(comb[1][i].toFixed(1), px(tmin) - 6, py(comb[1][i]));
+ ctx.moveTo(px(tmin) - 5, py(comb[1][i]));
+ ctx.lineTo(px(tmax), py(comb[1][i]));
+ }
+ ctx.strokeStyle = "gray";
+ ctx.stroke();
+
+ ctx.textAlign = "left";
+ ctx.fillText("l/min", px(tmin) + 4, py(hmax) + 12);
+ }
+
+ /* @ updates global var `hmax` */
+ function differentiate(times) {
+ var res = [];
+ var dv, dt, v;
+
+ for (i = 0; i < times.length - 1; i++) {
+ dv = times[i+1][1] - times[i][1];
+ dt = times[i+1][0] - times[i][0];
+ if (dt != 0 && dv != 0) {
+ v = (dv / dt) * 600 ; /* Litres per min */
+ if (hmax < v) hmax = v;
+ res.push([times[i][0], v]);
+ }
+ }
+ if (i) res.push([times[i][0], v]);
+
+ return res;
+ }
+
+ function drawplot(data, color) {