class Bio::Graphics::SVGEE

The Bio::Graphics::SVGEE class takes argument information in a hash and creates SVG Markup tags, which it will draw

Attributes

defs[R]
primitives[R]
supported_primitives[R]

Public Class Methods

new(args) click to toggle source

Creates a new Bio::Graphics::SVGEE object which will contain all the necessary objects to display all the features on a page

args

  • :width = the width of the SVG page (100%)

  • :height = the amount of the page height the svg should take up (100%)

  • :style = the svg style information

# File lib/bio/graphics/svgee.rb, line 13
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

Public Instance Methods

add_primitive(primitive_object) click to toggle source

Adds a Primitive object to the SVGEE object and makes the svg text for that Primitive

# File lib/bio/graphics/svgee.rb, line 97
def add_primitive(primitive_object)
  args = {}
  primitive_object.instance_variables.each{|v| args[v.to_s.gsub(/@/,"").to_sym] = primitive_object.instance_variable_get(v)  }
  primitive_string = args.delete(:primitive)
  make_tag(primitive_string, args)
end
close_tag() click to toggle source

Produces the closing text for an svg file

# File lib/bio/graphics/svgee.rb, line 29
def close_tag
  %{</svg>}
end
draw() click to toggle source

Produces the svg text to display all the features on a Page

# File lib/bio/graphics/svgee.rb, line 118
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) click to toggle source

Takes the gradient information from a Glyph, which must be of type ‘radial’ or ‘linear’ and creates the svg text for that gradient

  • a = a gradient (a gradient type, a colour and the parameters for a given type)

# File lib/bio/graphics/svgee.rb, line 105
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_tag() click to toggle source

Produces the opening text for an svg file

# File lib/bio/graphics/svgee.rb, line 24
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

Private Instance Methods

add(primitive_string) click to toggle source
# File lib/bio/graphics/svgee.rb, line 34
def add(primitive_string)
  @primitives << primitive_string
end
add_def(definition_string) click to toggle source
# File lib/bio/graphics/svgee.rb, line 38
def add_def(definition_string)
  @defs << definition_string
end
circle_tag(a = {}) click to toggle source
# File lib/bio/graphics/svgee.rb, line 55
def circle_tag(a = {})
  %{<circle cx="#{a[:x_center]}" cy="#{a[:y_center]}" r="#{a[:radius]}" fill="#{a[:fill_color]}" } + common_attributes(a) + %q{/>}
end
common_attributes(a={}) click to toggle source
# File lib/bio/graphics/svgee.rb, line 83
def common_attributes(a={})
  param_str = ""
  a[:params].each{|k,v| param_str << %{ #{k}="#{v}"}} if a[:params]

  %{stroke="#{a[:stroke]}" stroke-width="#{a[:stroke_width]}" style="#{a[:style]}"} + param_str
end
ellipse_tag(a = {}) click to toggle source
# File lib/bio/graphics/svgee.rb, line 63
def ellipse_tag(a = {})
  %{<ellipse cx="#{a[:x_center]}" cy="#{a[:y_center]}" rx="#{a[:x_radius]}" ry="#{a[:y_radius]}" stroke="#{a[:stroke]}" stroke-width="#{a[:stroke_width]}" fill="#{a[:fill_color]}" style="#{a[:style]}" />}
end
line_tag(a = {}) click to toggle source
# File lib/bio/graphics/svgee.rb, line 67
def line_tag(a = {})
  %{<line x1="#{a[:x1]}" y1="#{a[:y1]}" x2="#{a[:x2]}" y2="#{a[:y2]}" } + common_attributes(a) + %q{/>}
end
make_tag(primitive, args) click to toggle source
# File lib/bio/graphics/svgee.rb, line 42
def make_tag(primitive, args)
  if args.has_key?(:link)
    add link_and_tag(primitive, args)
  else
    add self.send("#{primitive}_tag", args)
  end
  return Bio::Graphics::Primitive.new(primitive, args)
end
method_missing(primitive, args={}) click to toggle source
# File lib/bio/graphics/svgee.rb, line 90
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
polygon_tag(a={}) click to toggle source
# File lib/bio/graphics/svgee.rb, line 75
def polygon_tag(a={})
  %{<polygon points="#{a[:points]}" fill="#{a[:fill_color]}" } + common_attributes(a) + %{ />}
end
polyline_tag(a={}) click to toggle source
# File lib/bio/graphics/svgee.rb, line 71
def polyline_tag(a={})
  %{<polyline points="#{a[:points]}" fill="#{a[:fill]}" } + common_attributes(a) + %{ />}
end
rectangle_tag(a = {}) click to toggle source
# File lib/bio/graphics/svgee.rb, line 59
def rectangle_tag(a = {})
  %{<rect x="#{a[:x]}" y="#{a[:y]}" width="#{a[:width]}" height="#{a[:height]}" fill="#{a[:fill_color]}" rx="#{a[:x_round]}" ry="#{a[:y_round]}" } + common_attributes(a) + %q{/>}
end
text_tag(a = {}) click to toggle source
# File lib/bio/graphics/svgee.rb, line 79
def text_tag(a = {})
  %{<text x="#{a[:x]}" y="#{a[:y]}" fill="#{a[:fill]}" transform="#{a[:transform]}" style="#{a[:style]}">#{a[:text]}</text>}
end