Skip to content
Browse files

Print long numbers with thousand separators.

  • Loading branch information...
1 parent dbbd5ef commit 5e9f5a959a8a0b7e095574bad5ebf5247382d6d6 @jcoglan jcoglan committed
Showing with 17 additions and 5 deletions.
  1. +9 −5 lib/gruff/base.rb
  2. +8 −0 test/test_bar.rb
View
14 lib/gruff/base.rb
@@ -41,6 +41,8 @@ class Base
DEFAULT_MARGIN = 20.0
DEFAULT_TARGET_WIDTH = 800
+
+ THOUSAND_SEPARATOR = ','
# Blank space above the graph
attr_accessor :top_margin
@@ -1061,17 +1063,19 @@ def increment_color
# Return a formatted string representing a number value that should be
# printed as a label.
def label(value)
- if (@spread.to_f % @marker_count.to_f == 0) || !@y_axis_increment.nil?
- return value.to_i.to_s
- end
-
- if @spread > 10.0
+ label = if (@spread.to_f % @marker_count.to_f == 0) || !@y_axis_increment.nil?
+ value.to_i.to_s
+ elsif @spread > 10.0
sprintf("%0i", value)
elsif @spread >= 3.0
sprintf("%0.2f", value)
else
value.to_s
end
+
+ parts = label.split('.')
+ parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{THOUSAND_SEPARATOR}")
+ parts.join('.')
end
# Returns the height of the capital letter 'X' for the current font and
View
8 test/test_bar.rb
@@ -33,6 +33,14 @@ def test_bar_graph
g.theme_odeo
g.write("test/output/bar_odeo.png")
end
+
+ def test_thousand_separators
+ g = Gruff::Bar.new(600)
+ g.title = "Formatted numbers"
+ g.marker_count = 8
+ g.data("data", [4025, 1024, 50257, 703672, 1580456])
+ g.write("test/output/bar_formatted_numbers.png")
+ end
def test_bar_graph_set_colors
g = Gruff::Bar.new

0 comments on commit 5e9f5a9

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