Class: MiniHistogram::MiniUnicodePlot::Barplot

Inherits:
Plot
  • Object
show all
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

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

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(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

Instance Attribute Details

#max_freqObject (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_lenObject (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

#widthObject (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)
  @bars.concat(bars)
  @max_freq, i = find_max(transform_values(@transform, bars))
  @max_len = @bars[i].to_s.length
end

#n_columnsObject



743
744
745
# File 'lib/mini_histogram/plot.rb', line 743

def n_columns
  @width
end

#n_rowsObject



739
740
741
# File 'lib/mini_histogram/plot.rb', line 739

def n_rows
  @bars.length
end


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)
  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