+ var dbg;
+ var xmlhttp;
+ var canvas, ctx;
+ var ww, wh;
+ var hmax, hfact;
+ var tmin, tmax, tfact;
+ var xzero = 20, yzero = 20;
+ var cold_d = [], hot_d = [];
+
+ function showdate(utime) {
+ var dt = new Date(utime*1000);
+ return dt.toLocaleDateString() + " " + dt.toLocaleTimeString();
+ }
+
+ function getcomb(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.5; }
+ else if (scl < 5) { inc = 0.2; inc2 = 1; }
+ else { inc = 0.5; inc2 = 2; }
+ inc *= ord;
+ inc2 *= ord;
+ first = (Math.floor(lo / inc) + 1) * inc;
+ for (x = 0; x < (d / inc) - 1.5; x++)
+ comb.push(first + inc * x);
+ first = (Math.floor(lo / inc2) + 1) * inc2;
+ for (x = 0; x < (d / inc2) - 1.5; x++)
+ lb.push(first + inc2 * x);
+ //dbg.innerHTML = "ord=" + ord + "<br>inc=" + inc + "<br>"
+ // + comb + "<br>" + lb;
+ return [comb, lb];
+ }
+
+ function px(x) {
+ return xzero + ((x - tmin) * tfact);
+ }
+
+ function py(y) {
+ return wh - yzero - (y * hfact);
+ }
+
+ function xaxis() {
+ 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 8px Courier";
+ ctx.textAlign = "left";
+ ctx.fillText(showdate(tmin), px(tmin), py(0) + 16);
+ ctx.textAlign = "right";
+ ctx.fillText(showdate(tmax), px(tmax), py(0) + 16);
+ }
+
+ function yaxis() {
+ var comb = getcomb(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 8px Courier";
+ ctx.textAlign = "right";
+ ctx.fillText(0, px(tmin) - 2, py(0));
+ for (i = 0; comb[1][i]; i++) {
+ ctx.fillText(comb[1][i], px(tmin) - 2, py(comb[1][i]));
+ }
+ ctx.textAlign = "left";
+ ctx.fillText("l/h", px(tmin) + 2, py(hmax) + 8);
+ }
+
+ /* @ 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) * 360 ; /* Litres per hour */
+ 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) {