Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added label_format option for trailing label and number

  • Loading branch information...
commit 32cf81dc97301f101166931d113d1ea925506265 1 parent 51c1d70
Geoffrey Grosenbach authored July 14, 2009
17  lib/sparklines.rb
@@ -67,6 +67,8 @@
67 67
  :below_color       => 'grey'
68 68
  :background_color  => 'white'
69 69
  :line_color        => 'lightgrey'
  70
+ :label             => name_of_label_after_graph
  71
+ :label_format      => sprintf_string_to_format_label
70 72
 
71 73
 ==License
72 74
 
@@ -743,10 +745,8 @@ def create_canvas(w, h, bkg_col)
743 745
         @options[:has_last] = true
744 746
       end
745 747
       @label_width = calculate_width(@options[:label])
746  
-      @data_last_width = calculate_width(@data.last.to_s)
747  
-      
748  
-      puts "Width #{w}"
749  
-      
  748
+      @data_last_width = calculate_width(formatted_last_data_string)
  749
+
750 750
       # TODO: Must figure out correct spacing
751 751
       @label_and_data_last_width = @label_width + @data_last_width + @@label_margin * 3.0
752 752
       w += @label_and_data_last_width
@@ -777,8 +777,15 @@ def create_canvas(w, h, bkg_col)
777 777
       @draw.annotate( @canvas,
778 778
                       @data_last_width, 1.0,
779 779
                       w - @data_last_width - @@label_margin, h - calculate_caps_height/2.0,
780  
-                      @data.last.to_s)
  780
+                      formatted_last_data_string)
  781
+    end
  782
+  end
  783
+
  784
+  def formatted_last_data_string
  785
+    if @options[:label_format]
  786
+      return sprintf(@options[:label_format], @data.last)
781 787
     end
  788
+    @data.last.to_s
782 789
   end
783 790
 
784 791
   ##
BIN  test/expected/labeled_bar_sprintf.png
218  test/test_all.rb
@@ -23,76 +23,76 @@ def setup
23 23
 
24 24
   test "basic bullet" do
25 25
     Sparklines.plot_to_file("#{@output_dir}/bullet_basic.png", 85, {
26  
-      :type => "bullet",
27  
-      :target => 80,
28  
-      :good => 100,
29  
-      :height => 15
30  
-    })
  26
+                              :type => "bullet",
  27
+                              :target => 80,
  28
+                              :good => 100,
  29
+                              :height => 15
  30
+                            })
31 31
   end
32 32
 
33 33
   test "full-featured bullet" do
34 34
     Sparklines.plot_to_file("#{@output_dir}/bullet_full_featured.png", 85, {
35  
-      :type => "bullet",
36  
-      :target => 90,
37  
-      :bad => 60,
38  
-      :satisfactory => 80,
39  
-      :good => 100,
40  
-      :height => 15
41  
-    })
  35
+                              :type => "bullet",
  36
+                              :target => 90,
  37
+                              :bad => 60,
  38
+                              :satisfactory => 80,
  39
+                              :good => 100,
  40
+                              :height => 15
  41
+                            })
42 42
   end
43 43
 
44 44
   test "colorful bullet" do
45 45
     Sparklines.plot_to_file("#{@output_dir}/bullet_colorful.png", 85, {
46  
-      :type => "bullet",
47  
-      :target => 90,
48  
-      :bad => 60,
49  
-      :satisfactory => 80,
50  
-      :good => 100,
51  
-      :height => 15,
52  
-      :bad_color => '#c3e3bf',
53  
-      :satisfactory_color => '#96cf90',
54  
-      :good_color => "#6ab162"
55  
-    })
  46
+                              :type => "bullet",
  47
+                              :target => 90,
  48
+                              :bad => 60,
  49
+                              :satisfactory => 80,
  50
+                              :good => 100,
  51
+                              :height => 15,
  52
+                              :bad_color => '#c3e3bf',
  53
+                              :satisfactory_color => '#96cf90',
  54
+                              :good_color => "#6ab162"
  55
+                            })
56 56
   end
57 57
 
58 58
   test "tall bullet" do
59 59
     Sparklines.plot_to_file("#{@output_dir}/bullet_tall.png", 85, {
60  
-      :type => "bullet",
61  
-      :target => 90,
62  
-      :bad => 60,
63  
-      :satisfactory => 80,
64  
-      :good => 100,
65  
-      :height => 30
66  
-    })
  60
+                              :type => "bullet",
  61
+                              :target => 90,
  62
+                              :bad => 60,
  63
+                              :satisfactory => 80,
  64
+                              :good => 100,
  65
+                              :height => 30
  66
+                            })
67 67
   end
68 68
 
69 69
   test "wide bullet" do
70 70
     Sparklines.plot_to_file("#{@output_dir}/bullet_wide.png", 85, {
71  
-      :type => "bullet",
72  
-      :target => 90,
73  
-      :bad => 60,
74  
-      :satisfactory => 80,
75  
-      :good => 100,
76  
-      :height => 15,
77  
-      :width => 200
78  
-    })
  71
+                              :type => "bullet",
  72
+                              :target => 90,
  73
+                              :bad => 60,
  74
+                              :satisfactory => 80,
  75
+                              :good => 100,
  76
+                              :height => 15,
  77
+                              :width => 200
  78
+                            })
79 79
   end
80 80
 
81 81
   test "smooth with target" do
82 82
     quick_graph("smooth_with_target", {
83  
-      :type => "smooth",
84  
-      :target => 50,
85  
-      :target_color => '#999999',
86  
-      :line_color => "#6699cc",
87  
-      :underneath_color => "#ebf3f6"
88  
-    })
  83
+                  :type => "smooth",
  84
+                  :target => 50,
  85
+                  :target_color => '#999999',
  86
+                  :line_color => "#6699cc",
  87
+                  :underneath_color => "#ebf3f6"
  88
+                })
89 89
   end
90 90
 
91 91
   test "whisker with step" do
92 92
     quick_graph("whisker_with_step", {
93  
-      :type => "whisker",
94  
-      :step => 5
95  
-    })
  93
+                  :type => "whisker",
  94
+                  :step => 5
  95
+                })
96 96
   end
97 97
 
98 98
   def test_each_graph
@@ -106,7 +106,11 @@ def test_each_graph_with_label
106 106
       quick_graph("labeled_#{type}", :type => type, :label => 'Glucose')
107 107
     end
108 108
   end
109  
-
  109
+  
  110
+  test "bar graph with sprintf for label" do
  111
+    quick_graph("labeled_bar_sprintf", :type => "bar", :label => "Formatted", :label_format => "%i")    
  112
+  end
  113
+  
110 114
   def test_each_graph_with_nil
111 115
     %w{pie area discrete smooth bar}.each do |type|
112 116
       nil_graph("nil_#{type}", :type => type, :label => 'Glucose')
@@ -120,10 +124,10 @@ def test_smooth_graph_with_target_and_constant_data
120 124
   def test_whisker_decimals
121 125
     @data = (1..200).map {|n| n.to_f/100 }
122 126
     quick_graph("labeled_whisker_decimals", {
123  
-      :height => 30,
124  
-      :type => 'smooth',
125  
-      :label => 'png'
126  
-    })
  127
+                  :height => 30,
  128
+                  :type => 'smooth',
  129
+                  :label => 'png'
  130
+                })
127 131
   end
128 132
 
129 133
   def test_whisker_random
@@ -149,16 +153,16 @@ def test_pie
149 153
     # Test extremes which previously did not work right
150 154
     [0, 1, 45, 95, 99, 100].each do |value|
151 155
       Sparklines.plot_to_file("#{@output_dir}/pie#{value}.png",
152  
-      value, {
153  
-        :type => 'pie',
154  
-        :diameter => 128
155  
-      })
  156
+                              value, {
  157
+                                :type => 'pie',
  158
+                                :diameter => 128
  159
+                              })
156 160
     end
157 161
     Sparklines.plot_to_file("#{@output_dir}/pie_flat.png",
158  
-    [60],
159  
-    {
160  
-      :type => 'pie'
161  
-    })
  162
+                            [60],
  163
+                            {
  164
+                              :type => 'pie'
  165
+                            })
162 166
   end
163 167
 
164 168
   def test_special_conditions
@@ -215,49 +219,49 @@ def test_special_conditions
215 219
 
216 220
   def test_bar_extreme_values
217 221
     Sparklines.plot_to_file("#{@output_dir}/bar_extreme_values.png",
218  
-    [0,1,100,2,99,3,98,4,97,5,96,6,95,7,94,8,93,9,92,10,91], {
219  
-      :type => 'bar',
220  
-      :below_color => 'blue',
221  
-      :above_color => 'red',
222  
-      :upper => 90,
223  
-      :step => 4
224  
-    })
  222
+                            [0,1,100,2,99,3,98,4,97,5,96,6,95,7,94,8,93,9,92,10,91], {
  223
+                              :type => 'bar',
  224
+                              :below_color => 'blue',
  225
+                              :above_color => 'red',
  226
+                              :upper => 90,
  227
+                              :step => 4
  228
+                            })
225 229
   end
226 230
 
227 231
   def test_string_args
228 232
     quick_graph("bar_string.png", {
229  
-      'type' => 'bar',
230  
-      'below_color' => 'blue',
231  
-      'above_color' => 'red',
232  
-      'upper' => 50,
233  
-      'height' => 50,
234  
-      'step' => 8
235  
-    })
  233
+                  'type' => 'bar',
  234
+                  'below_color' => 'blue',
  235
+                  'above_color' => 'red',
  236
+                  'upper' => 50,
  237
+                  'height' => 50,
  238
+                  'step' => 8
  239
+                })
236 240
   end
237 241
 
238 242
   def test_area_min_max
239 243
     quick_graph("area_min_max", {
240  
-      :has_min => true,
241  
-      :has_max => true,
242  
-      :has_first => true,
243  
-      :has_last => true
244  
-    })
  244
+                  :has_min => true,
  245
+                  :has_max => true,
  246
+                  :has_first => true,
  247
+                  :has_last => true
  248
+                })
245 249
   end
246 250
 
247 251
   def test_smooth_underneath_color
248 252
     quick_graph("smooth_underneath_color", {
249  
-      :type => 'smooth',
250  
-      :line_color => "#6699cc",
251  
-      :underneath_color => "#ebf3f6"
252  
-    })
  253
+                  :type => 'smooth',
  254
+                  :line_color => "#6699cc",
  255
+                  :underneath_color => "#ebf3f6"
  256
+                })
253 257
   end
254 258
 
255 259
   def test_close_values
256 260
     Sparklines.plot_to_file("#{@output_dir}/smooth_similar_nonzero_values.png", [100, 90, 95, 99, 80, 90], {
257  
-      :type => 'smooth',
258  
-      :line_color => "#6699cc",
259  
-      :underneath_color => "#ebf3f6"
260  
-    })
  261
+                              :type => 'smooth',
  262
+                              :line_color => "#6699cc",
  263
+                              :underneath_color => "#ebf3f6"
  264
+                            })
261 265
   end
262 266
 
263 267
   def test_no_type
@@ -266,39 +270,39 @@ def test_no_type
266 270
 
267 271
   def test_standard_deviation
268 272
     quick_graph('standard_deviation', {
269  
-      :type => 'smooth',
270  
-      :height => 100,
271  
-      :line_color => '#666',
272  
-      :has_std_dev => true,
273  
-      :std_dev_color => '#cccccc'
274  
-    })
  273
+                  :type => 'smooth',
  274
+                  :height => 100,
  275
+                  :line_color => '#666',
  276
+                  :has_std_dev => true,
  277
+                  :std_dev_color => '#cccccc'
  278
+                })
275 279
   end
276 280
 
277 281
   def test_standard_deviation_tall
278 282
     quick_graph('standard_deviation_tall', {
279  
-      :type => 'smooth',
280  
-      :height => 300,
281  
-      :line_color => '#666',
282  
-      :has_std_dev => true,
283  
-      :std_dev_color => '#cccccc'
284  
-    })
  283
+                  :type => 'smooth',
  284
+                  :height => 300,
  285
+                  :line_color => '#666',
  286
+                  :has_std_dev => true,
  287
+                  :std_dev_color => '#cccccc'
  288
+                })
285 289
   end
286 290
 
287 291
   def test_standard_deviation_short
288 292
     quick_graph('standard_deviation_short', {
289  
-      :type => 'smooth',
290  
-      :height => 20,
291  
-      :line_color => '#666',
292  
-      :has_std_dev => true,
293  
-      :std_dev_color => '#cccccc'
294  
-    })
  293
+                  :type => 'smooth',
  294
+                  :height => 20,
  295
+                  :line_color => '#666',
  296
+                  :has_std_dev => true,
  297
+                  :std_dev_color => '#cccccc'
  298
+                })
295 299
   end
296 300
 
297 301
   def test_zeros
298 302
     @data = [0,0,0,0]
299 303
     quick_graph('zeros', {
300  
-      :type => 'smooth',
301  
-    })
  304
+                  :type => 'smooth',
  305
+                })
302 306
   end
303 307
 
304 308
   private
@@ -344,4 +348,6 @@ def image_tag(image_path)
344 348
     EOL
345 349
     f.write output
346 350
   end
  351
+
  352
+  # TODO: Squawk if there are files in actual that aren't in expected
347 353
 }

0 notes on commit 32cf81d

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