Setup Constants
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
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
# File lib/scruffy/layers/pie.rb, line 119 def points=(val) throw ArgumentsError, "Pie layers cannot accept points, only pie slices." end
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