class UnicodePlot::Barplot
Constants
- DEFAULT_COLOR
- DEFAULT_SYMBOL
- MIN_WIDTH
Attributes
max_freq[R]
max_len[R]
width[R]
Public Class Methods
new(bars, width, color, symbol, transform, **kw)
click to toggle source
Calls superclass method
# File lib/unicode_plot/barplot.rb, line 9 def initialize(bars, width, color, symbol, transform, **kw) if symbol.length > 1 raise ArgumentError, "symbol must be a single character" end @bars = bars @symbol = symbol @max_freq, i = find_max(transform_values(transform, bars)) @max_len = bars[i].to_s.length @width = [width, max_len + 7, MIN_WIDTH].max @color = color @symbol = symbol @transform = transform super(**kw) end
Public Instance Methods
add_row!(bars)
click to toggle source
# File lib/unicode_plot/barplot.rb, line 36 def add_row!(bars) @bars.concat(bars) @max_freq, i = find_max(transform_values(@transform, bars)) @max_len = @bars[i].to_s.length end
n_columns()
click to toggle source
# File lib/unicode_plot/barplot.rb, line 32 def n_columns @width end
n_rows()
click to toggle source
# File lib/unicode_plot/barplot.rb, line 28 def n_rows @bars.length end
print_row(out, row_index)
click to toggle source
# File lib/unicode_plot/barplot.rb, line 42 def print_row(out, row_index) check_row_index(row_index) bar = @bars[row_index] max_bar_width = [width - 2 - max_len, 1].max val = transform_values(@transform, bar) bar_len = max_freq > 0 ? ([val, 0].max.fdiv(max_freq) * max_bar_width).round : 0 bar_str = max_freq > 0 ? @symbol * bar_len : "" bar_lbl = bar.to_s print_styled(out, bar_str, color: @color) print_styled(out, " ", bar_lbl, color: :normal) pan_len = [max_bar_width + 1 + max_len - bar_len - bar_lbl.length, 0].max pad = " " * pan_len.round out.print(pad) end
Private Instance Methods
find_max(values)
click to toggle source
# File lib/unicode_plot/barplot.rb, line 59 def find_max(values) i = j = 0 max = values[i] while j < values.length if values[j] > max i, max = j, values[j] end j += 1 end [max, i] end