PageRenderTime 14ms CodeModel.GetById 1ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 1ms

/src/graph/main.ml

http://github.com/hhughes/ocaml-frui
OCaml | 110 lines | 89 code | 17 blank | 4 comment | 7 complexity | 4694d5417a27347a7f2a146fcacdedd5 MD5 | raw file
  1open JQuery
  2open Json
  3open Fvar
  4
  5let main_elt = Dom.document#getElementById "main"
  6
  7let gdp_data = Hashtbl.create 50
  8let gdp_vars = Hashtbl.create 240
  9let gdp_max = ref 0.
 10
 11let life_data = Hashtbl.create 50
 12let life_vars = Hashtbl.create 240
 13let life_max = ref 0.
 14
 15let min_year = ref 2000
 16let max_year = ref 2000
 17
 18let time = new fvar 2000.
 19
 20let ids = ref []
 21
 22let load_datum ht max d =
 23  let year = int_of_string (Datum.date d) in
 24  let country = Item.id (Datum.country d) in
 25  let value = try float_of_string (Datum.value d) with _ -> nan in
 26  if not (Hashtbl.mem ht year) then Hashtbl.add ht year [];
 27  let tl = Hashtbl.find ht year in
 28  if(year < !min_year) then min_year := year;
 29  if(year > !max_year) then max_year := year;
 30  begin
 31  match classify_float value with
 32    | FP_normal -> if(value > !max) then max := value else ()
 33    | _ -> ()
 34  end;
 35  Hashtbl.replace ht year ((country,value)::tl)
 36
 37let load_var ht (id, value) = Hashtbl.replace ht id (new fvar value)
 38
 39let position_div (elt : Dom.element) x_max y_max x y =
 40  let w = float_of_int (main_elt#_get_offsetWidth) in
 41  let h = float_of_int (main_elt#_get_offsetHeight) in
 42  let l = (x *. w) /. x_max in
 43  let t = h -. ((y *. h) /. y_max) in
 44  elt#_get_style#_set_left (string_of_int (int_of_float l));
 45  elt#_get_style#_set_top (string_of_int (int_of_float t))
 46
 47let create_div id =
 48  let elt = Dom.document#createElement "div" in
 49  elt#_set_className "datum";
 50  elt#_set_title id;
 51  let x_axis = Hashtbl.find gdp_vars id in
 52  let y_axis = Hashtbl.find life_vars id in
 53(*  let foo = !gdp_max in
 54  Logger.debug (string_of_float foo);
 55  Logger.debug (Printf.sprintf "gdp_max: %f, life_max: %f" !gdp_max !life_max);*)
 56  ignore (Froc.lift2 (position_div elt !gdp_max !life_max) x_axis#b y_axis#b);
 57  ignore (main_elt#appendChild elt)
 58
 59let update_axis ht_d ht_v y =
 60  let update (id,v) =
 61    let var = Hashtbl.find ht_v id in
 62    var#set v
 63  in
 64  List.iter update (Hashtbl.find ht_d (int_of_float y))
 65(* HACK *)
 66let to_load = ref 99
 67let loaded () =
 68  if !to_load > 0 then to_load := !to_load - 1
 69  else List.iter create_div !ids
 70
 71let load_objects ht_d ht_v max o s =
 72  match s with
 73    | "success" ->
 74      begin
 75	let data = js_to_list (unmarshall_json o) in (* expensive!? *)
 76	let data = js_to_list (List.hd data) in
 77	List.iter (load_datum ht_d max) data;
 78	List.iter (load_var ht_v) (Hashtbl.find ht_d 2009);
 79	ids := [];
 80	List.iter (fun (id,_) -> ids := id :: !ids) (Hashtbl.find ht_d 2009);
 81	ignore(Froc.lift (update_axis ht_d ht_v) time#b);
 82	loaded ()
 83      end
 84    | _ -> Logger.debug s
 85
 86let rec get_n u d v m = function
 87  | 0 -> ()
 88  | n -> 
 89    let url = Printf.sprintf u n in
 90    Logger.debug url;
 91    ignore (jQuery_util#get url () (load_objects d v m));
 92    get_n u d v m (n-1)
 93
 94let load_json _ =
 95  let n_input = (Dom.document#getElementById "n" : Dom.input) in
 96  let n = int_of_string (n_input#_get_value) + 1 in
 97  to_load := (2 * n) - 1;
 98  get_n "http://localhost:8080/gdp/gdp-%d.json" gdp_data gdp_vars gdp_max n;
 99  get_n "http://localhost:8080/life/life-%d.json" life_data life_vars life_max n;
100  true
101
102let onload () =
103  let load_button = Dom.document#getElementById "load" in
104  load_button#_set_onclick (load_json);
105  let spinner_div = Dom.document#getElementById "spinner" in
106  Spinner.create spinner_div time 1.
107;;
108
109Dom.window#_set_onload onload
110