Class: Minichart::Base

Inherits:
Victor::SVGBase
  • Object
show all
Defined in:
lib/minichart/base.rb

Overview

Base class for all Minichart classes

Direct Known Subclasses

Chart, Leds, Meter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, user_options = {}) ⇒ Base

Returns a new instance of Base.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/minichart/base.rb', line 31

def initialize(data, user_options = {})
  @data = data
  @options = self.class.options.merge user_options

  super viewBox: viewbox, style: options[:style]
  element :rect, x: 0, y: 0,
    width: full_width, height: full_height,
    fill: options[:background], stroke_width: 0

  clip_path_id = IDGenerator.next
  setup_clip_path clip_path_id

  element :g, clip_path: "url(##{clip_path_id})" do
    build
  end      
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



4
5
6
# File 'lib/minichart/base.rb', line 4

def data
  @data
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/minichart/base.rb', line 4

def options
  @options
end

Class Method Details

.defaultsObject

For subclasses to define



20
21
22
# File 'lib/minichart/base.rb', line 20

def defaults
  {}
end

.master_defaultsObject



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/minichart/base.rb', line 7

def master_defaults
  {
    background: 'white',
    height: 100,
    width: 300,
    stroke: 2,
    style: {},
    color: '#66f',
    padding: 10,
  }
end

.options(update_hash = nil) ⇒ Object



24
25
26
27
28
# File 'lib/minichart/base.rb', line 24

def options(update_hash = nil)
  @options ||= master_defaults.merge defaults
  @options.merge! update_hash if update_hash
  @options
end

Instance Method Details

#buildObject

Raises:

  • (NotImplementedError)


68
69
70
# File 'lib/minichart/base.rb', line 68

def build
  raise NotImplementedError, "#build is not implemented"
end

#full_heightObject



60
61
62
# File 'lib/minichart/base.rb', line 60

def full_height
  options[:height] + options[:padding] * 2
end

#full_widthObject



64
65
66
# File 'lib/minichart/base.rb', line 64

def full_width
  options[:width] + options[:padding] * 2
end

#setup_clip_path(id) ⇒ Object



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

def setup_clip_path(id)
  element :defs do
    element :clipPath, id: id do
      element :rect, width: full_width, height: full_height
    end
  end
end

#viewboxObject



56
57
58
# File 'lib/minichart/base.rb', line 56

def viewbox
  "0 0 #{full_width} #{full_height}"
end