Raxol.UI.Charts.ChartUtils (Raxol v2.6.0)

View Source

Shared utilities for chart rendering: scaling, range computation, axis/legend cell generation, and data normalization.

Summary

Functions

Computes a display range with 5% padding on each side. Returns {min, max}. Single-value and empty inputs are handled gracefully.

Computes an auto range for 2D {x, y} data. Returns {{x_min, x_max}, {y_min, y_max}}.

Computes the plot region within a chart area, reserving space for axes and legend.

Formats a number as an axis label string with configurable precision.

Formats a number with 1 decimal place.

Normalizes a list or CircularBuffer to a plain list of numbers.

Normalizes a list or CircularBuffer of {x, y} tuples to a plain list.

Renders Y-axis labels and X-axis line as cell tuples.

Renders a legend showing series names with their colors.

Resolves a Y-axis range from data values and opts (:min, :max). Falls back to auto_range/1 for :auto bounds. Empty data defaults to {0.0, 1.0}.

Linearly scales a value from [min, max] to [new_min, new_max]. Returns new_min when min == max to avoid division by zero.

Converts a string into a list of cell tuples placed at consecutive X positions.

Summarizes series for MCP tool output: name, color, and normalized 1D data.

Summarizes series for MCP tool output with 2D {x, y} data.

Types

cell()

@type cell() ::
  {non_neg_integer(), non_neg_integer(), String.t(), atom(), atom(), map()}

Functions

auto_range(values)

@spec auto_range([number()]) :: {number(), number()}

Computes a display range with 5% padding on each side. Returns {min, max}. Single-value and empty inputs are handled gracefully.

auto_range_2d(points)

@spec auto_range_2d([{number(), number()}]) ::
  {{number(), number()}, {number(), number()}}

Computes an auto range for 2D {x, y} data. Returns {{x_min, x_max}, {y_min, y_max}}.

clamp(val, lo, hi)

See Raxol.Core.Utils.Math.clamp/3.

compute_plot_region(x, y, w, h, show_axes, show_legend)

@spec compute_plot_region(
  non_neg_integer(),
  non_neg_integer(),
  pos_integer(),
  pos_integer(),
  boolean(),
  boolean()
) :: map()

Computes the plot region within a chart area, reserving space for axes and legend.

format_axis_label(value, precision)

@spec format_axis_label(number(), 0..253) :: String.t()

Formats a number as an axis label string with configurable precision.

format_number(value)

@spec format_number(number()) :: String.t()

Formats a number with 1 decimal place.

normalize_data(data)

@spec normalize_data(list() | struct()) :: [number()]

Normalizes a list or CircularBuffer to a plain list of numbers.

normalize_data_2d(data)

@spec normalize_data_2d(list() | struct()) :: [{number(), number()}]

Normalizes a list or CircularBuffer of {x, y} tuples to a plain list.

render_axes(arg1, arg2, opts \\ [])

@spec render_axes(
  {non_neg_integer(), non_neg_integer(), pos_integer(), pos_integer()},
  {number(), number()},
  keyword()
) :: [cell()]

Renders Y-axis labels and X-axis line as cell tuples.

render_legend(x, y, series)

@spec render_legend(non_neg_integer(), non_neg_integer(), [map()]) :: [cell()]

Renders a legend showing series names with their colors.

resolve_range(values, opts)

@spec resolve_range(
  [number()],
  keyword()
) :: {number(), number()}

Resolves a Y-axis range from data values and opts (:min, :max). Falls back to auto_range/1 for :auto bounds. Empty data defaults to {0.0, 1.0}.

scale_value(value, same, same, new_min, new_max)

@spec scale_value(number(), number(), number(), number(), number()) :: float()

Linearly scales a value from [min, max] to [new_min, new_max]. Returns new_min when min == max to avoid division by zero.

string_to_cells(string, x, y, fg, bg)

@spec string_to_cells(
  String.t(),
  non_neg_integer(),
  non_neg_integer(),
  atom(),
  atom()
) :: [cell()]

Converts a string into a list of cell tuples placed at consecutive X positions.

summarize_series(series)

@spec summarize_series([map()]) :: [map()]

Summarizes series for MCP tool output: name, color, and normalized 1D data.

summarize_series_2d(series)

@spec summarize_series_2d([map()]) :: [map()]

Summarizes series for MCP tool output with 2D {x, y} data.