Class: MiniHistogram::MiniUnicodePlot::Barplot
- Includes:
- ValueTransformer
- Defined in:
- lib/mini_histogram/plot.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
#color?, #print_color, #print_styled
Constructor Details
#initialize(bars, width, color, symbol, transform, **kw) ⇒ Barplot
Returns a new instance of Barplot.
720 721 722 723 724 725 726 727 728 729 730 731 732 733 |
# File 'lib/mini_histogram/plot.rb', line 720 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.
735 736 737 |
# File 'lib/mini_histogram/plot.rb', line 735 def max_freq @max_freq end |
#max_len ⇒ Object (readonly)
Returns the value of attribute max_len.
736 737 738 |
# File 'lib/mini_histogram/plot.rb', line 736 def max_len @max_len end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
737 738 739 |
# File 'lib/mini_histogram/plot.rb', line 737 def width @width end |
Instance Method Details
#add_row!(bars) ⇒ Object
747 748 749 750 751 |
# File 'lib/mini_histogram/plot.rb', line 747 def add_row!() @bars.concat() @max_freq, i = find_max(transform_values(@transform, )) @max_len = @bars[i].to_s.length end |
#n_columns ⇒ Object
743 744 745 |
# File 'lib/mini_histogram/plot.rb', line 743 def n_columns @width end |
#n_rows ⇒ Object
739 740 741 |
# File 'lib/mini_histogram/plot.rb', line 739 def n_rows @bars.length end |
#print_row(out, row_index) ⇒ Object
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 |
# File 'lib/mini_histogram/plot.rb', line 753 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 |