Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yminsky/c5b8088ce355a658174e to your computer and use it in GitHub Desktop.
Save yminsky/c5b8088ce355a658174e to your computer and use it in GitHub Desktop.
Plotting sine and cosine with c3 and js_of_ocaml
let get_by_id id =
let d = Dom_html.document in
Js.Opt.get (d##getElementById (Js.string id))
(fun () -> assert false)
let rec range i n =
if i >= n then [] else i :: range (i + 1) n
let base =
range 0 100
|> List.map float_of_int
|> List.map (fun x -> x /. 40.)
let sin_data = List.map (fun x -> (x,sin x)) base
let cos_data = List.map (fun x -> (x,cos x)) base
let timeseries name =
let chart = C3.Line.make ~kind:`XY () |> C3.Line.render ~bindto:name in
C3.Line.update chart
~segments:[ C3.Segment.make () ~label:"sin" ~points:sin_data
; C3.Segment.make () ~label:"cos" ~points:cos_data
]
;;
let () =
Dom_html.window##onload <- Dom_html.handler
(fun _ ->
timeseries "#timeseries";
Js._true
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment