Class: Scruffy::Layers::Pie
- 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
-
#center_x ⇒ Object
Returns the value of attribute center_x.
-
#center_y ⇒ Object
Returns the value of attribute center_y.
-
#degree_offset ⇒ Object
Returns the value of attribute degree_offset.
-
#diameter ⇒ Object
Returns the value of attribute diameter.
-
#percent_used ⇒ Object
Returns the value of attribute percent_used.
-
#scaler ⇒ Object
Returns the value of attribute scaler.
Attributes inherited from Base
#color, #complexity, #height, #max_value, #min_value, #opacity, #options, #points, #preferred_color, #relevant_data, #title, #width
Instance Method Summary collapse
-
#initialize(options = {}, &block) ⇒ Pie
constructor
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.
-
#legend_data ⇒ Object
A stacked graph has many data sets.
- #points=(val) ⇒ Object
-
#render(svg, options = {}) ⇒ Object
Overrides Base#render to fiddle with layers’ points to achieve a stacked effect.
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( = {}, &block) super() # 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_x ⇒ Object
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_y ⇒ Object
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_offset ⇒ Object
Returns the value of attribute degree_offset.
49 50 51 |
# File 'lib/scruffy/layers/pie.rb', line 49 def degree_offset @degree_offset end |
#diameter ⇒ Object
Returns the value of attribute diameter.
47 48 49 |
# File 'lib/scruffy/layers/pie.rb', line 47 def diameter @diameter end |
#percent_used ⇒ Object
Returns the value of attribute percent_used.
48 49 50 |
# File 'lib/scruffy/layers/pie.rb', line 48 def percent_used @percent_used end |
#scaler ⇒ Object
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_data ⇒ Object
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, = {}) # #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| = .dup = .merge(@options) = .merge(layer.) [:scaler] = @scaler [:percent_used] = @percent_used @percent_used += @scaler * layer.sum_values [:color] = layer.preferred_color || layer.color || [:theme].next_color layer.render(svg, ) end end |