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

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

Constant Summary

Constants inherited from Base

Base::COLORS, 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/line.rb', line 6

def create_background
  draw_with_color :background do
    fill_rectangle [0, cursor], bounds.width, @height
  end
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/line.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/booky/layout/base/charts/line.rb', line 54

def create_labels
  
  # X Labels
  @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
  
  # Y Labels
  @data['labels_y'].each do |label|
    name  = label.last.to_s
    y     = map_point([0, label.first]).last + 0.12.cm
    x     = @left + 0.2.cm
    
    draw_with_color :default do
      text_box name, :at => [x, y], :width => 100, :size => 8, :style => :italic
    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
  
  # Y Axis Labels
  if @data['label_y_axis']
    name  = @data['label_y_axis']
    x     = @left + 0.2.cm
    y     = @top - 0.2.cm
    
    draw_with_color :label do
      text_box name, :at => [x, y], :width => @width, :size => 10, :style => :italic, :align => :left
    end
  end
end

#create_legendObject



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

def create_legend
  legend_width = @width / @data['lines'].size
  
  @data['lines'].each_with_index do |line, index|
    draw_with_color(data_color(index)) do
      name = line['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

#create_linesObject



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/booky/layout/base/charts/line.rb', line 209

def create_lines
  return if @data['lines'].nil?
  
  self.line_width = 1
  @data['lines'].each_with_index do |line, index|
    draw_with_color(data_color(index)) do
      
      x, y = map_point(line['points'].first)
      move_to x, y
      
      stroke do
        line['points'].each do |point|
          x, y = map_point(point)
          line_to x, y
        end
      end
    
    end
  end
  self.line_width = 1
end

#create_markersObject



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/booky/layout/base/charts/line.rb', line 157

def create_markers
  return if @data['lines'].nil?
  
  dash(0.1.cm, :space => 0.1.cm, :phase => 0)
  
  # Horizontal Markers
  unless @data['markers_x'].nil?
    
    self.line_width = 0.1
    @data['markers_x'].each_with_index do |marker, index|
      draw_with_color(data_color(index)) do
        
        name = marker['name']
        x, y = map_point([0, marker['y']])
        stroke_line [@left, y], [@right, y]
        y += 0.35.cm
        x = @left + 0.1.cm
        
        text_box name, :at => [x, y], :width => @width, :size => 8, :style => :italic, :align => :left
        
      end
    end
    self.line_width = 1
    
  end
  
  # Verticla Markers
  unless @data['markers_y'].nil?
    
    self.line_width = 0.1
    @data['markers_y'].each_with_index do |marker, index|
      draw_with_color(data_color(index)) do
        
        name = marker['name']
        x, y = map_point([marker['x'], 0])
        stroke_line [x, @top], [x, @bottom]
        x += 0.35.cm
        y = @top - 1.cm
        
        rotate(-90, :origin => [x, y]) do
          text_box name, :at => [x, y], :width => @width, :size => 8, :style => :italic, :align => :left
        end
      end
    end
    self.line_width = 1
    
  end
  
  undash
  
end

#create_pointsObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/booky/layout/base/charts/line.rb', line 134

def create_points
  return if @data['points'].nil?
  
  self.line_width = 1
  @data['points'].each_with_index do |point, index|
    draw_with_color(data_color(index)) do
      
      name = point['name']
      x, y = map_point(point['point'])
      fill_circle [x, y], 3
      
      x -= @width / 2
      y += 0.5.cm
      
      draw_with_color :default do
        text_box name, :at => [x, y], :width => @width, :size => 8, :style => :italic, :align => :center
      end
    
    end
  end
  self.line_width = 1
end

#map_point(point) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/booky/layout/base/charts/line.rb', line 119

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



231
232
233
# File 'lib/booky/layout/base/charts/line.rb', line 231

def move_to_end
  move_cursor_to @bottom - 0.5.cm
end

#to_prawnObject



235
236
237
238
239
240
241
242
243
244
245
# File 'lib/booky/layout/base/charts/line.rb', line 235

def to_prawn
  create_background
  create_grid
  create_markers
  create_lines
  create_points
  create_labels
  create_legend
  
  move_to_end
end