Class: Bio::Graphics::SVGEE

Inherits:
Object
  • Object
show all
Defined in:
lib/bio/graphics/svgee.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ SVGEE

Returns a new instance of SVGEE.



6
7
8
9
10
11
12
13
14
# File 'lib/bio/graphics/svgee.rb', line 6

def initialize(args)
  opts = {:width => '100%', :height => '100%'}.merge!(args)
  @width = opts[:width]
  @height = opts[:height]
  @style= opts[:style]
  @primitives = []
  @defs = []
  @supported_primitives = [:circle, :rectangle, :ellipse, :line, :polyline, :text]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(primitive, args = {}) ⇒ Object (private)

only used to dynamically select the primitive type..

Raises:

  • (NoMethodError)


78
79
80
81
# File 'lib/bio/graphics/svgee.rb', line 78

def method_missing(primitive, args={}) #only used to dynamically select the primitive type.. 
  raise NoMethodError if not self.supported_primitives.include?(primitive) #we're only doing the listed primitive types...
  self.send "make_tag", primitive, args 
end

Instance Attribute Details

#defsObject (readonly)

Returns the value of attribute defs.



5
6
7
# File 'lib/bio/graphics/svgee.rb', line 5

def defs
  @defs
end

#primitivesObject (readonly)

Returns the value of attribute primitives.



5
6
7
# File 'lib/bio/graphics/svgee.rb', line 5

def primitives
  @primitives
end

#supported_primitivesObject (readonly)

Returns the value of attribute supported_primitives.



5
6
7
# File 'lib/bio/graphics/svgee.rb', line 5

def supported_primitives
  @supported_primitives
end

Instance Method Details

#add_primitive(primitive_object) ⇒ Object

adds a primitive object to the SVG



84
85
86
87
88
89
# File 'lib/bio/graphics/svgee.rb', line 84

def add_primitive(primitive_object) #adds a primitive object to the SVG 
  args = {}
  primitive_object.instance_variables.each{|v| args[v.gsub(/@/,"").to_sym] = primitive_object.instance_variable_get(v)  }
  primitive_string = args.delete(:primitive)
  make_tag(primitive_string, args)
end

#close_tagObject



20
21
22
# File 'lib/bio/graphics/svgee.rb', line 20

def close_tag
  %{</svg>}
end

#drawObject



104
105
106
107
108
109
110
111
# File 'lib/bio/graphics/svgee.rb', line 104

def draw
  head = self.open_tag
  defstring = ""
  defstring = "<defs>\n" + self.defs.join("\n") + "\n </defs>\n" if not defs.empty?
  shapes = self.primitives.join("\n")
  close = self.close_tag
  head + defstring + shapes + close
end

#gradient(a) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/bio/graphics/svgee.rb', line 91

def gradient(a)
  definition_string = case a[:type]
  when :radial
    %{<radialGradient id="#{a[:id]}" cx="#{a[:cx]}%" cy="#{a[:cy]}%" r="#{a[:r]}%" fx="#{a[:fx]}%" fy="#{a[:fy]}%">}
  else
    %{<linearGradient id="#{a[:id]}" x1="#{a[:x1]}%" x2="#{a[:x2]}%" y1="#{a[:y1]}%" y2="#{a[:y2]}%">}
  end
  a[:stops].each do |s|
    definition_string = definition_string + "\n" + %{<stop offset="#{s[:offset]}%" style="stop-color:#{s[:color]};stop-opacity:#{s[:opacity]}" />}
  end
  add_def definition_string + (a[:type] == :linear ? '</linearGradient>' : '</radialGradient>')
end

#open_tagObject



16
17
18
# File 'lib/bio/graphics/svgee.rb', line 16

def open_tag
  %{<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="#{@width}" height="#{@height}" style="#{@style}" xmlns:xlink="http://www.w3.org/1999/xlink">}
end