Class: SVGPlot::Plot

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

Overview

Main Plot object

Constant Summary collapse

DEFAULTS =
{
  version: 1.1,
  xmlns: 'http://www.w3.org/2000/svg',
  :'xmlns:xlink' => 'http://www.w3.org/1999/xlink' # rubocop:disable Style/HashSyntax, Metrics/LineLength, Lint/UnneededDisable
}

Instance Attribute Summary

Attributes inherited from ChildTag

#img

Attributes inherited from Tag

#attributes, #children, #defaults, #tag

Instance Method Summary collapse

Methods inherited from ChildTag

#linear_gradient, #radial_gradient

Methods inherited from Tag

#append_child, #method_missing, #path, #raw, #respond_to?, #spawn_child, #to_s, #use

Methods included from Expansion

#expand

Methods included from Transform

#matrix, #rotate, #scale, #skew_x, #skew_y, #translate

Constructor Details

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

Returns a new instance of Plot.



11
12
13
14
15
16
# File 'lib/svgplot/plot.rb', line 11

def initialize(params = {}, output = nil, &block)
  params = DEFAULTS.dup.merge params
  super(self, 'svg', params, &block)
  @output = output || ''
  write(@output) if block
end

Dynamic Method Handling

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

Instance Method Details

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



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/svgplot/plot.rb', line 18

def add_def(id, child, if_exists = :skip, &block)
  @defs ||= ChildTag.new(@img, 'defs')
  @defs_ids ||= {}
  old_id = check_conflicts(id, if_exists) if @defs_ids.key? id
  return old_id if old_id

  child.attributes[:id] = id
  @defs.append_child child
  @defs_ids[id] = child
  child.instance_exec block if block

  child
end

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



32
33
34
35
# File 'lib/svgplot/plot.rb', line 32

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

#write(output) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/svgplot/plot.rb', line 37

def write(output)
  fail("Illegal output: #{@output.inspect}") unless @output.respond_to? :<<
  output << header
  @children.unshift @defs if @defs
  super(output)
  @children.shift if @defs
end