class UnicodePlot::NumericStemplot

Public Class Methods

new(vector, scale: 10, **kw) click to toggle source
Calls superclass method UnicodePlot::Stemplot::new
# File lib/unicode_plot/stemplot.rb, line 138
def initialize(vector, scale: 10, **kw)
  super
  Array(vector).each do |value|
    fvalue = value.to_f.fdiv(scale/10.0)
    stemnum = (fvalue/10).to_i
    leafnum = (fvalue - (stemnum*10)).to_i
    stemsign = value.negative? ? "-" : ''
    stem = stemsign + stemnum.abs.to_s
    leaf = leafnum.abs.to_s
    self.insert(stem, leaf)
  end
end
sorted_stem_list(stems, all: true) click to toggle source

Used when we have stems from a back-to-back stemplot and a combined list of stems is given @param stems [Array] Concatenated list of stems from two plots @param all [Boolean] Return all stems if true, otherwise only return stems if a leaf exists for a stem @return [Array] Sorted list of stems

# File lib/unicode_plot/stemplot.rb, line 168
def self.sorted_stem_list(stems, all: true)
  negkeys, poskeys = stems.partition { |str| str[0] == '-'}
  if all
    negmin, negmax = negkeys.map(&:to_i).map(&:abs).minmax
    posmin, posmax = poskeys.map(&:to_i).minmax
    negrange = negmin ? (negmin..negmax).to_a.reverse.map { |s| "-"+s.to_s } : []
    posrange = posmin ? (posmin..posmax).to_a.map(&:to_s) : []
    return negrange + posrange
  else
    negkeys.sort! { |a,b| a.to_i <=> b.to_i }
    poskeys.sort! { |a,b| a.to_i <=> b.to_i }
    return negkeys + poskeys
  end
end

Public Instance Methods

print_key(scale, divider) click to toggle source

Print key to STDOUT @param scale [Integer] Scale, should be a power of 10 @param divider [String] Divider character between stem and leaf