Class: Charts::Chart

Inherits:
Object
  • Object
show all
Defined in:
lib/charts/chart.rb

Direct Known Subclasses

BarChart, CountChart, PieChart

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, opts = {}) ⇒ Chart

Returns a new instance of Chart.



7
8
9
10
11
12
13
14
# 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

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



2
3
4
# File 'lib/charts/chart.rb', line 2

def data
  @data
end

#optionsObject (readonly)

Returns the value of attribute options.



2
3
4
# File 'lib/charts/chart.rb', line 2

def options
  @options
end

#prepared_dataObject (readonly)

Returns the value of attribute prepared_data.



2
3
4
# File 'lib/charts/chart.rb', line 2

def prepared_data
  @prepared_data
end

#rendererObject (readonly)

Returns the value of attribute renderer.



2
3
4
# File 'lib/charts/chart.rb', line 2

def renderer
  @renderer
end

Instance Method Details

#create_options_methodsObject



97
98
99
100
101
# 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_optionsObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 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

#drawObject

Raises:

  • (NotImplementedError)


75
76
77
# File 'lib/charts/chart.rb', line 75

def draw
  raise NotImplementedError
end

#draw_backgroundObject



83
84
85
# File 'lib/charts/chart.rb', line 83

def draw_background
  renderer.rect 0, 0, width, height, fill: background_color, class: 'background_color'
end

#draw_titleObject



87
88
89
90
91
92
# 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_variablesObject



94
95
# File 'lib/charts/chart.rb', line 94

def initialize_instance_variables
end

#post_drawObject



79
80
81
# File 'lib/charts/chart.rb', line 79

def post_draw
  renderer.post_draw
end

#pre_drawObject



69
70
71
72
73
# File 'lib/charts/chart.rb', line 69

def pre_draw
  @renderer = Charts::Renderer.new(self)
  draw_background
  draw_title
end

#prepare_dataObject



59
60
61
# File 'lib/charts/chart.rb', line 59

def prepare_data
  data
end

#renderObject



63
64
65
66
67
# File 'lib/charts/chart.rb', line 63

def render
  pre_draw
  draw
  post_draw
end

#validate_arguments(data, options) ⇒ Object

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
# 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) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# 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