class Gruff::Bar

Public Instance Methods

draw() click to toggle source
Calls superclass method
# File lib/reconn/visualizer.rb, line 60
def draw
  @center_labels_over_point = (@labels.keys.length > @column_count ? true : false)
  super
  return unless @has_data

  draw_bars
  draw_additional_line
end
draw_additional_line() click to toggle source

Draws additional horizontal line

# File lib/reconn/visualizer.rb, line 137
def draw_additional_line
   @additional_line_colors << '#f61100'
   @d = @d.stroke_opacity 100.0
   i = 0
   @additional_line_values.each do |value|
     @increment_scaled = @graph_height.to_f / (@maximum_value.to_f / value)

     y = @graph_top + @graph_height - @increment_scaled

     @d = @d.stroke(@additional_line_colors[i])
     @d = @d.line(@graph_left, y, @graph_right, y)


     @d.fill = @additional_line_colors[i]
     @d.font = @font if @font
     @d.stroke('transparent')
     @d.pointsize = scale_fontsize(@marker_font_size)
     @d.gravity = EastGravity
     @d = @d.annotate_scaled( @base_image,
                             @graph_right - LABEL_MARGIN, 1.0,
                             0.0, y - (@marker_font_size/2.0),
                             label(value, value), @scale)
     i += 1
   end

  @d = @d.stroke_antialias true
end
draw_bars() click to toggle source
# File lib/reconn/visualizer.rb, line 68
def draw_bars
  # Setup spacing.
  #
  # Columns sit side-by-side.
 @bar_spacing ||= @spacing_factor # space between the bars
  @bar_width = @graph_width / (@column_count * @data.length).to_f
  padding = (@bar_width * (1 - @bar_spacing)) / 2

  @d = @d.stroke_opacity 0.0

  # Setup the BarConversion Object
  conversion = Gruff::BarConversion.new()
  conversion.graph_height = @graph_height
  conversion.graph_top = @graph_top

  # Set up the right mode [1,2,3] see BarConversion for further explanation
  if @minimum_value >= 0 then
    # all bars go from zero to positiv
    conversion.mode = 1
  else
    # all bars go from 0 to negativ
    if @maximum_value <= 0 then
      conversion.mode = 2
    else
      # bars either go from zero to negativ or to positiv
      conversion.mode = 3
      conversion.spread = @spread
      conversion.minimum_value = @minimum_value
      conversion.zero = -@minimum_value/@spread
    end
  end

  # iterate over all normalised data
  @norm_data.each_with_index do |data_row, row_index|

    data_row[DATA_VALUES_INDEX].each_with_index do |data_point, point_index|
      # Use incremented x and scaled y
      # x
      left_x = @graph_left + (@bar_width * (row_index + point_index + ((@data.length - 1) * point_index))) + padding
      right_x = left_x + @bar_width * @bar_spacing
      # y
      conv = []
      conversion.get_left_y_right_y_scaled( data_point, conv )

      # create new bar
      @d = @d.fill data_row[DATA_COLOR_INDEX]
      @d = @d.rectangle(left_x, conv[0], right_x, conv[1])

      # Calculate center based on bar_width and current row
      label_center = @graph_left + 
                  (@data.length * @bar_width * point_index) + 
                  (@data.length * @bar_width / 2.0)

      # Subtract half a bar width to center left if requested
      draw_label(label_center - (@center_labels_over_point ? @bar_width / 2.0 : 0.0), point_index)
      if @show_labels_for_bar_values
        val = (@label_formatting || '%.2f') % @norm_data[row_index][3][point_index]
        draw_value_label(left_x + (right_x - left_x)/2, conv[0]-30, val.commify, true)
      end
    end
  end

  # Draw the last label if requested
  draw_label(@graph_right, @column_count) if @center_labels_over_point

  draw_additional_line
  @d.draw(@base_image)
end