ExRatatui.Widgets.Chart (ExRatatui v0.8.2)

Copy Markdown View Source

An XY plotting widget for line, scatter, and bar visualizations.

Wraps Ratatui's Chart widget. Each :datasets entry is a ExRatatui.Widgets.Chart.Dataset struct carrying its own (x, y) pairs, marker, graph type, and color. Both axes are required and configured via ExRatatui.Widgets.Chart.Axis structs — the caller controls :bounds, optional :title, tick :labels, and label alignment.

Graph types

Each dataset chooses how its points are drawn:

  • :line - connects consecutive points with line segments
  • :scatter - draws one marker per point (default-friendly for sparse data)
  • :bar - draws a vertical bar from the X axis up to each point

Markers

The :marker on each dataset controls cell-level pixel resolution. Valid values mirror ExRatatui.Widgets.Canvas: :braille (default), :dot, :block, :bar, :half_block.

Legend

:legend_position accepts :top, :top_left, :top_right (default), :bottom, :bottom_left, :bottom_right, :left, :right, or nil to hide the legend entirely. :hidden_legend_constraints takes a {horizontal, vertical} tuple of Constraints describing the smallest area in which the legend will still render — handy when the chart shrinks below a usable size.

Fields

  • :datasets - list of %ExRatatui.Widgets.Chart.Dataset{} (default [])
  • :x_axis - required %ExRatatui.Widgets.Chart.Axis{}
  • :y_axis - required %ExRatatui.Widgets.Chart.Axis{}
  • :legend_position - position atom (default :top_right) or nil to hide
  • :hidden_legend_constraints - {Constraint.t(), Constraint.t()} or nil
  • :block - optional %ExRatatui.Widgets.Block{} container

Examples

iex> alias ExRatatui.Widgets.Chart
iex> alias ExRatatui.Widgets.Chart.{Axis, Dataset}
iex> %Chart{
...>   datasets: [%Dataset{name: "cpu", data: [{0.0, 1.0}, {1.0, 2.0}]}],
...>   x_axis: %Axis{bounds: {0.0, 10.0}},
...>   y_axis: %Axis{bounds: {0.0, 10.0}}
...> }
%ExRatatui.Widgets.Chart{
  datasets: [
    %ExRatatui.Widgets.Chart.Dataset{
      name: "cpu",
      data: [{0.0, 1.0}, {1.0, 2.0}],
      marker: :braille,
      graph_type: :line,
      style: %ExRatatui.Style{}
    }
  ],
  x_axis: %ExRatatui.Widgets.Chart.Axis{
    title: nil,
    bounds: {0.0, 10.0},
    labels: [],
    style: %ExRatatui.Style{},
    labels_alignment: :left
  },
  y_axis: %ExRatatui.Widgets.Chart.Axis{
    title: nil,
    bounds: {0.0, 10.0},
    labels: [],
    style: %ExRatatui.Style{},
    labels_alignment: :left
  },
  legend_position: :top_right,
  hidden_legend_constraints: nil,
  block: nil
}

Summary

Types

legend_position()

@type legend_position() ::
  :top
  | :top_left
  | :top_right
  | :bottom
  | :bottom_left
  | :bottom_right
  | :left
  | :right
  | nil

t()

@type t() :: %ExRatatui.Widgets.Chart{
  block: ExRatatui.Widgets.Block.t() | nil,
  datasets: [ExRatatui.Widgets.Chart.Dataset.t()],
  hidden_legend_constraints:
    {ExRatatui.Layout.constraint(), ExRatatui.Layout.constraint()} | nil,
  legend_position: legend_position(),
  x_axis: ExRatatui.Widgets.Chart.Axis.t(),
  y_axis: ExRatatui.Widgets.Chart.Axis.t()
}