Class: UnicodePlot::Barplot
- Includes:
- ValueTransformer
- Defined in:
- src/lib/unicode_plot/barplot.rb
Constant Summary collapse
- MIN_WIDTH =
10
- DEFAULT_COLOR =
:green
- DEFAULT_SYMBOL =
"■"
Constants included from ValueTransformer
ValueTransformer::PREDEFINED_TRANSFORM_FUNCTIONS
Constants inherited from Plot
Plot::COLOR_CYCLE, Plot::DEFAULT_BORDER, Plot::DEFAULT_MARGIN, Plot::DEFAULT_PADDING, Plot::DEFAULT_WIDTH
Constants included from StyledPrinter
StyledPrinter::COLOR_DECODE, StyledPrinter::COLOR_ENCODE, StyledPrinter::DISABLE_TEXT_STYLE, StyledPrinter::TEXT_COLORS
Instance Attribute Summary collapse
-
#max_freq ⇒ Object
readonly
Returns the value of attribute max_freq.
-
#max_len ⇒ Object
readonly
Returns the value of attribute max_len.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Attributes inherited from Plot
#border, #colors_deco, #colors_left, #colors_right, #decorations, #labels_left, #labels_right, #margin, #padding, #title, #xlabel, #ylabel
Instance Method Summary collapse
-
#add_row!(bars) ⇒ Object
-
#initialize(bars, width, color, symbol, transform, **kw) ⇒ Barplot
constructor
A new instance of Barplot.
-
#n_columns ⇒ Object
-
#n_rows ⇒ Object
-
#print_row(out, row_index) ⇒ Object
Methods included from ValueTransformer
transform_name, #transform_values
Methods inherited from Plot
#annotate!, #annotate_row!, #next_color, #render, #show_labels?, #title_given?, #to_s, #xlabel_given?, #ylabel_given?, #ylabel_length
Methods included from StyledPrinter
Constructor Details
#initialize(bars, width, color, symbol, transform, **kw) ⇒ Barplot
Returns a new instance of Barplot.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'src/lib/unicode_plot/barplot.rb', line 9 def initialize(, width, color, symbol, transform, **kw) if symbol.length > 1 raise ArgumentError, "symbol must be a single character" end @bars = @symbol = symbol @max_freq, i = find_max(transform_values(transform, )) @max_len = [i].to_s.length @width = [width, max_len + 7, MIN_WIDTH].max @color = color @symbol = symbol @transform = transform super(**kw) end |
Instance Attribute Details
#max_freq ⇒ Object (readonly)
Returns the value of attribute max_freq.
24 25 26 |
# File 'src/lib/unicode_plot/barplot.rb', line 24 def max_freq @max_freq end |
#max_len ⇒ Object (readonly)
Returns the value of attribute max_len.
25 26 27 |
# File 'src/lib/unicode_plot/barplot.rb', line 25 def max_len @max_len end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
26 27 28 |
# File 'src/lib/unicode_plot/barplot.rb', line 26 def width @width end |
Instance Method Details
#add_row!(bars) ⇒ Object
36 37 38 39 40 |
# File 'src/lib/unicode_plot/barplot.rb', line 36 def add_row!() @bars.concat() @max_freq, i = find_max(transform_values(@transform, )) @max_len = @bars[i].to_s.length end |
#n_columns ⇒ Object
32 33 34 |
# File 'src/lib/unicode_plot/barplot.rb', line 32 def n_columns @width end |
#n_rows ⇒ Object
28 29 30 |
# File 'src/lib/unicode_plot/barplot.rb', line 28 def n_rows @bars.length end |
#print_row(out, row_index) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'src/lib/unicode_plot/barplot.rb', line 42 def print_row(out, row_index) check_row_index(row_index) = @bars[row_index] = [width - 2 - max_len, 1].max val = transform_values(@transform, ) = max_freq > 0 ? ([val, 0].max.fdiv(max_freq) * ).round : 0 = max_freq > 0 ? @symbol * : "" = .to_s print_styled(out, , color: @color) print_styled(out, " ", , color: :normal) pan_len = [ + 1 + max_len - - .length, 0].max pad = " " * pan_len.round out.print(pad) end |