forked from consensus-oracle/coracle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_basic.ml
More file actions
45 lines (40 loc) · 1.31 KB
/
Copy pathjson_basic.ml
File metadata and controls
45 lines (40 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
open Common
open Yojson.Safe
let figure_in_json ~title ~y_axis ~x_axis ~legand
~x_start ~y_start ~x_end ~y_end ~lines (data:json) : json=
`Assoc [
("title", `String title);
("x axis", `Assoc [
("label", `String x_axis);
("start", `Int x_start);
("end", `Int x_end);
]);
("y axis", `Assoc [
("label", `String y_axis);
("start", `Int y_start);
("end", `Int y_end);
]);
("legand", `Assoc [
("label", `String legand);
("data sets", `Int lines);
]);
("data", data);
]
(* convert a simple list of (x,y) coordinate to JSON *)
let simple_data_in_json (data: (int * int) list) =
`List (List.map (fun (x,y) -> `Assoc [("x",`Int x); ("y",`Int y); ]) data)
(* convery a list of list of (x,y) cooridates to JSON *)
let data_in_json (data: (int * ((int * int) list)) list) =
`List (List.map (fun (line_id,xy) -> `Assoc [
("line id", `Int line_id);
("data", simple_data_in_json xy)]) data)
let max_y_of_data data =
data
|> List.map (fun (_,lst) -> lst |> List.map (fun (_,y) -> y) |> max)
|> max
(* given a list of (x,y), create the fill in corridates and termination point *)
let rec fill_points term_x (data: (int * int) list) =
match data with
| (x1,y1)::(x2,y2)::rest -> (x1,y1) :: (x2,y1) :: (fill_points term_x ((x2,y2)::rest))
| (x1,y1)::[] -> [(x1,y1); (term_x,y1)]
| [] -> []