Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.