Class: Booky::Layout::Base::Charts::Base

Inherits:
Element
  • Object
show all
Includes:
Helpers
Defined in:
lib/booky/layout/base/charts/base.rb

Direct Known Subclasses

Bar, Line

Constant Summary collapse

OPTIONS =
{
  :align => :left,
  :size => 12
}
COLORS =
{
  :default                => 'FFFFFF',
  :background             => '2B2B2B',
  :label                  => '575757',
  :legend                 => '000000',
  :grid                   => '303030',
  :data                   => ['6D9CBE', 'A5C361', 'DA4939', 'FFC66D', 'D0D0FF', 'D1FFFD']
}

Instance Attribute Summary

Attributes inherited from Element

#ast, #document, #index, #options

Instance Method Summary collapse

Methods included from Helpers

#add_to_toc, #is_first_rendering?, #update_levels

Methods inherited from Element

#method_missing, #next_option, #previous_option, #to_prawn

Constructor Details

#initialize(options, document, layout, data) ⇒ Base

Returns a new instance of Base.



20
21
22
23
24
25
26
27
28
29
# File 'lib/booky/layout/base/charts/base.rb', line 20

def initialize(options, document, layout, data)
  @data = data
  super(options, document, layout)
  
  @data_colors = Fiber.new { COLORS[:data].cycle{ |color| Fiber.yield(color) } }
  
  start_new_page_if_needed
  create_heading
  calculate_dimensions
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Booky::Layout::Element

Instance Method Details

#calculate_dimensionsObject



31
32
33
34
35
36
37
38
39
# File 'lib/booky/layout/base/charts/base.rb', line 31

def calculate_dimensions
  @width  = bounds.width
  @height = eval(@data['height'])
  
  @top    = cursor
  @bottom = @top - @height
  @left   = 0
  @right  = bounds.right
end

#color(name) ⇒ Object



54
55
56
# File 'lib/booky/layout/base/charts/base.rb', line 54

def color(name)
  COLORS[name]
end

#create_headingObject



45
46
47
48
49
50
51
52
# File 'lib/booky/layout/base/charts/base.rb', line 45

def create_heading
  return unless @options[:name]

  fill_color Booky::Layout::Base::COLORS[1]
  text @options[:name], OPTIONS
  fill_color Booky::Layout::Base::COLORS[0]
  move_down 0.2.cm
end

#data_color(index) ⇒ Object



62
63
64
65
# File 'lib/booky/layout/base/charts/base.rb', line 62

def data_color(index)
  index = index % COLORS[:data].size
  COLORS[:data][index]
end

#draw_with_color(name) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/booky/layout/base/charts/base.rb', line 67

def draw_with_color(name)
  last_fill_color = fill_color
  last_stroke_color = stroke_color
  
  new_color = name.is_a?(Symbol) ? color(name) : name
  
  fill_color new_color
  stroke_color new_color
  yield
  fill_color last_fill_color
  stroke_color last_fill_color
end

#next_data_colorObject



58
59
60
# File 'lib/booky/layout/base/charts/base.rb', line 58

def next_data_color
  @data_colors.resume
end

#start_new_page_if_neededObject



41
42
43
# File 'lib/booky/layout/base/charts/base.rb', line 41

def start_new_page_if_needed
  start_new_page if cursor < (eval(@data['height']) + 1.cm)
end