Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added the option to change the spacing between bar charts.

  • Loading branch information...
commit 6c07b5cbabb90bfb5292b3676453c753dc09efbe 1 parent aff0047
@nikosd nikosd authored
Showing with 47 additions and 2 deletions.
  1. +18 −2 lib/gruff/bar.rb
  2. +29 −0 test/test_bar.rb
View
20 lib/gruff/bar.rb
@@ -3,6 +3,11 @@
class Gruff::Bar < Gruff::Base
+ def initialize_ivars
+ super
+ @spacing_factor = 0.9
+ end
+
def draw
# Labels will be centered over the left of the bar if
# there are more labels than columns. This is basically the same
@@ -15,6 +20,17 @@ def draw
draw_bars
end
+ # Can be used to adjust the spaces between the bars.
+ # Accepts values between 0.00 and 1.00 where 0.00 means no spacing at all
+ # and 1 means that each bars' width is nearly 0 (so each bar is a simple
+ # line with no x dimension).
+ #
+ # Default value is 0.9.
+ def spacing_factor=(space_percent)
+ raise ArgumentError, "spacing_factor must be between 0.00 and 1.00" unless (space_percent >= 0 and space_percent <= 1)
+ @spacing_factor = (1 - space_percent)
+ end
+
protected
def draw_bars
@@ -23,7 +39,7 @@ def draw_bars
# Columns sit side-by-side.
spacing_factor = 0.9 # space between the bars
@bar_width = @graph_width / (@column_count * @data.length).to_f
- padding = (@bar_width * (1 - spacing_factor)) / 2
+ padding = (@bar_width * (1 - @spacing_factor)) / 2
@d = @d.stroke_opacity 0.0
@@ -56,7 +72,7 @@ def draw_bars
# 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 * spacing_factor
+ right_x = left_x + @bar_width * @spacing_factor
# y
conv = []
conversion.getLeftYRightYscaled( data_point, conv )
View
29 test/test_bar.rb
@@ -221,6 +221,35 @@ def generate_with_y_axis_increment(increment)
g.write("test/output/bar_y_increment_#{increment}.png")
end
+ def test_custom_spacing
+ g = Gruff::Bar.new
+ g.spacing_factor = 0
+ g.title = "Zero spacing graff"
+ g.labels = {
+ 0 => '5/6',
+ 1 => '5/15',
+ 2 => '5/24',
+ 3 => '5/30',
+ }
+
+ g.data(:apples, [1, 5, 8, 4])
+ g.data(:peaches, [4, 1, 2, 10])
+ g.minimum_value = 0
+ g.maximum_value = 10
+ g.write("test/output/bar_zero_spacing.png")
+ end
+
+ def test_spacing_factor_does_not_accept_values_lt_0_and_gt_1
+ g = Gruff::Bar.new
+
+ assert_raise ArgumentError do
+ g.spacing_factor = 1.01
+ end
+
+ assert_raise ArgumentError do
+ g.spacing_factor = -0.01
+ end
+ end
def test_custom_theme
g = Gruff::Bar.new
Please sign in to comment.
Something went wrong with that request. Please try again.