lib/gruff/bar.rb in gruff-0.1.2 vs lib/gruff/bar.rb in gruff-0.2.3
- old
+ new
@@ -14,15 +14,20 @@
spacing_factor = 0.9
@bar_width = @graph_width / (@column_count * @data.length).to_f
@d = @d.stroke_opacity 0.0
- # setup the BarConversion Object
+ # 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 explains
+
+ # 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
@@ -52,14 +57,20 @@
# 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)
- draw_label(label_center, point_index)
+ 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
end