lib/sparklines.rb in sparklines-0.4.5 vs lib/sparklines.rb in sparklines-0.4.6
- old
+ new
@@ -72,11 +72,11 @@
Licensed under the MIT license.
=end
class Sparklines
- VERSION = '0.4.5'
+ VERSION = '0.4.6'
@@label_margin = 5.0
@@pointsize = 10.0
class << self
@@ -100,16 +100,16 @@
:remain_color => 'lightgrey',
:min_color => 'blue',
:max_color => 'green',
:last_color => 'red',
:std_dev_color => '#efefef',
-
+
:has_min => false,
:has_max => false,
:has_last => false,
:has_std_dev => false,
-
+
:label => nil
}
# HACK for HashWithIndifferentAccess
options_sym = Hash.new
@@ -282,11 +282,11 @@
background_color = @options[:background_color]
step = @options[:step].to_f
width = @norm_data.size * step - 1
- create_canvas(width, height, background_color)
+ create_canvas(@norm_data.size * step - 1, height, background_color)
below_color = @options[:below_color]
above_color = @options[:above_color]
std_dev_color = @options[:std_dev_color]
@@ -371,11 +371,10 @@
@draw.draw(@canvas)
@canvas.to_blob
end
-
##
# Creates a smooth sparkline.
#
# :step - An integer that determines the distance between each point on the sparkline. Defaults to 2.
#
@@ -394,19 +393,19 @@
# :max_color - A string or color code representing the color that the dot drawn at the largest value will be displayed as. Defaults to green.
#
# :last_color - A string or color code representing the color that the dot drawn at the last value will be displayed as. Defaults to red.
#
# :std_dev_color - A string or color code representing the color that the standard deviation bar behind the smooth graph will be displayed as. Defaults to #efefef
-
+ #
+ # :underneath_color - A string or color code representing the color that will be used to fill in the area underneath the line. Optional.
def smooth
step = @options[:step].to_f
height = @options[:height].to_f
+ width = ((@norm_data.size - 1) * step).to_f
+
background_color = @options[:background_color]
-
- width = (@norm_data.size - 1) * step + 4
-
create_canvas(width, height, background_color)
min_color = @options[:min_color]
max_color = @options[:max_color]
last_color = @options[:last_color]
@@ -421,27 +420,28 @@
@draw.stroke(line_color)
coords = []
i=0
@norm_data.each do |r|
- coords.push [ 2 + i, (height - 3 - r/(101.0/(height-4))) ]
+ coords.push [ i, (height - 3 - r/(101.0/(height-4))) ]
i += step
end
- open_ended_polyline(coords)
+ if @options[:underneath_color]
+ closed_polygon(height, width, coords)
+ else
+ open_ended_polyline(coords)
+ end
drawbox(coords[@norm_data.index(@norm_data.min)], 2, min_color) if has_min == true
-
drawbox(coords[@norm_data.index(@norm_data.max)], 2, max_color) if has_max == true
-
drawbox(coords[-1], 2, last_color) if has_last == true
@draw.draw(@canvas)
@canvas.to_blob
end
-
-
+
##
# Creates a whisker sparkline to track on/off type data. There are five states:
# on, off, no value, exceptional on, exceptional off. On values create an up
# whisker and off values create a down whisker. Exceptional values may be
# colored differently than regular values to indicate, for example, a shut out.
@@ -531,11 +531,22 @@
def open_ended_polyline(arr)
0.upto(arr.length - 2) { |i|
@draw.line(arr[i][0], arr[i][1], arr[i+1][0], arr[i+1][1])
}
end
-
+
+ # Fills in the area under the line (used for a smooth graph)
+ def closed_polygon(height, width, coords)
+ return if @options[:underneath_color].nil?
+ list = []
+ list << [0, height]
+ list << coords
+ list << [width, height]
+ @draw.fill( @options[:underneath_color] )
+ @draw.polygon( *list.flatten )
+ end
+
##
# Create an image to draw on and a drawable to do the drawing with.
#
# TODO Refactor into smaller methods
@@ -644,6 +655,5 @@
Math.sqrt(variance(population))
end
end
-