class Minichart::Base

Base class for all Minichart classes

Attributes

data[R]
options[R]

Public Class Methods

defaults() click to toggle source

For subclasses to define

# File lib/minichart/base.rb, line 20
def defaults
  {}
end
master_defaults() click to toggle source
# File lib/minichart/base.rb, line 7
def master_defaults
  {
    background: 'white',
    height: 100,
    width: 300,
    stroke: 2,
    style: {},
    color: '#66f',
    padding: 10,
  }
end
new(data, user_options = {}) click to toggle source
Calls superclass method
# 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
options(update_hash = nil) click to toggle source
# 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

Public Instance Methods

build() click to toggle source
# File lib/minichart/base.rb, line 68
def build
  raise NotImplementedError, "#build is not implemented"
end
full_height() click to toggle source
# File lib/minichart/base.rb, line 60
def full_height
  options[:height] + options[:padding] * 2
end
full_width() click to toggle source
# File lib/minichart/base.rb, line 64
def full_width
  options[:width] + options[:padding] * 2
end
setup_clip_path(id) click to toggle source
# 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
viewbox() click to toggle source
# File lib/minichart/base.rb, line 56
def viewbox
  "0 0 #{full_width} #{full_height}"
end