Class: Scruffy::Layers::Box

Inherits:
Base
  • Object
show all
Defined in:
lib/scruffy/layers/box.rb

Overview

Scruffy::Layers::Box

Author

Brasten Sager

Date

August 6th, 2006

Standard bar graph.

Instance Attribute Summary

Attributes inherited from Base

#color, #complexity, #height, #max_value, #min_value, #opacity, #options, #outline, #points, #preferred_color, #preferred_outline, #relevant_data, #title, #width

Instance Method Summary collapse

Methods inherited from Base

#bottom_key, #initialize, #legend_data, #relevant_data?, #render, #sum_values, #top_key

Constructor Details

This class inherits a constructor from Scruffy::Layers::Base

Instance Method Details

#bottom_value(padding = nil) ⇒ Object

Returns the lowest value in any of this container’s layers.

If padding is set to :padded, a 15% padding is added below the lowest value. If the lowest value is greater than zero, then the padding will not cross the zero line, preventing negative values from being introduced into the graph purely due to padding.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/scruffy/layers/box.rb', line 66

def bottom_value(padding=nil) # :nodoc:
  botval = points[0].min
  points.each do |point_set|
    botval = ( (botval>point_set.min) ? point_set.min : botval )
  end
  #botval = layers.inject(0) do |min, layer| 
  #  (min = ((min > layer.bottom_value) ? layer.bottom_value : min)) unless layer.bottom_value.nil?
  #  min 
  #end
  above_zero = (botval >= 0)
  botval = (botval - ((top_value(nil) - botval) * 0.15)) if padding == :padded

  # Don't introduce negative values solely due to padding.
  # A user-provided value must be negative before padding will extend into negative values.
  (above_zero && botval < 0) ? 0 : botval
end

#draw(svg, coords, options = {}) ⇒ Object

Draw box plot.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/scruffy/layers/box.rb', line 11

def draw(svg, coords, options = {})
  coords.each_with_index do |coord,idx|
    x, y, bar_height = (coord.first), coord.last, 1#(height - coord.last)
    
      valh = max_value + min_value * -1 #value_height
      maxh = max_value * height / valh #positive area height
      minh = min_value * height / valh #negative area height
      #puts "height = #{height} and max_value = #{max_value} and min_value = #{min_value} and y = #{y} and point = #{points[idx]}"
      
      #if points[idx] > 0
      #  bar_height = points[idx]*maxh/max_value
      #else
      #  bar_height = points[idx]*minh/min_value
      #end
    
    #puts " y = #{y} and point = #{points[idx]}"  
    #svg.g(:transform => "translate(-#{relative(0.5)}, -#{relative(0.5)})") {
    #  svg.rect( :x => x, :y => y, :width => @bar_width + relative(1), :height => bar_height + relative(1), 
    #            :style => "fill: black; fill-opacity: 0.15; stroke: none;" )
    #  svg.rect( :x => x+relative(0.5), :y => y+relative(2), :width => @bar_width + relative(1), :height => bar_height - relative(0.5), 
    #            :style => "fill: black; fill-opacity: 0.15; stroke: none;" )
    #
    #}
    
    svg.line(:x1=>x+@bar_width/2,:x2=>x+@bar_width/2,:y1=>y[0],:y2=>y[4], :style => "stroke:#{(outline.to_s || options[:theme].marker || 'white').to_s}; stroke-width:1")
    svg.line(:x1=>x+@bar_width/4,:x2=>x+@bar_width/4*3,:y1=>y[0],:y2=>y[0], :style => "stroke:#{(outline.to_s || options[:theme].marker || 'white').to_s}; stroke-width:1")
    svg.line(:x1=>x+@bar_width/4,:x2=>x+@bar_width/4*3,:y1=>y[4],:y2=>y[4], :style => "stroke:#{(outline.to_s || options[:theme].marker || 'white').to_s}; stroke-width:1")
    svg.rect( :x => x, :y => y[1], :width => @bar_width, :height => (y[1]-y[3])*-1, 
              :fill => color.to_s, 'style' => "opacity: #{opacity}; stroke:#{(outline.to_s || options[:theme].marker || 'white').to_s}; stroke-width:1;" )
    svg.line(:x1=>x,:x2=>x+@bar_width,:y1=>y[2],:y2=>y[2], :style => "stroke:#{(outline.to_s || options[:theme].marker || 'white').to_s}; stroke-width:1")
    #svg.rect( :x => x, :y => y, :width => @bar_width, :height => bar_height, 
    #          :fill => color.to_s, 'style' => "opacity: #{opacity}; stroke: none;" )
  end
end

#top_value(padding = nil) ⇒ Object

If padding is set to :padded, a 15% padding is added to the highest value.



50
51
52
53
54
55
56
57
58
59
# File 'lib/scruffy/layers/box.rb', line 50

def top_value(padding=nil) # :nodoc:
  topval = points[0].max
  points.each do |point_set|
    topval = ( (topval < point_set.max) ? point_set.max : topval )
  end
  #topval = layers.inject(0) { |max, layer| (max = ((max < layer.top_value) ? layer.top_value : max)) unless layer.top_value.nil?; max }
  below_zero = (topval <= 0)
  topval = padding == :padded ? (topval + ((topval - bottom_value(nil)) * 0.15)) : topval
  (below_zero && topval > 0) ? 0 : topval
end