class DYI::Chart::Table

@since 0.0.0

Attributes

data_canvas[R]
frame_canvas[R]

Public Instance Methods

column_colors() click to toggle source
# File lib/dyi/chart/table.rb, line 105
def column_colors
  @options[:column_colors]
end
column_colors=(colors) click to toggle source
# File lib/dyi/chart/table.rb, line 109
def column_colors=(colors)
  if colors && !colors.empty?
    @options[:column_colors] = colors
  else
    @options.delete(:column_colors)
  end
  colors
end
column_width(index) click to toggle source
# File lib/dyi/chart/table.rb, line 70
def column_width(index)
  case column_widths
    when Length then column_widths
    when Array then column_widths[index]
    else width / series.size
  end
end
column_widths() click to toggle source
# File lib/dyi/chart/table.rb, line 55
def column_widths
  @options[:column_widths]
end
column_widths=(widths) click to toggle source
# File lib/dyi/chart/table.rb, line 59
def column_widths=(widths)
  if widths.kind_of?(Array)
    @options[:column_widths] = widths.map {|width| Length.new_or_nil(width)}
  elsif widths
    @options[:column_widths] = Length.new(widths)
  else
    @options.delete(:column_widths)
  end
  point
end
font() click to toggle source
# File lib/dyi/chart/table.rb, line 29
def font
  @options[:font]
end
font=(font) click to toggle source
# File lib/dyi/chart/table.rb, line 33
def font=(font)
  if font && !font.empty?
    @options[:font] = Font.new(font)
  else
    @options.delete(:font)
  end
  font
end
horizontal_position(index) click to toggle source
# File lib/dyi/chart/table.rb, line 101
def horizontal_position(index)
  @options[:horizontal_positions] && @options[:horizontal_positions][index]
end
horizontal_positions() click to toggle source
# File lib/dyi/chart/table.rb, line 88
def horizontal_positions
  @options[:horizontal_positions]
end
horizontal_positions=(positions) click to toggle source
# File lib/dyi/chart/table.rb, line 92
def horizontal_positions=(positions)
  if positions && !positions.empty?
    @options[:horizontal_positions] = positions
  else
    @options.delete(:horizontal_positions)
  end
  positions
end
row_height() click to toggle source
# File lib/dyi/chart/table.rb, line 42
def row_height
  @options[:row_height] ||= (font && font.draw_size || Font::DEFAULT_SIZE) * 1.4
end
row_height=(height) click to toggle source
# File lib/dyi/chart/table.rb, line 46
def row_height=(height)
  if heights
    @options[:row_height] = Length.new(height)
  else
    @options.delete(:row_height)
  end
  point
end
table_height() click to toggle source
# File lib/dyi/chart/table.rb, line 84
def table_height
  row_height * data[series.first].size
end
table_width() click to toggle source
# File lib/dyi/chart/table.rb, line 78
def table_width
  series.inject(Length.new(0)) do |width, i|
    width + column_width(i)
  end
end

Private Instance Methods

convert_data(value) click to toggle source
# File lib/dyi/chart/table.rb, line 142
def convert_data(value)
  value.strip
end
create_vector_image() click to toggle source
# File lib/dyi/chart/table.rb, line 146
def create_vector_image
  pen = Drawing::Pen.new
  @frame_canvas = Shape::ShapeGroup.draw_on(@canvas)
  @data_canvas = Shape::ShapeGroup.draw_on(@canvas)
  draw_frame(pen)
  draw_data(pen)
end
default_csv_format() click to toggle source
# File lib/dyi/chart/table.rb, line 138
def default_csv_format
  [0, 1]
end
draw_column_colors() click to toggle source
# File lib/dyi/chart/table.rb, line 170
def draw_column_colors
  if column_colors
    brush = Drawing::Brush.new
    h = table_height

    series.inject(Length.new(0)) do |x, j|
      if column_colors[j]
        brush.color = Color.new(column_colors[j])
        brush.draw_rectangle(@frame_canvas, [x, 0], column_width(j), h)
        x + column_width(j)
      end
    end
  end
end
draw_data(pen) click to toggle source
# File lib/dyi/chart/table.rb, line 185
def draw_data(pen)
  cell_margin = (row_height - (font && font.draw_size || Font::DEFAULT_SIZE)) / 2
  series.inject(cell_margin) do |x, column_index|
    y = row_height - cell_margin
    data[column_index].each do |value|
      case horizontal_position(column_index)
        when 'middle' then pen.draw_text(@data_canvas, [x + column_width(column_index) / 2 - cell_margin, y], value, :text_anchor=>'middle')
        when 'end' then pen.draw_text(@data_canvas, [x + column_width(column_index) - cell_margin * 2, y], value, :text_anchor=>'end')
        else pen.draw_text(@data_canvas, [x, y], value)
      end
      y += row_height
    end
    x + column_width(column_index)
  end
end
draw_frame(pen) click to toggle source
# File lib/dyi/chart/table.rb, line 154
def draw_frame(pen)
  w = table_width
  h = table_height

  draw_column_colors

  pen.draw_rectangle(@frame_canvas, [0, 0], w, h)
  (1...data[series.first].size).each do |i|
    pen.draw_line(@frame_canvas, [0, row_height * i], [w, row_height * i])
  end
  series.inject(Length.new(0)) do |x, j|
    pen.draw_line(@frame_canvas, [x, 0], [x, h]) if x.nonzero?
    x + column_width(j)
  end
end
options() click to toggle source

def precedence_attribute

@options[:precedence] || :column

end

def precedence=(row_or_column)

case row_or_column.to_sym
  when :column then @options[:precedence] = :column
  when :row then @options[:precedence] = :row
  when nil, false then @options.delete(:precedence)
  else raise ArgumentError, "\"#{row_or_column}\" is invalid value"
end
row_or_column

end

# File lib/dyi/chart/table.rb, line 134
def options
  @options
end