package miaou-core
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>
Miaou core/widgets (no drivers, no SDL)
Install
dune-project
Dependency
Authors
Maintainers
Sources
v0.5.2.tar.gz
md5=60a3b9f181f24572a06a9492532bfdda
sha512=fcc35a275066be2900e6201782faf47503076fa4640f08cf78067835a6f447b74613009e55b2ac799adb7ca46f1bffa261fc5971753f2cc3c6bef327511c7ef6
doc/src/miaou_widgets_display/line_chart_widget.ml.html
Source file line_chart_widget.ml
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458(*****************************************************************************) (* *) (* SPDX-License-Identifier: MIT *) (* Copyright (c) 2025 Nomadic Labs <contact@nomadic-labs.com> *) (* Copyright (c) 2026 Mathias Bourgoin <mathias.bourgoin@atacama.tech> *) (* *) (*****************************************************************************) module W = Widgets type point = {x : float; y : float; color : string option} type series = {label : string; points : point list; color : string option} type threshold = {value : float; color : string} type render_mode = ASCII | Braille | Octant type axis_config = { show_labels : bool; x_label : string; y_label : string; x_ticks : int; y_ticks : int; } let default_axis_config = {show_labels = false; x_label = ""; y_label = ""; x_ticks = 5; y_ticks = 5} type t = { width : int; height : int; series : series list; title : string option; axis_config : axis_config; } let create ~width ~height ~series ?title ?(axis_config = default_axis_config) () = {width; height; series; title; axis_config} let update_series t ~label ~points = let series = List.map (fun s -> if s.label = label then {s with points} else s) t.series in {t with series} let add_point t ~label ~point = let series = List.map (fun s -> if s.label = label then {s with points = point :: s.points} else s) t.series in (* Reverse points list when rendering if order matters, or keep as-is for perf *) {t with series} let set_axis_config t axis_config = {t with axis_config} (* Grid canvas: mutable character grid *) type cell = {mutable char : string; mutable style : string option} let make_grid width height = Array.init height (fun _ -> Array.init width (fun _ -> {char = " "; style = None})) let set_cell grid x y char = if y >= 0 && y < Array.length grid && x >= 0 && x < Array.length grid.(0) then grid.(y).(x).char <- char let set_cell_styled grid x y char style = if y >= 0 && y < Array.length grid && x >= 0 && x < Array.length grid.(0) then ( grid.(y).(x).char <- char ; grid.(y).(x).style <- Some style) (* Coordinate mapping *) let map_x x x_min x_max width = let range = x_max -. x_min in if range = 0. then width / 2 else int_of_float ((x -. x_min) /. range *. float_of_int (width - 1)) let map_y y y_min y_max height = let range = y_max -. y_min in if range = 0. then height / 2 else (* Invert Y because terminal coordinates go top-down *) height - 1 - int_of_float ((y -. y_min) /. range *. float_of_int (height - 1)) (* Simplified line drawing *) let draw_line grid x1 y1 x2 y2 char style = let dx = abs (x2 - x1) in let dy = abs (y2 - y1) in let sx = if x1 < x2 then 1 else -1 in let sy = if y1 < y2 then 1 else -1 in let rec loop x y err = (match style with | Some s -> set_cell_styled grid x y char s | None -> set_cell grid x y char) ; if x = x2 && y = y2 then () else let e2 = 2 * err in let x', err' = if e2 > -dy then (x + sx, err - dy) else (x, err) in let y', err'' = if e2 < dx then (y + sy, err' + dx) else (y, err') in loop x' y' err'' in loop x1 y1 (dx - dy) (* Render axes *) let render_axes grid _t _x_min _x_max _y_min _y_max = let width = Array.length grid.(0) in let height = Array.length grid in (* Y-axis (left edge) *) for y = 0 to height - 1 do set_cell grid 0 y (if W.prefer_ascii () then "|" else "│") done ; set_cell grid 0 (height - 1) (if W.prefer_ascii () then "+" else "└") ; (* X-axis (bottom edge) *) for x = 0 to width - 1 do set_cell grid x (height - 1) (if W.prefer_ascii () then "-" else "─") done ; set_cell grid 0 (height - 1) (if W.prefer_ascii () then "+" else "└") (* Render grid lines *) let render_grid_lines grid t = let width = Array.length grid.(0) in let height = Array.length grid in let x_step = max 1 (width / (t.axis_config.x_ticks + 1)) in let y_step = max 1 (height / (t.axis_config.y_ticks + 1)) in (* Vertical grid lines *) for i = 1 to t.axis_config.x_ticks do let x = min (width - 1) (i * x_step) in for y = 0 to height - 2 do if grid.(y).(x).char = " " then set_cell grid x y (if W.prefer_ascii () then ":" else "┊") done done ; (* Horizontal grid lines *) for i = 1 to t.axis_config.y_ticks do let y = min (height - 2) (i * y_step) in for x = 1 to width - 1 do if grid.(y).(x).char = " " then set_cell grid x y (if W.prefer_ascii () then "." else "┈") done done let get_color ~thresholds ~series_color (point : point) : string option = let a = List.sort (fun a b -> Float.compare b.value a.value) thresholds in match point.color with | Some _ -> point.color | None -> ( match List.find_opt (fun t -> point.y > t.value) a with | Some t -> Some t.color | None -> series_color) (* Plot a single series *) let plot_series grid (series : series) x_min x_max y_min y_max width height symbol ~thresholds = List.iteri (fun idx (point : point) -> let x = map_x point.x x_min x_max width in let y = map_y point.y y_min y_max height in let color = get_color ~thresholds ~series_color:series.color point in (match color with | Some c -> set_cell_styled grid x y symbol c | None -> set_cell grid x y symbol) ; (* Draw line to next point *) match List.nth_opt series.points (idx + 1) with | Some next_point -> let next_x = map_x next_point.x x_min x_max width in let next_y = map_y next_point.y y_min y_max height in let line_char = if W.prefer_ascii () then "-" else "●" in draw_line grid x y next_x next_y line_char color | None -> ()) series.points (* Calculate data bounds *) let calculate_bounds series_list = let all_points = List.concat_map (fun s -> s.points) series_list in if all_points = [] then (0., 0., 0., 0.) else let xs = List.map (fun p -> p.x) all_points in let ys = List.map (fun p -> p.y) all_points in let x_min, x_max = Chart_utils.bounds xs in let y_min, y_max = Chart_utils.bounds ys in (x_min, x_max, y_min, y_max) let render_octant t ~thresholds = (* Octant line chart: same structure as Braille but with per-series color *) let canvas = Octant_canvas.create ~width:t.width ~height:t.height in let dot_width, dot_height = Octant_canvas.get_dot_dimensions canvas in let has_colors = thresholds <> [] || List.exists (fun (s : series) -> s.color <> None || List.exists (fun (p : point) -> p.color <> None) s.points) t.series in let x_min, x_max, y_min, y_max = calculate_bounds t.series in let x_range = x_max -. x_min in let y_range = y_max -. y_min in let inv_x = if x_range = 0. then 0. else 1. /. x_range in let inv_y = if y_range = 0. then 0. else 1. /. y_range in let map_x x = if x_range = 0. then dot_width / 2 else int_of_float ((x -. x_min) *. inv_x *. float_of_int (dot_width - 1)) in let map_y y = if y_range = 0. then dot_height / 2 else dot_height - 1 - int_of_float ((y -. y_min) *. inv_y *. float_of_int (dot_height - 1)) in List.iter (fun (s : series) -> let sorted_thresholds = if has_colors then List.sort (fun a b -> Float.compare b.value a.value) thresholds else [] in let get_color_cached (point : point) : string option = match point.color with | Some c -> Some c | None -> ( match List.find_opt (fun t -> point.y > t.value) sorted_thresholds with | Some t -> Some t.color | None -> s.color) in List.iteri (fun idx (point : point) -> let x = map_x point.x in let y = map_y point.y in let color = if has_colors then get_color_cached point else s.color in Octant_canvas.set_dot canvas ~x ~y ~color ; match List.nth_opt s.points (idx + 1) with | Some next_point -> let next_x = map_x next_point.x in let next_y = map_y next_point.y in Octant_canvas.draw_line canvas ~x0:x ~y0:y ~x1:next_x ~y1:next_y ~color | None -> ()) s.points) t.series ; let chart_output = Octant_canvas.render canvas in match t.title with | Some title -> W.themed_emphasis title ^ "\n" ^ chart_output | None -> chart_output let render t ~show_axes ~show_grid ?(thresholds = []) ?(mode = ASCII) () = match mode with | Octant -> render_octant t ~thresholds | ASCII -> ( let grid = make_grid t.width t.height in let x_min, x_max, y_min, y_max = calculate_bounds t.series in (* Render grid lines first (background) *) if show_grid then render_grid_lines grid t ; (* Render axes *) if show_axes then render_axes grid t x_min x_max y_min y_max ; (* Plot symbols for different series *) let symbols = [|"●"; "■"; "▲"; "◆"; "★"|] in (* Plot each series *) List.iteri (fun idx series -> let symbol = symbols.(idx mod Array.length symbols) in plot_series grid series x_min x_max y_min y_max t.width t.height symbol ~thresholds) t.series ; (* Convert grid to string *) let lines = Array.to_list grid |> List.map (fun row -> let buf = Buffer.create (t.width * 10) in Array.iter (fun cell -> match cell.style with | Some style -> Buffer.add_string buf (W.ansi style cell.char) | None -> Buffer.add_string buf cell.char) row ; Buffer.contents buf) in (* Add title if present *) match t.title with | Some title -> let buf = Buffer.create (String.length title + (List.length lines * t.width) + 1) in Buffer.add_string buf (W.themed_emphasis title) ; Buffer.add_char buf '\n' ; List.iteri (fun i line -> if i > 0 then Buffer.add_char buf '\n' ; Buffer.add_string buf line) lines ; Buffer.contents buf | None -> let buf = Buffer.create (List.length lines * (t.width + 1)) in List.iteri (fun i line -> if i > 0 then Buffer.add_char buf '\n' ; Buffer.add_string buf line) lines ; Buffer.contents buf) | Braille -> ( (* Use braille canvas for higher resolution *) let canvas = Braille_canvas.create ~width:t.width ~height:t.height in let width_cells, height_cells = Braille_canvas.get_dimensions canvas in let has_colors = thresholds <> [] || List.exists (fun (s : series) -> s.color <> None || List.exists (fun (p : point) -> p.color <> None) s.points) t.series in let styles = if has_colors then Some (Array.make_matrix height_cells width_cells None) else None in let dot_width, dot_height = Braille_canvas.get_dot_dimensions canvas in let x_min, x_max, y_min, y_max = calculate_bounds t.series in let x_range = x_max -. x_min in let y_range = y_max -. y_min in let inv_x = if x_range = 0. then 0. else 1. /. x_range and inv_y = if y_range = 0. then 0. else 1. /. y_range in (* Map coordinate to dot position *) let map_x x = if x_range = 0. then dot_width / 2 else int_of_float ((x -. x_min) *. inv_x *. float_of_int (dot_width - 1)) in let map_y y = if y_range = 0. then dot_height / 2 else (* Invert Y because terminal coordinates go top-down *) dot_height - 1 - int_of_float ((y -. y_min) *. inv_y *. float_of_int (dot_height - 1)) in (* Plot each series *) let set_styled_dot x y color = (match styles with | Some s -> let cell_x = x / 2 in let cell_y = y / 4 in if cell_y < height_cells && cell_x < width_cells then s.(cell_y).(cell_x) <- color | None -> ()) ; Braille_canvas.set_dot canvas ~x ~y in let draw_line_styled ~x0 ~y0 ~x1 ~y1 color = let dx = abs (x1 - x0) in let dy = abs (y1 - y0) in let sx = if x0 < x1 then 1 else -1 in let sy = if y0 < y1 then 1 else -1 in let rec loop x y err = set_styled_dot x y color ; if x = x1 && y = y1 then () else let e2 = 2 * err in let x', err' = if e2 > -dy then (x + sx, err - dy) else (x, err) in let y', err'' = if e2 < dx then (y + sy, err' + dx) else (y, err') in loop x' y' err'' in loop x0 y0 (dx - dy) in List.iter (fun series -> let sorted_thresholds = if has_colors then List.sort (fun a b -> Float.compare b.value a.value) thresholds else [] in let get_color_cached (point : point) : string option = match point.color with | Some c -> Some c | None -> ( match List.find_opt (fun t -> point.y > t.value) sorted_thresholds with | Some t -> Some t.color | None -> (series : series).color) in List.iteri (fun idx point -> let x = map_x point.x in let y = map_y point.y in let color = if has_colors then get_color_cached point else None in set_styled_dot x y color ; (* Draw line to next point *) match List.nth_opt series.points (idx + 1) with | Some next_point -> let next_x = map_x next_point.x in let next_y = map_y next_point.y in draw_line_styled ~x0:x ~y0:y ~x1:next_x ~y1:next_y color | None -> ()) series.points) t.series ; let chart_output = match styles with | Some s -> Chart_utils.render_braille_with_colors canvas s | None -> Braille_canvas.render canvas in (* Add title if present *) match t.title with | Some title -> W.themed_emphasis title ^ "\n" ^ chart_output | None -> chart_output) (* Accessor functions for SDL rendering *) let get_series t = t.series let get_title t = t.title let get_dimensions t = (t.width, t.height) let () = Miaou_registry.register ~name:"line_chart" ~mli:[%blob "line_chart_widget.mli"] ()
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>