Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion jsoo/jsoo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ let rec conv_value v =
| Value (Array ty, vs) ->
let vs = Array.map (fun v -> conv_value (Value (ty, v))) vs in
Js.Unsafe.inject @@ Js.array vs
| Object kvs ->
let obj = Js.Unsafe.obj (Array.of_list @@ List.map (fun (k, v) -> (k, conv_value v)) kvs) in
Js.Unsafe.inject obj
| Value (Object, obj) -> of_json_value (obj : Ezjsonm.value)

and of_json_value (j : Ezjsonm.value) : _ Js.t =
match (j : Ezjsonm.value) with
| `String s -> Js.Unsafe.inject @@ Js.string s
Expand Down
4 changes: 4 additions & 0 deletions python/python.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ let rec of_value v =
| Value (Bool, b) -> Py.Bool.of_bool b
| Value (Array ty, vs) ->
Py.List.of_array @@ Array.map (fun v -> of_value (Value (ty, v))) vs
| Object kvs ->
let d = Py.Dict.create () in
List.iter (fun (k, v) -> Py.Dict.set_item d (Py.String.of_string k) (of_value v)) kvs;
d
| Value (Object, obj) -> of_json_value (obj : Ezjsonm.value)

and of_json_value (j : Ezjsonm.value) : Py.Object.t =
Expand Down
29 changes: 18 additions & 11 deletions src/base.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ end

module Value = struct
type 'a t = 'a Type.t * 'a
type value = Value : 'a t -> value
type value =
| Value : 'a t -> value
| Object : (string * value) list -> value

let float f : float t = Type.Float, f
let string s : string t = Type.String, s
Expand All @@ -42,6 +44,7 @@ module Value = struct
| Value (Bool, b) -> `Bool b
| Value (Object, obj) -> obj
| Value (Array ty, xs) -> `A (List.map (fun x -> to_json (Value (ty, x))) @@ Array.to_list xs)
| Object kvs -> `O (List.map (fun (k, v) -> k, to_json v) kvs)

let rec of_json v =
let open Option in
Expand All @@ -55,16 +58,20 @@ module Value = struct
(match vs with
| [] -> None
| v::vs ->
let Value (ty, _) = v in
let rec check acc = function
| [] -> Some (Value (Type.Array ty, Array.of_list @@ List.rev acc))
| v'::vs ->
let Value (ty', v') = v' in
match Type.eq ty ty' with
| Some Eq -> check (v'::acc) vs
| None -> None
in
check [] (v::vs))
(match v with
| Value (ty, _) ->
let rec check acc = function
| [] -> Some (Value (Type.Array ty, Array.of_list @@ List.rev acc))
| v'::vs ->
(match v' with
| Value (ty', v') ->
(match Type.eq ty ty' with
| Some Eq -> check (v'::acc) vs
| None -> None)
| Object _ -> None)
in
check [] (v::vs)
| Object _ -> None))
| _ -> None
end

Expand Down
4 changes: 3 additions & 1 deletion src/base.mli
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ end

module Value : sig
type 'a t = 'a Type.t * 'a
type value = Value : 'a t -> value
type value =
| Value : 'a t -> value
| Object : (string * value) list -> value

val float : float -> float t
val string : string -> string t
Expand Down
14 changes: 14 additions & 0 deletions src/demo/demo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ open Data
open Graph
open Figure
open Layout
open Axis


let scatter_ =
figure
Expand Down Expand Up @@ -109,6 +111,17 @@ let histogram_ =
[ histogram [ x (Array.init 500 (fun _ -> Random.float 1.0)) ] ]
[ title "Histogram" ]

let scatter_with_axes =
let x_data = [| 1.0; 2.0; 3.0; 4.0; 5.0 |] in
let y_data = [| 1.0; 4.0; 9.0; 16.0; 25.0 |] in
figure
[ scatter [ x x_data; y y_data; mode "lines+markers" ] ]
[
title "Data with Custom Axis Labels";
xaxis [axis_title "Time (seconds)"; axis_type "linear"];
yaxis [axis_title "Distance (meters)"; axis_range 0. 30.];
]

let figures =
[ scatter_;
scatter_markers;
Expand All @@ -121,4 +134,5 @@ let figures =
bar_stack_horizontal;
pie_;
histogram_;
scatter_with_axes;
]
12 changes: 12 additions & 0 deletions src/plotly.ml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ module Layout = struct
let title = string "title"
let barmode = string "barmode"
let showlegend = bool "showlegend"
module Axis = struct
let axis_title s = ("title", Base.Value.Value (Base.Value.string s))
let axis_type s = ("type", Base.Value.Value (Base.Value.string s))
let axis_range low high = ("range", Base.Value.Value (Base.Value.array Base.Type.Float [|low; high|]))
let axis_showgrid b = ("showgrid", Base.Value.Value (Base.Value.bool b))
let axis_zeroline b = ("zeroline", Base.Value.Value (Base.Value.bool b))
let axis_tickformat s = ("tickformat", Base.Value.Value (Base.Value.string s))
end

let xaxis ax = [("xaxis", Base.Value.Object ax)]
let yaxis ay = [("yaxis", Base.Value.Object ay)]
let zaxis az = [("zaxis", Base.Value.Object az)]

let layout ats = ats

Expand Down
43 changes: 41 additions & 2 deletions src/plotly.mli
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ end

module Value : sig
type 'a t = 'a Type.t * 'a
type value = Value : 'a t -> value
type value =
| Value : 'a t -> value
| Object : (string * value) list -> value

val float : float -> float t
val string : string -> string t
val bool : bool -> bool t
Expand Down Expand Up @@ -99,7 +102,43 @@ module Layout : sig
val barmode : string -> t
val showlegend : bool -> t

(* Build custom layout attributes *)
module Axis : sig
(** Set axis title *)
val axis_title : string -> string * Base.Value.value

(**
Set axis type (e.g. ["linear"] or ["log"])

See https://plotly.com/python/axes/ for more details.
*)
val axis_type : string -> string * Base.Value.value

(** Set axis data range as (min, max) *)
val axis_range : float -> float -> string * Base.Value.value

(** Toggle grid display *)
val axis_showgrid : bool -> string * Base.Value.value

(** Toggle zero line display *)
val axis_zeroline : bool -> string * Base.Value.value

(**
Set tick label format

See https://plotly.com/python/tick-formatting/ for more details.
*)
val axis_tickformat : string -> string * Base.Value.value
end

(** Set the x-axis properties *)
val xaxis : (string * Base.Value.value) list -> t

(** Set the y-axis properties *)
val yaxis : (string * Base.Value.value) list -> t

(** Set the z-axis properties *)
val zaxis : (string * Base.Value.value) list -> t

val layout : Attribute.t list -> t

val to_json : t -> Ezjsonm.value
Expand Down