2 require File.dirname(__FILE__) + '/base'
3 require File.dirname(__FILE__) + '/bar_conversion'
5 class Gruff::Bar < Gruff::Base
8 # Labels will be centered over the left of the bar if
9 # there are more labels than columns. This is basically the same
10 # as where it would be for a line graph.
11 @center_labels_over_point = (@labels.keys.length > @column_count ? true : false)
14 return unless @has_data
24 # Columns sit side-by-side.
25 spacing_factor = 0.9 # space between the bars
26 @bar_width = @graph_width / (@column_count * @data.length).to_f
28 @d = @d.stroke_opacity 0.0
30 # Setup the BarConversion Object
31 conversion = Gruff::BarConversion.new()
32 conversion.graph_height = @graph_height
33 conversion.graph_top = @graph_top
35 # Set up the right mode [1,2,3] see BarConversion for further explanation
36 if @minimum_value >= 0 then
37 # all bars go from zero to positiv
40 # all bars go from 0 to negativ
41 if @maximum_value <= 0 then
44 # bars either go from zero to negativ or to positiv
46 conversion.spread = @spread
47 conversion.minimum_value = @minimum_value
48 conversion.zero = -@minimum_value/@spread
52 # iterate over all normalised data
53 @norm_data.each_with_index do |data_row, row_index|
55 data_row[1].each_with_index do |data_point, point_index|
57 # Use incremented x and scaled y
59 left_x = @graph_left + (@bar_width * (row_index + point_index + ((@data.length - 1) * point_index)))
60 right_x = left_x + @bar_width * spacing_factor
63 conversion.getLeftYRightYscaled( data_point, conv )
66 @d = @d.fill data_row[DATA_COLOR_INDEX]
67 @d = @d.rectangle(left_x, conv[0], right_x, conv[1])
69 # Calculate center based on bar_width and current row
70 label_center = @graph_left +
71 (@data.length * @bar_width * point_index) +
72 (@data.length * @bar_width / 2.0)
73 # Subtract half a bar width to center left if requested
74 draw_label(label_center - (@center_labels_over_point ? @bar_width / 2.0 : 0.0), point_index, count)
75 # custom modification to allow graph labels to alternate in height on
76 # bar graphs so they don't overlap
82 # Draw the last label if requested
83 draw_label(@graph_right, @column_count) if @center_labels_over_point
89 unless @x_axis_label.nil?
91 # Centered vertically and horizontally by setting the
92 # height to 1.0 and the width to the width of the graph.
93 x_axis_label_y_coordinate = @graph_bottom + LABEL_MARGIN * 2.5 + @marker_caps_height
95 # TODO Center between graph area
97 @d.font = @font if @font
98 @d.stroke('transparent')
99 @d.pointsize = scale_fontsize(@marker_font_size) * 1.2
100 @d.gravity = NorthGravity
101 @d = @d.annotate_scaled( @base_image,
103 0.0, x_axis_label_y_coordinate,
104 @x_axis_label, @scale)
105 debug { @d.line 0.0, x_axis_label_y_coordinate, @raw_columns, x_axis_label_y_coordinate }
108 unless @y_axis_label.nil?
109 # Y Axis, rotated vertically
111 @d.gravity = CenterGravity
112 @d = @d.annotate_scaled( @base_image,
114 LEFT_MARGIN + @marker_caps_height / 2.0, 0.0,
115 @y_axis_label, @scale)