Skip to content

Commit

Permalink
Handle missing data (nil) values in area, bar, discrete, and smooth.
Browse files Browse the repository at this point in the history
  • Loading branch information
jloveces committed Feb 16, 2009
1 parent 99dd177 commit ed5b585
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
1 change: 1 addition & 0 deletions History.txt
@@ -1,6 +1,7 @@
== 0.5.4 == 0.5.4


* Support disabling last point when label is shown [Justin Love] * Support disabling last point when label is shown [Justin Love]
* Handle missing data (nil) values in area, bar, discrete, smooth


== 0.5.2 == 0.5.2


Expand Down
30 changes: 17 additions & 13 deletions lib/sparklines.rb
Expand Up @@ -206,7 +206,7 @@ def area
coords = [[0,(height - 3 - upper/(101.0/(height-4)))]] coords = [[0,(height - 3 - upper/(101.0/(height-4)))]]
i=0 i=0
@norm_data.each do |r| @norm_data.each do |r|
coords.push [(2 + i), (height - 3 - r/(101.0/(height-4)))] coords.push [(2 + i), (height - 3 - r/(101.0/(height-4)))] if r
i += step i += step
end end
coords.push [(@norm_data.size - 1) * step + 4, (height - 3 - upper/(101.0/(height-4)))] coords.push [(@norm_data.size - 1) * step + 4, (height - 3 - upper/(101.0/(height-4)))]
Expand Down Expand Up @@ -240,8 +240,8 @@ def area
end end
@draw.clip_path('all') @draw.clip_path('all')


drawbox(coords[@norm_data.index(@norm_data.min)+1], 1, min_color) if has_min == true drawbox(coords[@norm_data.index(@norm_data.compact.min)+1], 1, min_color) if has_min == true
drawbox(coords[@norm_data.index(@norm_data.max)+1], 1, max_color) if has_max == true drawbox(coords[@norm_data.index(@norm_data.compact.max)+1], 1, max_color) if has_max == true


drawbox(coords[-2], 1, last_color) if has_last == true drawbox(coords[-2], 1, last_color) if has_last == true


Expand Down Expand Up @@ -274,7 +274,7 @@ def bar
# raise @norm_data.to_yaml # raise @norm_data.to_yaml
max_normalized = @norm_data.max max_normalized = @norm_data.max
@norm_data.each_with_index do |r, index| @norm_data.each_with_index do |r, index|
color = (@data[index] >= upper) ? above_color : below_color color = ((@data[index] || @minimum_value) >= upper) ? above_color : below_color
@draw.stroke('transparent') @draw.stroke('transparent')
@draw.fill(color) @draw.fill(color)
bar_height_from_top = @canvas.rows - ( (r.to_f / max_normalized.to_f) * @canvas.rows) bar_height_from_top = @canvas.rows - ( (r.to_f / max_normalized.to_f) * @canvas.rows)
Expand Down Expand Up @@ -324,10 +324,12 @@ def discrete


i = 0 i = 0
@norm_data.each do |r| @norm_data.each do |r|
color = (r >= upper) ? above_color : below_color if !r.nil?
@draw.stroke(color) color = (r >= upper) ? above_color : below_color
@draw.line(i, (@canvas.rows - r/(101.0/(height-4))-4).to_f, @draw.stroke(color)
i, (@canvas.rows - r/(101.0/(height-4))).to_f) @draw.line(i, (@canvas.rows - r/(101.0/(height-4))-4).to_f,
i, (@canvas.rows - r/(101.0/(height-4))).to_f)
end
i += step i += step
end end


Expand Down Expand Up @@ -459,7 +461,9 @@ def smooth
coords = [] coords = []
i=0 i=0
@norm_data.each do |r| @norm_data.each do |r|
coords.push [ i, (height - 3 - r/(101.0/(height-4))) ] if !r.nil?
coords.push [ i, (height - 3 - r/(101.0/(height-4))) ]
end
i += step i += step
end end


Expand All @@ -476,8 +480,8 @@ def smooth
open_ended_polyline([[-5, adjusted_target_value], [width + 5, adjusted_target_value]]) open_ended_polyline([[-5, adjusted_target_value], [width + 5, adjusted_target_value]])
end end


drawbox(coords[@norm_data.index(@norm_data.min)], 2, min_color) if has_min == true drawbox(coords[@norm_data.index(@norm_data.compact.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[@norm_data.index(@norm_data.compact.max)], 2, max_color) if has_max == true
drawbox(coords[-1], 2, last_color) if has_last == true drawbox(coords[-1], 2, last_color) if has_last == true


@draw.draw(@canvas) @draw.draw(@canvas)
Expand Down Expand Up @@ -630,10 +634,10 @@ def normalize_data
when 'bar' when 'bar'
@minimum_value = 0.0 @minimum_value = 0.0
else else
@minimum_value = @data.min @minimum_value = @data.compact.min
end end


@maximum_value = @data.max @maximum_value = @data.compact.max


case @options[:type].to_s case @options[:type].to_s
when 'pie' when 'pie'
Expand Down

0 comments on commit ed5b585

Please sign in to comment.