Class: Scruffy::Layers::Multi

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

Overview

Scruffy::Layers::Multi

Author

Jeremy Green

Date

July 29th, 2009

Based on

Scruffy::Layers::Stacked by

Author

Brasten Sager

Date

August 12th, 2006

Provides a generic way for displaying multiple bar graphs side by side.

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 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) ⇒ Multi

Returns new Multi graph.

You can provide a block for easily adding layers during (just after) initialization. Example:

Multi.new do |multi|
  multi << Scruffy::Layers::Line.new( ... )
  multi.add(:multi_bar, 'My Bar', [...])
end

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



26
27
28
29
30
# File 'lib/scruffy/layers/multi.rb', line 26

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

  block.call(self)    # Allow for population of data with a block during initialization.
end

Instance Method Details

#legend_dataObject

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



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/scruffy/layers/multi.rb', line 55

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

#points=(val) ⇒ Object



70
71
72
# File 'lib/scruffy/layers/multi.rb', line 70

def points=(val)
  throw ArgumentsError, "Multi layers cannot accept points, only other layers."
end

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

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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/scruffy/layers/multi.rb', line 33

def render(svg, options = {})
  #TODO ensure this works with new points
  #current_points = points
  layers.each_with_index do |layer,i|
    
    #real_points = layer.points
    #layer.points = current_points
    layer_options = options.dup
 
    layer_options[:num_bars] = layers.size
    layer_options[:position] = i
    layer_options[:color] = layer.preferred_color || layer.color || options[:theme].next_color
    layer.render(svg, layer_options)
          
    options.merge(layer_options)
    
    #layer.points = real_points
    #layer.points.each_with_index { |val, idx| current_points[idx] -= val }
  end
end