class UnicodePlot::Boxplot
Constants
- DEFAULT_COLOR
- MIN_WIDTH
Attributes
max_x[R]
min_x[R]
Public Class Methods
new(data, width, color, min_x, max_x, **kw)
click to toggle source
Calls superclass method
# File 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
Public Instance Methods
add_series!(data)
click to toggle source
# File 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_columns()
click to toggle source
# File lib/unicode_plot/boxplot.rb, line 33 def n_columns @width end
n_data()
click to toggle source
# File lib/unicode_plot/boxplot.rb, line 25 def n_data @data.length end
n_rows()
click to toggle source
# File lib/unicode_plot/boxplot.rb, line 29 def n_rows 3 * @data.length end
print_row(out, row_index)
click to toggle source
# File 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
Private Instance Methods
transform(values)
click to toggle source
# File lib/unicode_plot/boxplot.rb, line 86 def transform(values) values.map do |val| val = (val - @min_x).fdiv(@max_x - @min_x) * @width val.round(half: :even).clamp(1, @width).to_i end end