Permalink
Browse files

Added tests for bar value labels and fix for 0 marker count

Added commify to String to make numbers look pretty
Added label_format to allow for formatting of value labels
  • Loading branch information...
BRIMIL01 committed Dec 23, 2010
1 parent f64613d commit e7dd9b62d891555d14c1282bf4dafc4cdab895c8
Showing with 27 additions and 2 deletions.
  1. +2 −1 lib/gruff/bar.rb
  2. +12 −1 lib/gruff/base.rb
  3. +13 −0 test/test_bar.rb
View
@@ -92,7 +92,8 @@ def draw_bars
# Subtract half a bar width to center left if requested
draw_label(label_center - (@center_labels_over_point ? @bar_width / 2.0 : 0.0), point_index)
if @show_labels_for_bar_values
- draw_value_label(left_x + (right_x - left_x)/2, conv[0]-30, @norm_data[row_index][3][point_index])
+ val = (@label_formatting || "%.2f") % @norm_data[row_index][3][point_index]
+ draw_value_label(left_x + (right_x - left_x)/2, conv[0]-30, val.commify)
end
end
View
@@ -188,7 +188,11 @@ class Base
# Output the values for the bars on a bar graph
# Default is false
attr_accessor :show_labels_for_bar_values
-
+
+ # Set the number output format for labels using sprintf
+ # Default is "%.2f"
+ attr_accessor :label_formatting
+
# If one numerical argument is given, the graph is drawn at 4/3 ratio
# according to the given width (800 results in 800x600, 400 gives 400x300,
# etc.).
@@ -1105,3 +1109,10 @@ def annotate_scaled(img, width, height, x, y, text, scale)
end
end # Magick
+
+class String
+ #Taken from http://codesnippets.joyent.com/posts/show/330
+ def commify(delimiter=",")
+ return self.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")
+ end
+end
View
@@ -444,6 +444,19 @@ def test_set_label_max_size_and_label_truncation_style
g.write("test/output/bar_set_trailing_dots_trunc.png")
end
+
+ def test_bar_value_labels
+ g = setup_basic_graph
+ g.show_labels_for_bar_values = true
+ g.write("test/output/bar_value_labels.png")
+ end
+
+ def test_zero_marker_count
+ g = setup_basic_graph
+ g.marker_count = 0
+ g.write("test/output/bar_zero_marker_count.png")
+ end
+
protected
def setup_basic_graph(size=800)

0 comments on commit e7dd9b6

Please sign in to comment.