PageRenderTime 23ms CodeModel.GetById 15ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 1ms

/src/visualiser/cloud.ml

http://github.com/hhughes/ocaml-frui
OCaml | 51 lines | 50 code | 1 blank | 0 comment | 0 complexity | f7de89943b8842a67d60c0c1b67ed114 MD5 | raw file
 1class cloud (c : Dom.canvas) =
 2object (self)
 3  val counter = new Counter.counter
 4  val width = 200
 5  val height = 200
 6  method canvas = c
 7  method counter = counter
 8  method render_word w t h =
 9    begin
10      let context = c#getContext "2d" in
11      context#_set_fillStyle "red";
12      context#_set_textBaseline "top";
13      context#_set_font (Printf.sprintf "%dpx san-serif" (int_of_float h));
14      context#fillText w 2. t
15    end
16  method render_all =
17    begin
18      let kvps = counter#kvps in
19      let total = float (List.fold_right (fun (k,v) a -> v + a) kvps 0) in
20      List.fold_right (
21	fun (k,v) t ->
22	  let x = (((float v) *. float (height)) /. total) in
23	  self#render_word k t x;
24	  t +. x) kvps 0.
25    end
26  method render =
27    begin
28      let context = c#getContext "2d" in
29      context#clearRect 0. 0. 200. 200.;
30      self#render_all
31    end
32  method new_counter name = 
33    begin
34      counter#add name;
35      ignore (Froc.lift (fun v -> self#render) (counter#behavior name))
36    end
37  method init =
38    begin
39      c#_set_className "cloud";
40      c#_set_width width;
41      c#_set_height height
42    end
43  initializer self#init
44end
45
46let init (elt : Dom.element) t0 t1 =
47  let cloud_canvas = (Dom.document#createElement "canvas" : Dom.canvas) in
48  let c = new cloud cloud_canvas in
49  ignore (elt#appendChild c#canvas);
50  c
51;;