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|
56 # Use incremented x and scaled y
58 left_x = @graph_left + (@bar_width * (row_index + point_index + ((@data.length - 1) * point_index)))
59 right_x = left_x + @bar_width * spacing_factor
62 conversion.getLeftYRightYscaled( data_point, conv )
65 @d = @d.fill data_row[DATA_COLOR_INDEX]
66 @d = @d.rectangle(left_x, conv[0], right_x, conv[1])
68 # Calculate center based on bar_width and current row
69 label_center = @graph_left +
70 (@data.length * @bar_width * point_index) +
71 (@data.length * @bar_width / 2.0)
72 # Subtract half a bar width to center left if requested
73 draw_label(label_center - (@center_labels_over_point ? @bar_width / 2.0 : 0.0), point_index)
78 # Draw the last label if requested
79 draw_label(@graph_right, @column_count) if @center_labels_over_point