From f29b3c83dd653bf833f937833a74d1e8cad5a488 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sat, 21 Aug 2021 16:57:06 +0530 Subject: feat: add y-axis --- src/chart.ml | 20 +++++++++++++++++--- src/utils.ml | 2 ++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/chart.ml b/src/chart.ml index 39f97f9..807f026 100644 --- a/src/chart.ml +++ b/src/chart.ml @@ -8,11 +8,14 @@ let with_options v c = { c with opts = v } let pp formatter { vals : int list; opts : Options.t } = let open Utils in - let height = opts.height in + let height = opts.height - 1 in let min_val = list_min vals in let max_val = list_max vals in let range = max_val - min_val in - let scale v = (v - min_val) * height / range in + let scale v = + int_of_float + (float_of_int (v - min_val) /. float_of_int range *. float_of_int height) + in let new_vals = List.map scale vals in let rec pad left right s = if left = 0 then s :: repeat right space @@ -51,7 +54,18 @@ let pp formatter { vals : int list; opts : Options.t } = | (s, e) :: tl -> make_marker axis s (height - s) m :: make_connector s e :: make_graph tl in - make_graph new_vals' |> rot_left + let unscale v = + (float_of_int (v * range) /. float_of_int height) +. float_of_int min_val + in + let y_vals = + Format.sprintf "%10s" "┤" + :: + List.map + (fun x -> Format.sprintf "%6.2f ┤" (unscale x)) + (0 -- (height + 1)) + in + y_vals :: make_graph new_vals' + |> rot_left |> List.map (String.concat "") |> String.concat "\n" |> Format.fprintf formatter "%s" diff --git a/src/utils.ml b/src/utils.ml index 59eac6c..b3dbbda 100644 --- a/src/utils.ml +++ b/src/utils.ml @@ -37,3 +37,5 @@ let space = to_string ' ' let axis = "┬" let axis_spc = "─" + +let rec ( -- ) i j = if i >= j then [] else i :: (i + 1 -- j) -- cgit v1.2.3