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

Inherits:
Base show all
Includes:
Helpers
Defined in:
lib/booky/layout/base/charts/bar.rb

Constant Summary

Constants inherited from Base

Booky::Layout::Base::Charts::Base::COLORS, Booky::Layout::Base::Charts::Base::OPTIONS

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 Base

#calculate_dimensions, #color, #create_heading, #data_color, #draw_with_color, #initialize, #next_data_color, #start_new_page_if_needed

Methods inherited from Element

#initialize, #method_missing, #next_option, #previous_option

Constructor Details

This class inherits a constructor from Booky::Layout::Base::Charts::Base

Dynamic Method Handling

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

Instance Method Details

#create_backgroundObject



6
7
8
9
10
# File 'lib/booky/layout/base/charts/bar.rb', line 6

def create_background
  draw_with_color :background do
    fill_rectangle [0, cursor], bounds.width, @height
  end
end

#create_barsObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/booky/layout/base/charts/bar.rb', line 115

def create_bars
  return if @data['bars'].nil?
  
  bar_height = (@height - 0.5.cm) / @data['bars'].size
  
  bars = @data['bars']
  bars.sort! { |x,y| y['width'] <=> x['width'] } if @data['sort'] == "ASC"
  bars.sort! { |x,y| x['width'] <=> y['width'] } if @data['sort'] == "DESC"
  
  bars.each_with_index do |bar, index|
    draw_with_color(data_color(index)) do
      
      x, y    = map_point([bar['width'], 0])
      width   = x
      height  = 0.6.cm
      x       = @left
      y       = @top - ((index * bar_height) + ((bar_height - height) / 2))
      
      fill_rectangle [x, y], width, height
      
      name = bar['width'].to_s
      x, y2   = map_point([bar['width'], 0])
      x += 0.2.cm
      y -= 0.2.cm
      draw_with_color :default do
        text_box name, :at => [x, y], :width => @width, :size => 8, :style => :italic, :align => :left
      end
    
    end
  end
  self.line_width = 1
end

#create_gridObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/booky/layout/base/charts/bar.rb', line 12

def create_grid
  draw_with_color :grid do
    
    x_steps         = @data['grid_x_steps']     || 10
    y_steps         = @data['grid_y_steps']     || 8
    x_sub_steps     = @data['grid_x_sub_steps'] || 10
    y_sub_steps     = @data['grid_y_sub_steps'] || 10
    
    x_step_size     = (@width / x_steps).to_f
    y_step_size     = (@height / y_steps).to_f
    x_sub_step_size = x_step_size / x_sub_steps
    y_sub_step_size = y_step_size / y_sub_steps
    
    # X Sublines
    self.line_width = 0.5
    (x_steps * x_sub_steps).times do |sub_step|
      stroke_vertical_line @top, @bottom, :at => (@left + (sub_step * x_sub_step_size))
    end
    
    # X Mainlines
    x_steps += 1
    self.line_width = 1
    x_steps.times do |step|
      stroke_vertical_line @top, @bottom, :at => (@left + (step * x_step_size))
    end
    
    # Y Sublines
    self.line_width = 0.5
    (y_steps * y_sub_steps).times do |sub_step|
      stroke_horizontal_line @left, @right, :at => (@top - (sub_step * y_sub_step_size))
    end
    
    # Y Mainlines
    y_steps += 1
    self.line_width = 1
    y_steps.times do |step|
      stroke_horizontal_line @left, @right, :at => (@top - (step * y_step_size))
    end
    
  end
end

#create_labelsObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/booky/layout/base/charts/bar.rb', line 54

def create_labels
  
  # X Labels
  if @data['labels_x']
    @data['labels_x'].each do |label|
      name  = label.last.to_s
      x     = map_point([label.first,0]).first
      y     = @bottom + 0.45.cm
    
      draw_with_color :default do
        text_box name, :at => [x - 50, y], :width => 100, :align => :center, :size => 8, :style => :italic
      end
    end
  end
  
  # X Axis Labels
  if @data['label_x_axis']
    name  = @data['label_x_axis']
    x     = @right - 0.2.cm
    y     = @bottom + 0.5.cm
    
    draw_with_color :label do
      text_box name, :at => [x - @width, y], :width => @width, :size => 10, :style => :italic, :align => :right
    end
  end
  
end

#create_legendObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/booky/layout/base/charts/bar.rb', line 82

def create_legend
  legend_width = @width / @data['bars'].size
  
  @data['bars'].each_with_index do |bar, index|
    draw_with_color(data_color(index)) do
      name = bar['name']
      x = index * legend_width
      y = @bottom - 0.1.cm
      
      fill_rectangle [x, @bottom], legend_width, 0.5.cm
      
      draw_with_color :background do
        text_box name, :at => [x, y], :width => legend_width, :size => 10, :align => :center
      end
    end
  end
end

#map_point(point) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/booky/layout/base/charts/bar.rb', line 100

def map_point(point)
  x, y    = point
  min_x   = @data['min_x'] || 0
  min_y   = @data['min_y'] || 0
  max_x   = @data['max_x'] || 0
  max_y   = @data['max_y'] || 0
  width   = min_x.abs + max_x.abs
  height  = min_y.abs + max_y.abs
  
  factor_width  = @width / width
  factor_height = @height / height
  
  [(x * factor_width) + @left, (y * factor_height) + @bottom]
end

#move_to_endObject



148
149
150
# File 'lib/booky/layout/base/charts/bar.rb', line 148

def move_to_end
  move_cursor_to @bottom - 0.5.cm
end

#to_prawnObject



152
153
154
155
156
157
158
159
160
# File 'lib/booky/layout/base/charts/bar.rb', line 152

def to_prawn
  create_background
  create_grid
  create_labels
  create_bars
  create_legend
  
  move_to_end
end