Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactor Sparkr::Sparkline

  • Loading branch information...
commit 76974edd7ae7b3c06d747a4ffe40a18acac9e50a 1 parent f11999b
@rrrene authored
Showing with 36 additions and 5 deletions.
  1. +36 −5 lib/sparkr/sparkline.rb
View
41 lib/sparkr/sparkline.rb
@@ -6,17 +6,48 @@ class Sparkline
DEFAULT_SEPARATOR = ''
def initialize(_numbers)
- numbers = _numbers.map(&:to_i)
- min, max = numbers.minmax
- step_height = (max - min) / (TICKS.size - 1).to_f
- step_height = 1 if step_height == 0
+ numbers = normalize_numbers(_numbers)
+ one_step = step_height(numbers)
@ticks = numbers.map do |n|
- index = ((n - min) / step_height).to_i
+ index = (n / one_step).to_i
TICKS[index]
end
end
+ # Returns the normalized equivalent of a given list
+ #
+ # normalize_numbers([3,4,7])
+ # # => [0,1,4]
+ #
+ # @return [Fixnum] the normalized equivalent of the given +_numbers+
+ def normalize_numbers(_numbers)
+ numbers = _numbers.map(&:to_i)
+ min = numbers.min
+ numbers.map do |n|
+ n - min
+ end
+ end
+
+ # @return [Float] the numerical "height" of a single bar "step"
+ def step_height(_numbers)
+ min, max = _numbers.minmax
+ actual_height = max - min
+ step = actual_height / steps.to_f
+ if step == 0
+ 1
+ else
+ step
+ end
+ end
+
+ # @return [Fixnum] the count of steps between the smallest and highest bar
+ def steps
+ TICKS.size - 1
+ end
+
+ # @param sep [String,nil] separator used to join the bars of the sparkline
+ # @return [String] the sparkline, seperated by +sep+ (defaults to '')
def to_s(sep = nil)
@ticks.join(sep || DEFAULT_SEPARATOR)
end
Please sign in to comment.
Something went wrong with that request. Please try again.