Class: Kovid::AsciiCharts::Chart
- Inherits:
-
Object
- Object
- Kovid::AsciiCharts::Chart
- Defined in:
- lib/kovid/ascii_charts.rb
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_MAX_Y_VALS =
20
- DEFAULT_MIN_Y_VALS =
10
- STEPS =
[1, 2, 5].freeze
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #all_ints ⇒ Object
- #draw ⇒ Object
- #from_step(val) ⇒ Object
-
#initialize(data, options = {}) ⇒ Chart
constructor
data is a sorted array of [x, y] pairs.
- #lines ⇒ Object
- #max_xval_width ⇒ Object
- #max_yval ⇒ Object
- #max_yval_width ⇒ Object
- #min_yval ⇒ Object
- #nearest_step(val) ⇒ Object
- #next_step_down(val) ⇒ Object
- #next_step_up(val) ⇒ Object
-
#round_value(val) ⇒ Object
round to nearest step size, making sure we curtail precision to same order of magnitude as the step size to avoid 0.4 + 0.2 = 0.6000000000000001.
- #rounded_data ⇒ Object
- #scan_data ⇒ Object
- #scan_y_range ⇒ Object
- #step_size ⇒ Object
- #to_step(num, order) ⇒ Object
- #to_string ⇒ Object
- #y_range ⇒ Object
Constructor Details
#initialize(data, options = {}) ⇒ Chart
data is a sorted array of [x, y] pairs
35 36 37 38 |
# File 'lib/kovid/ascii_charts.rb', line 35 def initialize(data, = {}) @data = data @options = end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
28 29 30 |
# File 'lib/kovid/ascii_charts.rb', line 28 def data @data end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
28 29 30 |
# File 'lib/kovid/ascii_charts.rb', line 28 def @options end |
Instance Method Details
#all_ints ⇒ Object
153 154 155 156 |
# File 'lib/kovid/ascii_charts.rb', line 153 def all_ints scan_data unless defined? @all_ints @all_ints end |
#draw ⇒ Object
216 217 218 |
# File 'lib/kovid/ascii_charts.rb', line 216 def draw lines.join("\n") end |
#from_step(val) ⇒ Object
83 84 85 86 87 88 89 90 91 |
# File 'lib/kovid/ascii_charts.rb', line 83 def from_step(val) if val == 0 [0, 0] else order = Math.log10(val).floor.to_i num = (val / (10**order)) [num, order] end end |
#lines ⇒ Object
212 213 214 |
# File 'lib/kovid/ascii_charts.rb', line 212 def lines raise 'lines must be overridden' end |
#max_xval_width ⇒ Object
176 177 178 179 |
# File 'lib/kovid/ascii_charts.rb', line 176 def max_xval_width scan_data unless defined? @max_xval_width @max_xval_width end |
#max_yval ⇒ Object
143 144 145 146 |
# File 'lib/kovid/ascii_charts.rb', line 143 def max_yval scan_data unless defined? @max_yval @max_yval end |
#max_yval_width ⇒ Object
181 182 183 184 |
# File 'lib/kovid/ascii_charts.rb', line 181 def max_yval_width scan_y_range unless defined? @max_yval_width @max_yval_width end |
#min_yval ⇒ Object
148 149 150 151 |
# File 'lib/kovid/ascii_charts.rb', line 148 def min_yval scan_data unless defined? @min_yval @min_yval end |
#nearest_step(val) ⇒ Object
102 103 104 105 |
# File 'lib/kovid/ascii_charts.rb', line 102 def nearest_step(val) num, order = from_step(val) to_step(2, order) # #@@ end |
#next_step_down(val) ⇒ Object
117 118 119 120 121 122 123 124 125 |
# File 'lib/kovid/ascii_charts.rb', line 117 def next_step_down(val) num, order = from_step(val) next_index = STEPS.index(num.to_i) - 1 if next_index == -1 STEPS.size - 1 order -= 1 end to_step(STEPS[next_index], order) end |
#next_step_up(val) ⇒ Object
107 108 109 110 111 112 113 114 115 |
# File 'lib/kovid/ascii_charts.rb', line 107 def next_step_up(val) num, order = from_step(val) next_index = STEPS.index(num.to_i) + 1 if STEPS.size == next_index next_index = 0 order += 1 end to_step(STEPS[next_index], order) end |
#round_value(val) ⇒ Object
round to nearest step size, making sure we curtail precision to same order of magnitude as the step size to avoid 0.4 + 0.2 = 0.6000000000000001
128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/kovid/ascii_charts.rb', line 128 def round_value(val) remainder = val % step_size unprecised = if (remainder * 2) >= step_size (val - remainder) + step_size else val - remainder end if step_size < 1 precision = -Math.log10(step_size).floor (unprecised * (10**precision)).to_i.to_f / (10**precision) else unprecised end end |
#rounded_data ⇒ Object
40 41 42 |
# File 'lib/kovid/ascii_charts.rb', line 40 def rounded_data @rounded_data ||= data.map { |pair| [pair[0], round_value(pair[1])] } end |
#scan_data ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/kovid/ascii_charts.rb', line 158 def scan_data @max_yval = 0 @min_yval = 0 @all_ints = true @max_xval_width = 1 data.each do |pair| @max_yval = pair[1] if pair[1] > @max_yval @min_yval = pair[1] if pair[1] < @min_yval @all_ints = false if @all_ints && !pair[1].is_a?(Integer) if (xw = pair[0].to_s.length) > @max_xval_width @max_xval_width = xw end end end |
#scan_y_range ⇒ Object
186 187 188 189 190 191 192 193 194 |
# File 'lib/kovid/ascii_charts.rb', line 186 def scan_y_range @max_yval_width = 1 y_range.each do |yval| if (yw = yval.to_s.length) > @max_yval_width @max_yval_width = yw end end end |
#step_size ⇒ Object
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 |
# File 'lib/kovid/ascii_charts.rb', line 44 def step_size unless defined? @step_size if [:y_step_size] @step_size = [:y_step_size] else max_y_vals = [:max_y_vals] || DEFAULT_MAX_Y_VALS min_y_vals = [:max_y_vals] || DEFAULT_MIN_Y_VALS y_span = (max_yval - min_yval).to_f step_size = nearest_step(y_span.to_f / (data.size + 1)) if @all_ints && (step_size < 1) step_size = 1 else while (y_span / step_size) < min_y_vals candidate_step_size = next_step_down(step_size) if @all_ints && (candidate_step_size < 1) break end ## don't go below one step_size = candidate_step_size end end # go up if we undershot, or were never over while (y_span / step_size) > max_y_vals step_size = next_step_up(step_size) end @step_size = step_size end if !@all_ints && @step_size.is_a?(Integer) @step_size = @step_size.to_f end end @step_size end |
#to_step(num, order) ⇒ Object
93 94 95 96 97 98 99 100 |
# File 'lib/kovid/ascii_charts.rb', line 93 def to_step(num, order) s = num * (10**order) if order < 0 s.to_f else s end end |
#to_string ⇒ Object
220 221 222 |
# File 'lib/kovid/ascii_charts.rb', line 220 def to_string draw end |
#y_range ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/kovid/ascii_charts.rb', line 196 def y_range unless defined? @y_range @y_range = [] first_y = round_value(min_yval) first_y -= step_size if first_y > min_yval last_y = round_value(max_yval) last_y += step_size if last_y < max_yval current_y = first_y while current_y <= last_y @y_range << current_y current_y = round_value(current_y + step_size) ## to avoid fp arithmetic oddness end end @y_range end |