0
@@ -75,7 +75,7 @@ Licensed under the MIT license.
0
@@ -87,29 +87,29 @@ class Sparklines
0
def plot_to_image(data=[], options={})
0
- :line_color => 'lightgrey',
0
- :above_color => 'red',
0
- :below_color => 'grey',
0
+ :line_color => 'lightgrey',
0
+ :above_color => 'red',
0
+ :below_color => 'grey',
0
:background_color => 'white',
0
- :share_color => 'red',
0
- :remain_color => 'lightgrey',
0
- :max_color => 'green',
0
- :std_dev_color => '#efefef',
0
- :has_std_dev => false,
0
+ :share_color => 'red',
0
+ :remain_color => 'lightgrey',
0
+ :max_color => 'green',
0
+ :std_dev_color => '#efefef',
0
+ :has_std_dev => false,
0
# HACK for HashWithIndifferentAccess
0
@@ -139,10 +139,10 @@ class Sparklines
0
def plot(data=[], options={})
0
plot_to_image(data, options).to_blob
0
# Writes a graph to disk with the specified filename, or "sparklines.png".
0
def plot_to_file(filename="sparklines.png", data=[], options={})
0
File.open( filename, 'wb' ) do |png|
0
png << self.plot( data, options)
0
@@ -251,28 +251,44 @@ class Sparklines
0
+ # Also takes :target option (a line will be drawn) and :upper (values
0
+ # under will be drawn in :below_color, above in :above_color).
0
- step = @options[:step].to_f
0
- height = @options[:height].to_f
0
+ step = @options[:step].to_f
0
+ height = @options[:height].to_f
0
+ width = ((@norm_data.size - 1) * step).to_f
0
background_color = @options[:background_color]
0
create_canvas(@norm_data.length * step + 2, height, background_color)
0
- upper = @options[:upper].to_f
0
- below_color = @options[:below_color]
0
- above_color = @options[:above_color]
0
+ upper = @options[:upper].to_f
0
+ below_color = @options[:below_color]
0
+ above_color = @options[:above_color]
0
+ target = @options.has_key?(:target) ? @options[:target].to_f : nil
0
+ target_color = @options[:target_color] || 'white'
0
+ # raise @norm_data.to_yaml
0
+ max_normalized = @norm_data.max
0
@norm_data.each_with_index do |r, index|
0
- color = (
r >= upper) ? above_color : below_color
0
+ color = (
@data[index] >= upper) ? above_color : below_color
0
@draw.stroke('transparent')
0
- @draw.rectangle( i, @canvas.rows,
0
- i + step - 2, @canvas.rows - ( (r / @maximum_value) * @canvas.rows) )
0
+ bar_height_from_top = @canvas.rows - ( (r.to_f / max_normalized.to_f) * @canvas.rows)
0
+ @draw.rectangle( i, @canvas.rows, i + step - 2, bar_height_from_top )
0
+ normalized_target_value = ((target.to_f - @minimum_value)/(@maximum_value - @minimum_value)) * 100.0
0
+ adjusted_target_value = (height - 3 - normalized_target_value/(101.0/(height-4))).to_i
0
+ @draw.stroke(target_color)
0
+ open_ended_polyline([[-5, adjusted_target_value], [width + 5, adjusted_target_value]])
0
@@ -415,28 +431,27 @@ class Sparklines
0
# :target - A 1px horizontal line will be drawn at this value. Useful for showing an average.
0
# :target_color - Color of the target line. Defaults to white.
0
- step = @options[:step].to_f
0
- height = @options[:height].to_f
0
- width = ((@norm_data.size - 1) * step).to_f
0
+ step = @options[:step].to_f
0
+ height = @options[:height].to_f
0
+ width = ((@norm_data.size - 1) * step).to_f
0
background_color = @options[:background_color]
0
create_canvas(width, height, background_color)
0
- min_color = @options[:min_color]
0
- max_color = @options[:max_color]
0
- last_color = @options[:last_color]
0
- has_min = @options[:has_min]
0
- has_max = @options[:has_max]
0
- has_last = @options[:has_last]
0
- line_color = @options[:line_color]
0
- has_std_dev = @options[:has_std_dev]
0
- std_dev_color = @options[:std_dev_color]
0
+ min_color = @options[:min_color]
0
+ max_color = @options[:max_color]
0
+ last_color = @options[:last_color]
0
+ has_min = @options[:has_min]
0
+ has_max = @options[:has_max]
0
+ has_last = @options[:has_last]
0
+ line_color = @options[:line_color]
0
+ has_std_dev = @options[:has_std_dev]
0
+ std_dev_color = @options[:std_dev_color]
0
- target = @options.has_key?(:target) ? @options[:target].to_f : nil
0
- target_color = @options[:target_color] || 'white'
0
+ target = @options.has_key?(:target) ? @options[:target].to_f : nil
0
+ target_color = @options[:target_color] || 'white'
0
drawstddevbox(width,height,std_dev_color) if has_std_dev == true
0
@@ -541,7 +556,7 @@ class Sparklines
0
# :height - Numeric. Defaults to 15. Should be a multiple of three.
0
# :width - This graph expands to any specified width. Defaults to 100.
0
# :bad - Numeric. A darker shade background will be drawn up to this point.
0
# :satisfactory - Numeric. A medium background will be drawn up to this point.
0
@@ -611,8 +626,15 @@ class Sparklines
0
- @minimum_value = @data.min
0
+ case @options[:type].to_s
0
+ @minimum_value = @data.min
0
@maximum_value = @data.max
0
case @options[:type].to_s
Comments
No one has commented yet.