class Charts::Chart

Attributes

data[R]
options[R]
prepared_data[R]
renderer[R]

Public Class Methods

new(data, opts = {}) click to toggle source
# File lib/charts/chart.rb, line 7
def initialize(data, opts = {})
  validate_arguments(data, opts)
  @data = data
  @options = default_options.merge opts
  create_options_methods
  initialize_instance_variables
  @prepared_data = prepare_data
end

Public Instance Methods

create_options_methods() click to toggle source
# File lib/charts/chart.rb, line 97
def create_options_methods
  options.each do |key, value|
    define_singleton_method key, proc { value }
  end
end
default_options() click to toggle source
# File lib/charts/chart.rb, line 38
def default_options
  {
    title:            nil,
    type:             :svg,
    outer_margin:     30,
    background_color: 'white',
    labels:           [],
    colors:           [
      '#e41a1d',
      '#377eb9',
      '#4daf4b',
      '#984ea4',
      '#ff7f01',
      '#ffff34',
      '#a65629',
      '#f781c0',
      '#888888'
    ]
  }
end
draw() click to toggle source
# File lib/charts/chart.rb, line 75
def draw
  raise NotImplementedError
end
draw_background() click to toggle source
# File lib/charts/chart.rb, line 83
def draw_background
  renderer.rect 0, 0, width, height, fill: background_color, class: 'background_color'
end
draw_title() click to toggle source
# File lib/charts/chart.rb, line 87
def draw_title
  return unless options[:title]
  x = width / 2
  y = outer_margin / 2 + 2 * renderer.font_size / 5
  renderer.text options[:title], x, y, text_anchor: 'middle', class: 'title'
end
initialize_instance_variables() click to toggle source
# File lib/charts/chart.rb, line 94
def initialize_instance_variables
end
post_draw() click to toggle source
# File lib/charts/chart.rb, line 79
def post_draw
  renderer.post_draw
end
pre_draw() click to toggle source
# File lib/charts/chart.rb, line 69
def pre_draw
  @renderer = Charts::Renderer.new(self)
  draw_background
  draw_title
end
prepare_data() click to toggle source
# File lib/charts/chart.rb, line 59
def prepare_data
  data
end
render() click to toggle source
# File lib/charts/chart.rb, line 63
def render
  pre_draw
  draw
  post_draw
end
validate_arguments(data, options) click to toggle source
# File lib/charts/chart.rb, line 16
def validate_arguments(data, options)
  raise ArgumentError.new('Data missing') if data.empty?
  raise ArgumentError.new('Data not an array') unless data.is_a? Array
  raise ArgumentError.new('Options missing') unless options.is_a? Hash
  if options[:outer_margin] and !options[:outer_margin].is_a?(Numeric)
    raise ArgumentError.new('outer_margin not a number')
  end
  validate_array_and_count(data, options, :colors)
  validate_array_and_count(data, options, :labels)
end
validate_array_and_count(data, options, key) click to toggle source
# File lib/charts/chart.rb, line 27
def validate_array_and_count(data, options, key)
  if options[key]
    unless options[key].is_a? Array
      raise ArgumentError.new("#{ key } not an array")
    end
    if options[key].any? and data.count > options[key].count
      raise ArgumentError.new("not enough #{ key }")
    end
  end
end