Class: UnicodePlot::Barplot

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

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

#print_color, #print_styled

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



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

def max_freq
  @max_freq
end

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

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

#n_columnsObject



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

def n_columns
  @width
end

#n_rowsObject



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

def n_rows
  @bars.length
end


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