Skip to content

Commit

Permalink
Print long numbers with thousand separators.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoglan committed Aug 21, 2009
1 parent dbbd5ef commit 5e9f5a9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/gruff/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions test/test_bar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5e9f5a9

Please sign in to comment.