Class: Scruffy::Layers::Pie

Inherits:
Base
  • Object
show all
Includes:
Helpers::LayerContainer
Defined in:
lib/scruffy/layers/pie.rb

Overview

Scruffy::Layers::Pie

Author

A.J. Ostman

Date

August 15, 2006

Provides a container for pie slice.

Constant Summary collapse

RADIANS =

Setup Constants

Math::PI/180

Instance Attribute Summary collapse

Attributes inherited from Base

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

Instance Method Summary collapse

Methods included from Helpers::LayerContainer

#<<, #bottom_key, #bottom_value, #layers, #layers=, #top_key, #top_value

Methods inherited from Base

#bottom_key, #bottom_value, #draw, #relevant_data?, #sum_values, #top_key, #top_value

Constructor Details

#initialize(options = {}, &block) ⇒ Pie

The initialize method passes itself to the block, and since Pie is a LayerContainer, layers (pie slice) can be added just as if they were being added to Graph.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/scruffy/layers/pie.rb', line 57

def initialize(options = {}, &block)
  super(options)

  # Allow for population of data with a block during initialization.
  if block
    block.call(self)
  else
    # Otherwise, just iterate over the points, adding the slices
    if @points.class == Hash
      @points.keys.each {|k|
        self.add :pie_slice, k.to_s, [@points[k]]}
    end
    if @points.class == Array
      @points.each {|v|
        self.add :pie_slice, '', [v]}
    end
  end
end

Instance Attribute Details

#center_xObject

Returns the value of attribute center_x.



51
52
53
# File 'lib/scruffy/layers/pie.rb', line 51

def center_x
  @center_x
end

#center_yObject

Returns the value of attribute center_y.



51
52
53
# File 'lib/scruffy/layers/pie.rb', line 51

def center_y
  @center_y
end

#degree_offsetObject

Returns the value of attribute degree_offset.



49
50
51
# File 'lib/scruffy/layers/pie.rb', line 49

def degree_offset
  @degree_offset
end

#diameterObject

Returns the value of attribute diameter.



47
48
49
# File 'lib/scruffy/layers/pie.rb', line 47

def diameter
  @diameter
end

#percent_usedObject

Returns the value of attribute percent_used.



48
49
50
# File 'lib/scruffy/layers/pie.rb', line 48

def percent_used
  @percent_used
end

#scalerObject

Returns the value of attribute scaler.



50
51
52
# File 'lib/scruffy/layers/pie.rb', line 50

def scaler
  @scaler
end

Instance Method Details

#legend_dataObject

A stacked graph has many data sets. Return legend information for all of them.



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/scruffy/layers/pie.rb', line 107

def legend_data
  if relevant_data?
    retval = []
    layers.each do |layer|
      retval << layer.legend_data
    end
    retval
  else
    nil
  end
end

#points=(val) ⇒ Object



119
120
121
# File 'lib/scruffy/layers/pie.rb', line 119

def points=(val)
  throw ArgumentsError, "Pie layers cannot accept points, only pie slices."
end

#render(svg, options = {}) ⇒ Object

Overrides Base#render to fiddle with layers’ points to achieve a stacked effect.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/scruffy/layers/pie.rb', line 79

def render(svg, options = {})
  # #current_points = points.dup

  @scaler = 1
  total = 0
  
  layers.each do |layer|
    total += layer.sum_values
  end 
  
  @scaler = 100.0 / total
  
  @percent_used = 30
  
  layers.each do |layer|
    layer_options = options.dup
    layer_options = layer_options.merge(@options)
    layer_options = layer_options.merge(layer.options)
    layer_options[:scaler] = @scaler
    layer_options[:percent_used] = @percent_used
    @percent_used += @scaler * layer.sum_values
    layer_options[:color] = layer.preferred_color || layer.color || options[:theme].next_color          
    
    layer.render(svg, layer_options)
  end
end