PageRenderTime 20ms CodeModel.GetById 1ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/js-only/heatmap/main.js

http://github.com/hhughes/ocaml-frui
JavaScript | 144 lines | 120 code | 24 blank | 0 comment | 18 complexity | 02b3ea92b0fa59d256992fc290196e35 MD5 | raw file
  1var main_elt = $("div#main");
  2var elements = {};
  3
  4var time = 0;
  5var inc = 3600000;
  6
  7var elec_data = {};
  8
  9var rooms = [];
 10
 11var min_date = 0;
 12var max_date = 2000;
 13
 14function pos(r) {
 15    switch(r) {
 16    case "GN17":
 17	return { left: 470, top: 90 };
 18    case "FN30":
 19	return { left: 1120, top: 70 };
 20    case "SN30":
 21	return { left: 1730, top: 70 };
 22    default:
 23	return { left: 0, top: 0};
 24    }
 25}
 26
 27function load_objects(ht_d, o) {
 28    var e = eval(o)[0];
 29    var room = e.room;
 30    var data = e.data;
 31    for(x in data) {
 32	var d = data[x];
 33	var y = parseInt(d[0]);
 34	var v = parseInt(d[1]);
 35	if(ht_d[y] == null) {
 36	    ht_d[y] = [];
 37	}
 38	var x = {
 39	    "room": room,
 40	    "value": v
 41	};
 42	ht_d[y].push(x);
 43	if(!ht_d.max || v > ht_d.max) {
 44	    ht_d.max = v;
 45	}
 46
 47	if(y > max_date) {
 48	    max_date = y;
 49	}
 50
 51	if(!min_date || y < min_date) {
 52	    min_date = y;
 53	}
 54    }
 55}
 56
 57function create_elements() {
 58    for(r in rooms) {
 59	var room = rooms[r];
 60	var div = $(document.createElement("div")).addClass("room").attr("title", room);
 61	main_elt.append(div);
 62	elements[room] = div;
 63	div.offset(pos(room));
 64    }
 65}
 66
 67function render(r) {
 68    var rs = {};
 69    
 70    for(a in r[time]) {
 71	var d = r[time][a];
 72	if(a != "max") {
 73	    rs[d.room] = d.value;
 74	}
 75    }
 76
 77    for(x in rooms) {
 78	var room = rooms[x];
 79	var p = Math.round((rs[room] * 255) / r.max);
 80	elements[room].css({ backgroundColor: "rgb(" + p + "," + (255 - p) + ",0)" });
 81    }
 82}
 83
 84function set_time(y) {
 85    time = y;
 86    $("div#spinner > input").val(time);
 87}
 88
 89function load() {
 90    var n = parseInt($("input#n").val());
 91    var loaded = n;
 92    function all_done() {
 93	loaded--;
 94	if(!loaded) {
 95	    set_time(min_date);
 96	    for(r in elec_data[max_date]) {
 97		room = elec_data[max_date][r].room;
 98		rooms.push(room);
 99	    }
100	    create_elements();
101	    render(elec_data);
102	}
103    }
104
105    function load_elec(o) {
106	load_objects(elec_data, o);
107	all_done();
108    }
109
110    if(n > 0) $.get("http://localhost:8080/elec", load_elec);
111    if(n > 1) $.get("http://localhost:8080/elec2", load_elec);
112    if(n > 2) $.get("http://localhost:8080/elec3", load_elec);
113}
114
115function create_spinner() {
116
117    function spinner_up() {
118	time+=inc;
119	$("div#spinner > input").val(time);
120	render(elec_data);
121    }
122    
123    function spinner_down() {
124	time-=inc;
125	$("div#spinner > input").val(time);
126	render(elec_data);
127    }
128
129    var up = $(document.createElement("button")).addClass("up").text("^");
130    var down = $(document.createElement("button")).addClass("down").text("v");
131    var input = $(document.createElement("input")).addClass("spinner");
132
133    up.click(spinner_up);
134    down.click(spinner_down);
135
136    var spinner = $("div#spinner");
137    spinner.append(up);
138    spinner.append(down);
139    spinner.append(input);
140}
141
142create_spinner();
143
144$("button#load").click(load);