Class: SVGPlot::Plot

Inherits:
SVGTagWithParent show all
Defined in:
lib/svgplot/plot.rb

Instance Attribute Summary

Attributes inherited from SVGTagWithParent

#img

Attributes inherited from SVGTag

#attributes, #children, #tag

Instance Method Summary collapse

Methods inherited from SVGTag

#append_child, #linearGradient, #matrix, #merge_defaults, #method_missing, #path, #pop_defaults, #push_defaults, #radialGradient, #raw, #rotate, #scale, #skewX, #skewY, #spawn_child, #to_s, #translate, #use, #validate_attribute, #validate_attributes, #validate_child_name, #validate_tag, #with_style, #write_points, #write_styles, #write_transforms

Constructor Details

#initialize(params = {}, output = nil, &block) ⇒ Plot

Returns a new instance of Plot.



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/svgplot/plot.rb', line 328

def initialize(params = {}, output=nil, &block)
  @defs = nil
  @defs_ids = {}

  params[:"version"] = "1.1" unless params[:"version"]
  params[:"xmlns"] = "http://www.w3.org/2000/svg" unless params[:"xmlns"]
  params[:"xmlns:xlink"] = "http://www.w3.org/1999/xlink" unless params[:"xmlns:xlink"]
  super(self, "svg", params, &block)

  @output = (output or "")
  validate_output(@output) if output

  if block
    write(@output)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class SVGPlot::SVGTag

Instance Method Details

#add_def(id, child, if_exists = :skip, &block) ⇒ Object



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/svgplot/plot.rb', line 345

def add_def(id, child, if_exists = :skip, &block)
  #init on the fly if needed
  @defs = SVGPlot::SVGTagWithParent.new(@img, "defs") if @defs.nil?

  #raise an error if id is already present and if_exists is :fail
  raise "Definition '#{id}' already exists" if @defs_ids.has_key? id and if_exists == :fail

  #return the existing element if id is already present and if_exists is :skip
  return @defs_ids[id] if if_exists == :skip and @defs_ids.has_key? id

  #search for the existing element
  if @defs_ids[id]
    old_idx = nil
    @defs.children.each_with_index { |c,i| if c.attributes[:id] == id then old_idx = i ; break end }
  end

  #force the id, append the child to definitions and call the given block to fill the group
  child.attributes[:id] = id
  @defs.append_child child
  @defs_ids[id] = child
  child.instance_exec block

  #remove the old element if present
  @defs.children.delete_at old_idx if old_idx

  return child
end

#def_group(id, if_exists = :skip, &block) ⇒ Object



373
374
375
376
# File 'lib/svgplot/plot.rb', line 373

def def_group(id, if_exists = :skip, &block)
  g = SVGPlot::SVGTagWithParent.new(@img, "g", :id => id)
  return add_def(id, g, if_exists, &block)
end

#write(output) ⇒ Object



396
397
398
399
400
401
402
403
# File 'lib/svgplot/plot.rb', line 396

def write(output)
  validate_output(output)
  write_header(output)

  @children.unshift @defs if @defs
  super(output)
  @children.shift if @defs
end