Class: ReportEngine::Pdf::Table

Inherits:
Table
  • Object
show all
Defined in:
lib/report_engine/pdf/table.rb

Instance Method Summary collapse

Methods inherited from Table

#add_headers, #add_y_headers, #data

Constructor Details

#initialize(canvas, options) ⇒ Table

Returns a new instance of Table.



5
6
7
8
9
# File 'lib/report_engine/pdf/table.rb', line 5

def initialize(canvas, options)
  super
  @font_size = options[:font_size] || 8
  @padding = options[:padding] || 5
end

Instance Method Details

#height(options = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/report_engine/pdf/table.rb', line 50

def height(options={})
  pdf = @canvas.pdf
  # not an ideal way of getting the height, very aproximative
  font_size = options[:font_size] || 10
  padding = options[:padding] || 10

  line_height = pdf.font.height_at(font_size) + (padding * 2)
  number_of_lines = @y_headers.nil? ? 0 : 1

  if @data.first.is_a? ReportEngine::SubTable
    temp_height = @data.map{|sub_table| sub_table.height(pdf, options) }.inject(0){|sum, v| sum += v}
  else
    number_of_lines +=  @data.size
    temp_height = 0
  end

  temp_height + (number_of_lines * line_height)

end


43
44
45
46
47
48
# File 'lib/report_engine/pdf/table.rb', line 43

def print_legends
  @canvas.pdf.move_down(10)
  @legends.each do |legend|
    @canvas.pdf.text legend, :size => 10
  end
end

#renderObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/report_engine/pdf/table.rb', line 11

def render
  pdf = @canvas.pdf
  if @data.first.is_a? Hash
    @data = @data.map{|sub_table| ReportEngine::Pdf::SubTable.new(@canvas, sub_table.clone).data}.inject([]){|x, y| x+= y }
  end
  add_headers if @x_headers
  table_top_position = pdf.cursor
  margin = @y_label.nil? ? 0 : 40
  pdf.bounding_box([margin, table_top_position], :width => pdf.bounds.width) do
    pdf.table @data,  :border_style => :grid, 
                      :align => :center, 
                      :font_size => @font_size,
                      :vertical_padding => @padding,
                      :column_widths => @column_widths, 
                      :row_colors => @row_colors,
                      :border_color => @border_color
  end
  write_x_label(table_top_position) if @x_label
  write_y_label(table_top_position) if @y_label
  print_legends if @legends
end

#write_x_label(table_top_position) ⇒ Object



39
40
41
# File 'lib/report_engine/pdf/table.rb', line 39

def write_x_label(table_top_position)
  @canvas.pdf.draw_text @x_label, :at => [90, table_top_position + 10]
end

#write_y_label(table_top_position) ⇒ Object



33
34
35
36
37
# File 'lib/report_engine/pdf/table.rb', line 33

def write_y_label(table_top_position)
  pdf = @canvas.pdf
  vertical_text_width = pdf.width_of(@y_label)
  pdf.draw_text @y_label, :at => [30, table_top_position - 30 - vertical_text_width], :rotate => 90
end