Class: UnicodePlot::NumericStemplot
- Defined in:
- src/lib/unicode_plot/stemplot.rb
Class Method Summary collapse
-
.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.
Instance Method Summary collapse
-
#initialize(vector, scale: 10, **kw) ⇒ NumericStemplot
constructor
A new instance of NumericStemplot.
-
#print_key(scale, divider) ⇒ Object
Print key to STDOUT.
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
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(scale, divider) ⇒ Object
Print key to STDOUT
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 |