Class: UnicodePlot::Barplot

Inherits:
Plot
  • Object
show all
Includes:
ValueTransformer
Defined in:
lib/unicode_plot/barplot.rb

Constant Summary collapse

MIN_WIDTH =
10
DEFAULT_WIDTH =
40
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

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.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/unicode_plot/barplot.rb', line 10

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.



25
26
27
# File 'lib/unicode_plot/barplot.rb', line 25

def max_freq
  @max_freq
end

#max_lenObject (readonly)

Returns the value of attribute max_len.



26
27
28
# File 'lib/unicode_plot/barplot.rb', line 26

def max_len
  @max_len
end

#widthObject (readonly)

Returns the value of attribute width.



27
28
29
# File 'lib/unicode_plot/barplot.rb', line 27

def width
  @width
end

Instance Method Details

#n_columnsObject



33
34
35
# File 'lib/unicode_plot/barplot.rb', line 33

def n_columns
  @width
end

#n_rowsObject



29
30
31
# File 'lib/unicode_plot/barplot.rb', line 29

def n_rows
  @bars.length
end


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/unicode_plot/barplot.rb', line 37

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