Class: Gruff::Bar

Inherits:
Base
  • Object
show all
Defined in:
lib/gruff/bar.rb

Constant Summary

Constants inherited from Base

Gruff::Base::DATA_COLOR_INDEX, Gruff::Base::DATA_LABEL_INDEX, Gruff::Base::DATA_VALUES_INDEX, Gruff::Base::DEBUG, Gruff::Base::LEGEND_MARGIN, Gruff::Base::TOP_MARGIN

Constants included from Magick

Magick::BoldWeight, Magick::CenterGravity, Magick::EastGravity, Magick::NormalWeight, Magick::NorthEastGravity, Magick::NorthGravity, Magick::NorthWestGravity, Magick::SouthEastGravity, Magick::SouthGravity, Magick::SouthWestGravity, Magick::WestGravity

Instance Attribute Summary

Attributes inherited from Base

#additional_line_values, #colors, #font, #hide_legend, #hide_line_markers, #hide_title, #labels, #legend_font_size, #marker_color, #marker_count, #marker_font_size, #maximum_value, #minimum_value, #no_data_message, #stacked, #title, #title_font_size, #x_axis_label, #y_axis_increment, #y_axis_label

Instance Method Summary collapse

Methods inherited from Base

#add_color, #data, #initialize, #replace_colors, #theme=, #theme_37signals, #theme_keynote, #theme_odeo, #theme_rails_keynote, #to_blob, #write

Methods included from Deprecated

#graph_height, #graph_left, #graph_top, #graph_width, #scale_measurements, #total_height

Constructor Details

This class inherits a constructor from Gruff::Base

Instance Method Details

#drawObject



7
8
9
10
11
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/gruff/bar.rb', line 7

def draw
  super
  return unless @has_data

  # Setup spacing.
  #
  # Columns sit side-by-side.
  spacing_factor = 0.9
  @bar_width = @graph_width / (@column_count * @data.length).to_f

  @d = @d.stroke_opacity 0.0

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

  # Labels will be centered over the left of the bar if
  # there are more labels than columns.
  center_labels_left = (@labels.keys.length > @column_count ? true : false)

  # 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|
    @d = @d.fill data_row[DATA_COLOR_INDEX]

    data_row[1].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)))
      right_x = left_x + @bar_width * spacing_factor
      # y
      conv = []
      conversion.getLeftYRightYscaled( data_point, conv )

      # create new bar
      @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_left ? @bar_width / 2.0 : 0.0), point_index)
    end

  end

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

  @d.draw(@base_image)    
end