Class: Scruffy::Renderers::Base

Inherits:
Object
  • Object
show all
Includes:
Helpers::Canvas
Defined in:
lib/scruffy/renderers/base.rb

Overview

Scruffy::Renderers::Base

Author

Brasten Sager

Date

August 14th, 2006

Provides all the base functionality needed to render a graph, but does not provide a default layout.

For a basic layout, see Scruffy::Renderers::Standard.

Direct Known Subclasses

Empty, Pie, Reversed, Sparkline

Instance Attribute Summary collapse

Attributes included from Helpers::Canvas

#components

Instance Method Summary collapse

Methods included from Helpers::Canvas

#component, #remove, #reset_settings!

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



17
18
19
20
21
# File 'lib/scruffy/renderers/base.rb', line 17

def initialize(options = {})
  self.components = []
  self.options = options
  define_layout
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/scruffy/renderers/base.rb', line 56

def method_missing(sym, *args)
  self.options = {} if self.options.nil?
  
  if args.size > 0
    self.options[sym] = args[0]
  else
    return self.options[sym]
  end
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



15
16
17
# File 'lib/scruffy/renderers/base.rb', line 15

def options
  @options
end

Instance Method Details

#before_renderObject



47
48
49
50
51
52
53
54
# File 'lib/scruffy/renderers/base.rb', line 47

def before_render
  if self.options
    set_values(self.options[:values])    if (self.options[:values] && self.options[:values] != :hide)
    hide_grid     if (self.options[:grid] == :hide)
    hide_values   if (self.options[:values] == :hide)
    hide_labels   if (self.options[:labels] == :hide)
  end
end

#render(options = {}) ⇒ Object

Renders the graph and all components.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/scruffy/renderers/base.rb', line 24

def render(options = {})
  options[:graph_id]    ||= 'scruffy_graph'
  options[:complexity]  ||= (global_complexity || :normal)

  # Allow subclasses to muck with components prior to renders.
  rendertime_renderer = self.clone
  rendertime_renderer.instance_eval { before_render if respond_to?(:before_render) }

  svg = Builder::XmlMarkup.new(:indent => 2)
  svg.instruct!
  svg.instruct! 'DOCTYPE', 'svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"  type'
  svg.svg(:xmlns => "http://www.w3.org/2000/svg", 'xmlns:xlink' => "http://www.w3.org/1999/xlink", :width => options[:size].first, :height => options[:size].last) {
    svg.g(:id => options[:graph_id]) {
      rendertime_renderer.components.each do |component|
        component.render(svg, 
                         bounds_for( options[:size], component.position, component.size ), 
                         options)
      end
    }
  }
  svg.target!
end