Skip to content
This repository
Browse code

Provide a bar_spacing attr_accessor on bar-type graphs so users can c…

…hanges the spacing factor.
  • Loading branch information...
commit 084063f7575c01d039debd05e4e03b45dad79043 1 parent 5e9f5a9
James Coglan jcoglan authored
12 lib/gruff/bar.rb
@@ -3,6 +3,9 @@
3 3
4 4 class Gruff::Bar < Gruff::Base
5 5
  6 + # Spacing factor applied between bars
  7 + attr_accessor :bar_spacing
  8 +
6 9 def draw
7 10 # Labels will be centered over the left of the bar if
8 11 # there are more labels than columns. This is basically the same
@@ -21,9 +24,9 @@ def draw_bars
21 24 # Setup spacing.
22 25 #
23 26 # Columns sit side-by-side.
24   - spacing_factor = 0.9 # space between the bars
  27 + @bar_spacing ||= 0.9 # space between the bars
25 28 @bar_width = @graph_width / (@column_count * @data.length).to_f
26   - padding = (@bar_width * (1 - spacing_factor)) / 2
  29 + padding = (@bar_width * (1 - @bar_spacing)) / 2
27 30
28 31 @d = @d.stroke_opacity 0.0
29 32
@@ -56,7 +59,7 @@ def draw_bars
56 59 # Use incremented x and scaled y
57 60 # x
58 61 left_x = @graph_left + (@bar_width * (row_index + point_index + ((@data.length - 1) * point_index))) + padding
59   - right_x = left_x + @bar_width * spacing_factor
  62 + right_x = left_x + @bar_width * @bar_spacing
60 63 # y
61 64 conv = []
62 65 conversion.getLeftYRightYscaled( data_point, conv )
@@ -68,8 +71,7 @@ def draw_bars
68 71 # Calculate center based on bar_width and current row
69 72 label_center = @graph_left +
70 73 (@data.length * @bar_width * point_index) +
71   - (@data.length * @bar_width / 2.0) +
72   - padding
  74 + (@data.length * @bar_width / 2.0)
73 75 # Subtract half a bar width to center left if requested
74 76 draw_label(label_center - (@center_labels_over_point ? @bar_width / 2.0 : 0.0), point_index)
75 77 end
8 lib/gruff/side_bar.rb
@@ -13,14 +13,14 @@ def draw
13 13
14 14 # Setup spacing.
15 15 #
16   - spacing_factor = 0.9
  16 + @bar_spacing ||= 0.9
17 17
18 18 @bars_width = @graph_height / @column_count.to_f
19   - @bar_width = @bars_width * spacing_factor / @norm_data.size
  19 + @bar_width = @bars_width * @bar_spacing / @norm_data.size
20 20 @d = @d.stroke_opacity 0.0
21 21 height = Array.new(@column_count, 0)
22 22 length = Array.new(@column_count, @graph_left)
23   - padding = (@bars_width * (1 - spacing_factor)) / 2
  23 + padding = (@bars_width * (1 - @bar_spacing)) / 2
24 24
25 25 @norm_data.each_with_index do |data_row, row_index|
26 26 @d = @d.fill data_row[DATA_COLOR_INDEX]
@@ -44,7 +44,7 @@ def draw
44 44 @d = @d.rectangle(left_x, left_y, right_x, right_y)
45 45
46 46 # Calculate center based on bar_width and current row
47   - label_center = @graph_top + (@bars_width * point_index + @bars_width / 2) + padding
  47 + label_center = @graph_top + (@bars_width * point_index + @bars_width / 2)
48 48 draw_label(label_center, point_index)
49 49 end
50 50
8 lib/gruff/side_stacked_bar.rb
@@ -21,13 +21,13 @@ def draw
21 21 # Setup spacing.
22 22 #
23 23 # Columns sit stacked.
24   - spacing_factor = 0.9
  24 + @bar_spacing ||= 0.9
25 25
26 26 @bar_width = @graph_height / @column_count.to_f
27 27 @d = @d.stroke_opacity 0.0
28 28 height = Array.new(@column_count, 0)
29 29 length = Array.new(@column_count, @graph_left)
30   - padding = (@bar_width * (1 - spacing_factor)) / 2
  30 + padding = (@bar_width * (1 - @bar_spacing)) / 2
31 31
32 32 @norm_data.each_with_index do |data_row, row_index|
33 33 @d = @d.fill data_row[DATA_COLOR_INDEX]
@@ -45,14 +45,14 @@ def draw
45 45 left_x = length[point_index] #+ 1
46 46 left_y = @graph_top + (@bar_width * point_index) + padding
47 47 right_x = left_x + difference
48   - right_y = left_y + @bar_width * spacing_factor
  48 + right_y = left_y + @bar_width * @bar_spacing
49 49 length[point_index] += difference
50 50 height[point_index] += (data_point * @graph_width - 2)
51 51
52 52 @d = @d.rectangle(left_x, left_y, right_x, right_y)
53 53
54 54 # Calculate center based on bar_width and current row
55   - label_center = @graph_top + (@bar_width * point_index) + (@bar_width * spacing_factor / 2.0) + padding
  55 + label_center = @graph_top + (@bar_width * point_index) + (@bar_width * @bar_spacing / 2.0)
56 56 draw_label(label_center, point_index)
57 57 end
58 58
8 lib/gruff/stacked_bar.rb
@@ -14,9 +14,9 @@ def draw
14 14 # Setup spacing.
15 15 #
16 16 # Columns sit stacked.
17   - spacing_factor = 0.9
  17 + @bar_spacing ||= 0.9
18 18 @bar_width = @graph_width / @column_count.to_f
19   - padding = (@bar_width * (1 - spacing_factor)) / 2
  19 + padding = (@bar_width * (1 - @bar_spacing)) / 2
20 20
21 21 @d = @d.stroke_opacity 0.0
22 22
@@ -27,7 +27,7 @@ def draw
27 27 @d = @d.fill data_row[DATA_COLOR_INDEX]
28 28
29 29 # Calculate center based on bar_width and current row
30   - label_center = @graph_left + (@bar_width * point_index) + (@bar_width * spacing_factor / 2.0) + padding
  30 + label_center = @graph_left + (@bar_width * point_index) + (@bar_width * @bar_spacing / 2.0)
31 31 draw_label(label_center, point_index)
32 32
33 33 next if (data_point == 0)
@@ -36,7 +36,7 @@ def draw
36 36 left_y = @graph_top + (@graph_height -
37 37 data_point * @graph_height -
38 38 height[point_index]) + 1
39   - right_x = left_x + @bar_width * spacing_factor
  39 + right_x = left_x + @bar_width * @bar_spacing
40 40 right_y = @graph_top + @graph_height - height[point_index] - 1
41 41
42 42 # update the total height of the current stacked bar
1  test/test_bar.rb
@@ -37,6 +37,7 @@ def test_bar_graph
37 37 def test_thousand_separators
38 38 g = Gruff::Bar.new(600)
39 39 g.title = "Formatted numbers"
  40 + g.bar_spacing = 0.2
40 41 g.marker_count = 8
41 42 g.data("data", [4025, 1024, 50257, 703672, 1580456])
42 43 g.write("test/output/bar_formatted_numbers.png")

0 comments on commit 084063f

Please sign in to comment.
Something went wrong with that request. Please try again.