Class: RubyTUI::Widgets::Chart
- Inherits:
-
Object
- Object
- RubyTUI::Widgets::Chart
- Includes:
- RubyTUI::Widget
- Defined in:
- lib/rubytui/widgets/chart.rb
Overview
Chart renders line charts using Canvas (Braille) for high-resolution plotting. Supports multiple datasets, labeled axes, and automatic scaling.
Instance Method Summary collapse
-
#initialize(datasets: [], x_axis: nil, y_axis: nil, style: Style::DEFAULT, axis_style: nil, label_style: nil, block: nil) ⇒ Chart
constructor
A new instance of Chart.
-
#render(area, buf) ⇒ void
Render the axes and draw each dataset as connected line segments on a Canvas.
Constructor Details
#initialize(datasets: [], x_axis: nil, y_axis: nil, style: Style::DEFAULT, axis_style: nil, label_style: nil, block: nil) ⇒ Chart
Returns a new instance of Chart.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/rubytui/widgets/chart.rb', line 44 def initialize( datasets: [], x_axis: nil, # [label, min, max] or nil for auto y_axis: nil, # [label, min, max] or nil for auto style: Style::DEFAULT, axis_style: nil, label_style: nil, block: nil ) @datasets = datasets @x_axis = x_axis @y_axis = y_axis @style = style @axis_style = axis_style || Style.new.fg(Color::BRIGHT_BLACK) @label_style = label_style || Style.new.fg(Color::WHITE) @block = block end |
Instance Method Details
#render(area, buf) ⇒ void
This method returns an undefined value.
Render the axes and draw each dataset as connected line segments on a RubyTUI::Widgets::Canvas.
Nothing is drawn, not even block, when there are no datasets.
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 |
# File 'lib/rubytui/widgets/chart.rb', line 69 def render(area, buf) return if area.empty? || @datasets.empty? render_area = area if @block @block.render(area, buf) render_area = @block.inner(area) end return if render_area.empty? # Reserve space for axes y_label_width = compute_y_label_width x_label_height = @x_axis ? 1 : 0 y_label_area_width = y_label_width > 0 ? y_label_width + 1 : 0 chart_area = Rect.new( x: render_area.x + y_label_area_width, y: render_area.y, width: [render_area.width - y_label_area_width, 0].max, height: [render_area.height - x_label_height, 0].max ) return if chart_area.empty? # Compute data bounds x_min, x_max = compute_x_bounds y_min, y_max = compute_y_bounds # Draw axes render_y_axis(render_area, chart_area, buf, y_min, y_max) render_x_axis(render_area, chart_area, buf, x_min, x_max) # Draw data using Canvas canvas = Canvas.new( x_bounds: [x_min, x_max], y_bounds: [y_min, y_max] ) @datasets.each do |ds| points = ds.data next if points.length < 2 # Draw lines between consecutive points points.each_cons(2) do |(x1, y1), (x2, y2)| canvas.line(x1, y1, x2, y2, style: ds.style) end end canvas.render(chart_area, buf) end |