Class: UnicodePlot::NumericStemplot

Inherits:
Stemplot
  • Object
show all
Defined in:
src/lib/unicode_plot/stemplot.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Stemplot

factory, #insert, #leaves, #max_stem_length, #raw_stems, #stems

Constructor Details

#initialize(vector, scale: 10, **kw) ⇒ NumericStemplot

Returns a new instance of NumericStemplot.



138
139
140
141
142
143
144
145
146
147
148
149
# File 'src/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

Class Method Details

.sorted_stem_list(stems, all: true) ⇒ Array

Used when we have stems from a back-to-back stemplot and a combined list of stems is given

Parameters:

  • stems (Array)

    Concatenated list of stems from two plots

  • all (Boolean) (defaults to: true)

    Return all stems if true, otherwise only return stems if a leaf exists for a stem

Returns:

  • (Array)

    Sorted list of stems



168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'src/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

Instance Method Details

Print key to STDOUT

Parameters:

  • scale (Integer)

    Scale, should be a power of 10

  • divider (String)

    Divider character between stem and leaf



154
155
156
157
158
159
160
161
162
# File 'src/lib/unicode_plot/stemplot.rb', line 154

def print_key(scale, divider)
  # First print the key
  puts "Key: 1#{divider}0 = #{scale}"
  # Description of where the decimal is
  trunclog = Math.log10(scale).truncate
  ndigits = trunclog.abs
  right_or_left = (trunclog < 0) ? "left" : "right"
  puts "The decimal is #{ndigits} digit(s) to the #{right_or_left} of #{divider}"
end