Class: UnicodePlot::Boxplot

Inherits:
Plot
  • Object
show all
Defined in:
src/lib/unicode_plot/boxplot.rb

Constant Summary collapse

MIN_WIDTH =
10
DEFAULT_COLOR =
:green

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 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(data, width, color, min_x, max_x, **kw) ⇒ Boxplot

Returns a new instance of Boxplot.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'src/lib/unicode_plot/boxplot.rb', line 8

def initialize(data, width, color, min_x, max_x, **kw)
  if min_x == max_x
    min_x -= 1
    max_x += 1
  end
  width = [width, MIN_WIDTH].max
  @data = [data.percentile([0, 25, 50, 75, 100])]
  @color = color
  @width = [width, MIN_WIDTH].max
  @min_x = min_x
  @max_x = max_x
  super(**kw)
end

Instance Attribute Details

#max_xObject (readonly)

Returns the value of attribute max_x.



23
24
25
# File 'src/lib/unicode_plot/boxplot.rb', line 23

def max_x
  @max_x
end

#min_xObject (readonly)

Returns the value of attribute min_x.



22
23
24
# File 'src/lib/unicode_plot/boxplot.rb', line 22

def min_x
  @min_x
end

Instance Method Details

#add_series!(data) ⇒ Object



37
38
39
40
41
42
# File 'src/lib/unicode_plot/boxplot.rb', line 37

def add_series!(data)
  mi, ma = data.minmax
  @data << data.percentile([0, 25, 50, 75, 100])
  @min_x = [mi, @min_x].min
  @max_x = [ma, @max_x].max
end

#n_columnsObject



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

def n_columns
  @width
end

#n_dataObject



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

def n_data
  @data.length
end

#n_rowsObject



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

def n_rows
  3 * @data.length
end


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'src/lib/unicode_plot/boxplot.rb', line 44

def print_row(out, row_index)
  check_row_index(row_index)
  series = @data[(row_index / 3.0).to_i]

  series_row = row_index % 3

  min_char       = ['', '' , ''][series_row]
  line_char      = [' ', '' , ' '][series_row]
  left_box_char  = ['', '' , ''][series_row]
  line_box_char  = ['', ' ' , ''][series_row]
  median_char    = ['', '' , ''][series_row]
  right_box_char = ['', '' , ''][series_row]
  max_char       = ['', '' , ''][series_row]

  line = (0 ... @width).map { ' ' }

  # Draw shapes first - this is most important,
  # so they'll always be drawn even if there's not enough space

  transformed = transform(series)
  line[transformed[0] - 1] = min_char
  line[transformed[1] - 1] = left_box_char
  line[transformed[2] - 1] = median_char
  line[transformed[3] - 1] = right_box_char
  line[transformed[4] - 1] = max_char

  (transformed[0] ... (transformed[1] - 1)).each do |i|
    line[i] = line_char
  end
  (transformed[1] ... (transformed[2] - 1)).each do |i|
    line[i] = line_box_char
  end
  (transformed[2] ... (transformed[3] - 1)).each do |i|
    line[i] = line_box_char
  end
  (transformed[3] ... (transformed[4] - 1)).each do |i|
    line[i] = line_char
  end

  print_styled(out, line.join(''), color: @color)
end