Permalink
Browse files

Added label_format option for trailing label and number

  • Loading branch information...
1 parent 51c1d70 commit 32cf81dc97301f101166931d113d1ea925506265 @topfunky committed Jul 15, 2009
Showing with 124 additions and 111 deletions.
  1. +12 −5 lib/sparklines.rb
  2. BIN test/expected/labeled_bar_sprintf.png
  3. +112 −106 test/test_all.rb
View
17 lib/sparklines.rb
@@ -67,6 +67,8 @@
:below_color => 'grey'
:background_color => 'white'
:line_color => 'lightgrey'
+ :label => name_of_label_after_graph
+ :label_format => sprintf_string_to_format_label
==License
@@ -743,10 +745,8 @@ def create_canvas(w, h, bkg_col)
@options[:has_last] = true
end
@label_width = calculate_width(@options[:label])
- @data_last_width = calculate_width(@data.last.to_s)
-
- puts "Width #{w}"
-
+ @data_last_width = calculate_width(formatted_last_data_string)
+
# TODO: Must figure out correct spacing
@label_and_data_last_width = @label_width + @data_last_width + @@label_margin * 3.0
w += @label_and_data_last_width
@@ -777,8 +777,15 @@ def create_canvas(w, h, bkg_col)
@draw.annotate( @canvas,
@data_last_width, 1.0,
w - @data_last_width - @@label_margin, h - calculate_caps_height/2.0,
- @data.last.to_s)
+ formatted_last_data_string)
+ end
+ end
+
+ def formatted_last_data_string
+ if @options[:label_format]
+ return sprintf(@options[:label_format], @data.last)
end
+ @data.last.to_s
end
##
View
BIN test/expected/labeled_bar_sprintf.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
218 test/test_all.rb
@@ -23,76 +23,76 @@ def setup
test "basic bullet" do
Sparklines.plot_to_file("#{@output_dir}/bullet_basic.png", 85, {
- :type => "bullet",
- :target => 80,
- :good => 100,
- :height => 15
- })
+ :type => "bullet",
+ :target => 80,
+ :good => 100,
+ :height => 15
+ })
end
test "full-featured bullet" do
Sparklines.plot_to_file("#{@output_dir}/bullet_full_featured.png", 85, {
- :type => "bullet",
- :target => 90,
- :bad => 60,
- :satisfactory => 80,
- :good => 100,
- :height => 15
- })
+ :type => "bullet",
+ :target => 90,
+ :bad => 60,
+ :satisfactory => 80,
+ :good => 100,
+ :height => 15
+ })
end
test "colorful bullet" do
Sparklines.plot_to_file("#{@output_dir}/bullet_colorful.png", 85, {
- :type => "bullet",
- :target => 90,
- :bad => 60,
- :satisfactory => 80,
- :good => 100,
- :height => 15,
- :bad_color => '#c3e3bf',
- :satisfactory_color => '#96cf90',
- :good_color => "#6ab162"
- })
+ :type => "bullet",
+ :target => 90,
+ :bad => 60,
+ :satisfactory => 80,
+ :good => 100,
+ :height => 15,
+ :bad_color => '#c3e3bf',
+ :satisfactory_color => '#96cf90',
+ :good_color => "#6ab162"
+ })
end
test "tall bullet" do
Sparklines.plot_to_file("#{@output_dir}/bullet_tall.png", 85, {
- :type => "bullet",
- :target => 90,
- :bad => 60,
- :satisfactory => 80,
- :good => 100,
- :height => 30
- })
+ :type => "bullet",
+ :target => 90,
+ :bad => 60,
+ :satisfactory => 80,
+ :good => 100,
+ :height => 30
+ })
end
test "wide bullet" do
Sparklines.plot_to_file("#{@output_dir}/bullet_wide.png", 85, {
- :type => "bullet",
- :target => 90,
- :bad => 60,
- :satisfactory => 80,
- :good => 100,
- :height => 15,
- :width => 200
- })
+ :type => "bullet",
+ :target => 90,
+ :bad => 60,
+ :satisfactory => 80,
+ :good => 100,
+ :height => 15,
+ :width => 200
+ })
end
test "smooth with target" do
quick_graph("smooth_with_target", {
- :type => "smooth",
- :target => 50,
- :target_color => '#999999',
- :line_color => "#6699cc",
- :underneath_color => "#ebf3f6"
- })
+ :type => "smooth",
+ :target => 50,
+ :target_color => '#999999',
+ :line_color => "#6699cc",
+ :underneath_color => "#ebf3f6"
+ })
end
test "whisker with step" do
quick_graph("whisker_with_step", {
- :type => "whisker",
- :step => 5
- })
+ :type => "whisker",
+ :step => 5
+ })
end
def test_each_graph
@@ -106,7 +106,11 @@ def test_each_graph_with_label
quick_graph("labeled_#{type}", :type => type, :label => 'Glucose')
end
end
-
+
+ test "bar graph with sprintf for label" do
+ quick_graph("labeled_bar_sprintf", :type => "bar", :label => "Formatted", :label_format => "%i")
+ end
+
def test_each_graph_with_nil
%w{pie area discrete smooth bar}.each do |type|
nil_graph("nil_#{type}", :type => type, :label => 'Glucose')
@@ -120,10 +124,10 @@ def test_smooth_graph_with_target_and_constant_data
def test_whisker_decimals
@data = (1..200).map {|n| n.to_f/100 }
quick_graph("labeled_whisker_decimals", {
- :height => 30,
- :type => 'smooth',
- :label => 'png'
- })
+ :height => 30,
+ :type => 'smooth',
+ :label => 'png'
+ })
end
def test_whisker_random
@@ -149,16 +153,16 @@ def test_pie
# Test extremes which previously did not work right
[0, 1, 45, 95, 99, 100].each do |value|
Sparklines.plot_to_file("#{@output_dir}/pie#{value}.png",
- value, {
- :type => 'pie',
- :diameter => 128
- })
+ value, {
+ :type => 'pie',
+ :diameter => 128
+ })
end
Sparklines.plot_to_file("#{@output_dir}/pie_flat.png",
- [60],
- {
- :type => 'pie'
- })
+ [60],
+ {
+ :type => 'pie'
+ })
end
def test_special_conditions
@@ -215,49 +219,49 @@ def test_special_conditions
def test_bar_extreme_values
Sparklines.plot_to_file("#{@output_dir}/bar_extreme_values.png",
- [0,1,100,2,99,3,98,4,97,5,96,6,95,7,94,8,93,9,92,10,91], {
- :type => 'bar',
- :below_color => 'blue',
- :above_color => 'red',
- :upper => 90,
- :step => 4
- })
+ [0,1,100,2,99,3,98,4,97,5,96,6,95,7,94,8,93,9,92,10,91], {
+ :type => 'bar',
+ :below_color => 'blue',
+ :above_color => 'red',
+ :upper => 90,
+ :step => 4
+ })
end
def test_string_args
quick_graph("bar_string.png", {
- 'type' => 'bar',
- 'below_color' => 'blue',
- 'above_color' => 'red',
- 'upper' => 50,
- 'height' => 50,
- 'step' => 8
- })
+ 'type' => 'bar',
+ 'below_color' => 'blue',
+ 'above_color' => 'red',
+ 'upper' => 50,
+ 'height' => 50,
+ 'step' => 8
+ })
end
def test_area_min_max
quick_graph("area_min_max", {
- :has_min => true,
- :has_max => true,
- :has_first => true,
- :has_last => true
- })
+ :has_min => true,
+ :has_max => true,
+ :has_first => true,
+ :has_last => true
+ })
end
def test_smooth_underneath_color
quick_graph("smooth_underneath_color", {
- :type => 'smooth',
- :line_color => "#6699cc",
- :underneath_color => "#ebf3f6"
- })
+ :type => 'smooth',
+ :line_color => "#6699cc",
+ :underneath_color => "#ebf3f6"
+ })
end
def test_close_values
Sparklines.plot_to_file("#{@output_dir}/smooth_similar_nonzero_values.png", [100, 90, 95, 99, 80, 90], {
- :type => 'smooth',
- :line_color => "#6699cc",
- :underneath_color => "#ebf3f6"
- })
+ :type => 'smooth',
+ :line_color => "#6699cc",
+ :underneath_color => "#ebf3f6"
+ })
end
def test_no_type
@@ -266,39 +270,39 @@ def test_no_type
def test_standard_deviation
quick_graph('standard_deviation', {
- :type => 'smooth',
- :height => 100,
- :line_color => '#666',
- :has_std_dev => true,
- :std_dev_color => '#cccccc'
- })
+ :type => 'smooth',
+ :height => 100,
+ :line_color => '#666',
+ :has_std_dev => true,
+ :std_dev_color => '#cccccc'
+ })
end
def test_standard_deviation_tall
quick_graph('standard_deviation_tall', {
- :type => 'smooth',
- :height => 300,
- :line_color => '#666',
- :has_std_dev => true,
- :std_dev_color => '#cccccc'
- })
+ :type => 'smooth',
+ :height => 300,
+ :line_color => '#666',
+ :has_std_dev => true,
+ :std_dev_color => '#cccccc'
+ })
end
def test_standard_deviation_short
quick_graph('standard_deviation_short', {
- :type => 'smooth',
- :height => 20,
- :line_color => '#666',
- :has_std_dev => true,
- :std_dev_color => '#cccccc'
- })
+ :type => 'smooth',
+ :height => 20,
+ :line_color => '#666',
+ :has_std_dev => true,
+ :std_dev_color => '#cccccc'
+ })
end
def test_zeros
@data = [0,0,0,0]
quick_graph('zeros', {
- :type => 'smooth',
- })
+ :type => 'smooth',
+ })
end
private
@@ -344,4 +348,6 @@ def image_tag(image_path)
EOL
f.write output
end
+
+ # TODO: Squawk if there are files in actual that aren't in expected
}

0 comments on commit 32cf81d

Please sign in to comment.