Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Extract Method: normalize.

  • Loading branch information...
commit fd94ed0e8a915d455c0e3da885b37e970edb3dc1 1 parent ed5b585
@jloveces jloveces authored
Showing with 15 additions and 8 deletions.
  1. +2 −1  History.txt
  2. +13 −7 lib/sparklines.rb
View
3  History.txt
@@ -1,7 +1,8 @@
== 0.5.4
* Support disabling last point when label is shown [Justin Love]
-* Handle missing data (nil) values in area, bar, discrete, smooth
+* Handle missing data (nil) values in area, bar, discrete, smooth [Justin Love]
+* Extract method: normalize [Justin Love]
== 0.5.2
View
20 lib/sparklines.rb
@@ -272,7 +272,7 @@ def bar
i = 1
# raise @norm_data.to_yaml
- max_normalized = @norm_data.max
+ max_normalized = normalize(@maximum_value)
@norm_data.each_with_index do |r, index|
color = ((@data[index] || @minimum_value) >= upper) ? above_color : below_color
@draw.stroke('transparent')
@@ -283,8 +283,7 @@ def bar
end
unless target.nil?
- normalized_target_value = ((target.to_f - @minimum_value)/(@maximum_value - @minimum_value)) * 100.0
- adjusted_target_value = (height - 3 - normalized_target_value/(101.0/(height-4))).to_i
+ adjusted_target_value = (height - 3 - normalize(target)/(101.0/(height-4))).to_i
@draw.stroke(target_color)
open_ended_polyline([[-5, adjusted_target_value], [width + 5, adjusted_target_value]])
end
@@ -308,7 +307,7 @@ def bar
def discrete
height = @options[:height].to_f
- upper = @options[:upper].to_f
+ upper = normalize(@options[:upper].to_f)
background_color = @options[:background_color]
step = @options[:step].to_f
@@ -474,8 +473,7 @@ def smooth
end
unless target.nil?
- normalized_target_value = ((target.to_f - @minimum_value)/(@maximum_value - @minimum_value)) * 100.0
- adjusted_target_value = (height - 3 - normalized_target_value/(101.0/(height-4))).to_i
+ adjusted_target_value = (height - 3 - normalize(target)/(101.0/(height-4))).to_i
@draw.stroke(target_color)
open_ended_polyline([[-5, adjusted_target_value], [width + 5, adjusted_target_value]])
end
@@ -649,7 +647,7 @@ def normalize_data
if @maximum_value == @minimum_value
value = @maximum_value
else
- value = ((value.to_f - @minimum_value)/(@maximum_value - @minimum_value)) * 100.0
+ value = normalize(value)
end
end
end
@@ -792,4 +790,12 @@ def standard_deviation(population)
Math.sqrt(variance(population))
end
+ def normalize(x)
+ if x.nil?
+ nil
+ else
+ (x.to_f - @minimum_value) * 100 / (@maximum_value - @minimum_value)
+ end
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.