class Scruffy::Layers::Pie

Scruffy::Layers::Pie

Author

A.J. Ostman

Date

August 15, 2006

Provides a container for pie slice.

Constants

RADIANS

Setup Constants

Attributes

center_x[RW]
center_y[RW]
degree_offset[RW]
diameter[RW]
percent_used[RW]
scaler[RW]

Public Class Methods

new(options = {}, &block) click to toggle source

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.

# 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

Public Instance Methods

legend_data() click to toggle source

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

# 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) click to toggle source
# 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 = {}) click to toggle source

Overrides Scruffy::Layers::Base#render to fiddle with layers' points to achieve a stacked effect.

# 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