Class: Charming::Components::Chart

Inherits:
Charming::Component show all
Defined in:
lib/charming/presentation/components/chart.rb

Overview

Chart plots a numeric series into a width×height box of character cells. kind: :line (default) draws a connected line on a UI::BrailleCanvas (subpixel resolution); kind: :bar draws vertical eighth-block bars. Pure text — works on every terminal — and composes with row/column/box. Pass an optional style: (UI::Style) to colour it.

Constant Summary collapse

VBARS =

Vertical fill levels for bar mode, empty (0) to full (8 eighths).

[" ", "", "", "", "", "", "", "", ""].freeze

Instance Method Summary collapse

Methods inherited from Charming::Component

#captures_text?

Methods inherited from View

#focused?, #layout_assigns

Constructor Details

#initialize(series:, width:, height:, kind: :line, style: nil, theme: nil) ⇒ Chart

series is the numeric data. width/height size the chart in character cells. kind is :line or :bar. style optionally paints the result. theme is forwarded.



15
16
17
18
19
20
21
22
# File 'lib/charming/presentation/components/chart.rb', line 15

def initialize(series:, width:, height:, kind: :line, style: nil, theme: nil)
  super(theme: theme)
  @series = series
  @width = width
  @height = height
  @kind = kind
  @style = style
end

Instance Method Details

#renderObject

Renders the chart (empty string for an empty series or a non-positive box).



25
26
27
28
29
30
# File 'lib/charming/presentation/components/chart.rb', line 25

def render
  return "" if @series.empty? || @width < 1 || @height < 1

  body = (@kind == :bar) ? bars : line_plot
  @style ? @style.render(body) : body
end