Source

xJs / lib / gChart.ml

Full commit
(** google chart *)

open Spotlib_js.Spot
open Js
open Std

module DataTable = struct
  class type t = object
    method addColumn_withID : string_t (* type. Either "string", "number", "boolean", "date", "datetime" or "timeofday" *)
                              -> string_t (* label *)
                              -> string_t (* id *)
                              -> unit meth

    method addColumn : string_t (* type. Either "string", "number", "boolean", "date", "datetime" or "timeofday" *)
                       -> string_t (* label *)
                       -> unit meth

    method addRows : 'a Js.t array_t array_t -> unit meth
  end
end

module Chart = struct
  class type t = object
    method draw : DataTable.t Js.t -> 'options Js.t(* ? *) -> unit meth
  end
end

(* Does not work: google.visualization is undefined
   Use readonly_props
      let cls = Unsafe.variable "google.visualization.DataTable"
      let create () : dataTable Js.t = Unsafe.new_obj cls [| |]
*)

module Visualization = struct

  class type t = object
    (* methods and props begin with a Capital letter is prefixed with '_' in OCaml *)

    (*
      method _DataTable : dataTable Js.t readonly_prop 
      (* Simple, but no jsnew interface.
         for new, Unsafe.new_obj google##visualization##_DataTable_ [| |]
      *)
    *)

    method _DataTable : DataTable.t Js.t constr readonly_prop 
    method _PieChart : (Dom_html.element Js.t -> Chart.t Js.t) constr readonly_prop
    method _BarChart : (Dom_html.element Js.t -> Chart.t Js.t) constr readonly_prop
    method _ScatterChart : (Dom_html.element Js.t -> Chart.t Js.t) constr readonly_prop
  end
end

include Visualization

let load_via_ajax_api_loader () = 
  Google.load_ajax_api !$"visualization" !$"1.0" 
  & Unsafe.variable "{'packages':['corechart']}"

(** [load ()] must be done before [get ()] *)
let get () : t Js.t = Unsafe.variable "google.visualization"