public
Rubygem
Description: Sparkline library for Ruby
Homepage: http://sparklines.rubyforge.org
Clone URL: git://github.com/topfunky/sparklines.git
Fixed drawing and data normalization of bar graph
Added target and target_color to bar graph
topfunky (author)
Mon May 19 13:25:49 -0700 2008
commit  1f1d4fc537ecd2e0d3c1b9a314efdd648b889809
tree    ac0fbe1aa0f3708e58d8cde66a31cfd88c4b46bc
parent  4cccbfdc1ec0e3ae69ef419728475174e1e97c43
...
 
 
 
 
 
1
2
3
...
1
2
3
4
5
6
7
8
0
@@ -1,3 +1,8 @@
0
+== 0.5.1
0
+
0
+* Fixed drawing and normalization of bar graph
0
+* Added :target and :target_color to bar graph (draws a horizontal line at a value)
0
+
0
 == 0.5.0
0
 
0
 * Documentation for bullet graph.
...
75
76
77
78
 
79
80
81
...
87
88
89
90
91
92
93
94
95
96
97
98
 
 
 
 
 
 
 
 
 
99
100
101
102
103
104
105
106
107
108
109
110
111
112
 
 
 
 
 
 
 
 
 
 
 
 
 
113
114
115
...
139
140
141
142
 
143
144
145
 
146
147
148
...
251
252
253
 
 
 
254
255
256
257
 
 
 
258
259
260
261
262
263
264
 
 
 
 
 
 
265
266
 
 
267
268
 
269
270
271
272
 
 
273
274
275
 
 
 
 
 
 
 
276
277
278
...
415
416
417
418
419
420
421
422
423
 
 
 
 
424
425
426
427
428
429
430
431
432
433
434
435
436
 
 
 
 
 
 
 
 
 
437
438
439
 
 
440
441
442
...
541
542
543
544
 
545
546
547
...
611
612
613
614
 
 
 
 
 
 
 
615
 
616
617
618
...
75
76
77
 
78
79
80
81
...
87
88
89
 
 
 
 
 
 
 
 
 
90
91
92
93
94
95
96
97
98
99
 
 
 
 
 
 
 
 
 
 
 
 
 
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
...
139
140
141
 
142
143
144
 
145
146
147
148
...
251
252
253
254
255
256
257
258
 
 
259
260
261
262
263
264
265
 
 
 
266
267
268
269
270
271
272
273
274
275
276
 
277
278
279
 
 
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
...
431
432
433
 
 
434
 
 
 
435
436
437
438
439
440
441
442
 
 
 
 
 
 
 
 
 
443
444
445
446
447
448
449
450
451
452
 
 
453
454
455
456
457
...
556
557
558
 
559
560
561
562
...
626
627
628
 
629
630
631
632
633
634
635
636
637
638
639
640
0
@@ -75,7 +75,7 @@ Licensed under the MIT license.
0
 =end
0
 class Sparklines
0
 
0
- VERSION = '0.5.0'
0
+ VERSION = '0.5.1'
0
 
0
   @@label_margin = 5.0
0
   @@pointsize = 10.0
0
@@ -87,29 +87,29 @@ class Sparklines
0
 
0
     def plot_to_image(data=[], options={})
0
       defaults = {
0
- :type => 'smooth',
0
- :height => 14,
0
- :upper => 50,
0
- :diameter => 20,
0
- :step => 2,
0
- :line_color => 'lightgrey',
0
-
0
- :above_color => 'red',
0
- :below_color => 'grey',
0
+ :type => 'smooth',
0
+ :height => 14,
0
+ :upper => 50,
0
+ :diameter => 20,
0
+ :step => 2,
0
+ :line_color => 'lightgrey',
0
+
0
+ :above_color => 'red',
0
+ :below_color => 'grey',
0
         :background_color => 'white',
0
- :share_color => 'red',
0
- :remain_color => 'lightgrey',
0
- :min_color => 'blue',
0
- :max_color => 'green',
0
- :last_color => 'red',
0
- :std_dev_color => '#efefef',
0
-
0
- :has_min => false,
0
- :has_max => false,
0
- :has_last => false,
0
- :has_std_dev => false,
0
-
0
- :label => nil
0
+ :share_color => 'red',
0
+ :remain_color => 'lightgrey',
0
+ :min_color => 'blue',
0
+ :max_color => 'green',
0
+ :last_color => 'red',
0
+ :std_dev_color => '#efefef',
0
+
0
+ :has_min => false,
0
+ :has_max => false,
0
+ :has_last => false,
0
+ :has_std_dev => false,
0
+
0
+ :label => nil
0
       }
0
 
0
       # HACK for HashWithIndifferentAccess
0
@@ -139,10 +139,10 @@ class Sparklines
0
     def plot(data=[], options={})
0
       plot_to_image(data, options).to_blob
0
     end
0
-
0
+
0
     ##
0
     # Writes a graph to disk with the specified filename, or "sparklines.png".
0
-
0
+
0
     def plot_to_file(filename="sparklines.png", data=[], options={})
0
       File.open( filename, 'wb' ) do |png|
0
         png << self.plot( data, options)
0
@@ -251,28 +251,44 @@ class Sparklines
0
 
0
   ##
0
   # A bar graph.
0
+ #
0
+ # Also takes :target option (a line will be drawn) and :upper (values
0
+ # under will be drawn in :below_color, above in :above_color).
0
 
0
   def bar
0
- step = @options[:step].to_f
0
- height = @options[:height].to_f
0
+ step = @options[:step].to_f
0
+ height = @options[:height].to_f
0
+ width = ((@norm_data.size - 1) * step).to_f
0
     background_color = @options[:background_color]
0
 
0
     create_canvas(@norm_data.length * step + 2, height, background_color)
0
 
0
- upper = @options[:upper].to_f
0
- below_color = @options[:below_color]
0
- above_color = @options[:above_color]
0
+ upper = @options[:upper].to_f
0
+ below_color = @options[:below_color]
0
+ above_color = @options[:above_color]
0
+
0
+ target = @options.has_key?(:target) ? @options[:target].to_f : nil
0
+ target_color = @options[:target_color] || 'white'
0
 
0
     i = 1
0
+ # raise @norm_data.to_yaml
0
+ max_normalized = @norm_data.max
0
     @norm_data.each_with_index do |r, index|
0
- color = (r >= upper) ? above_color : below_color
0
+ color = (@data[index] >= upper) ? above_color : below_color
0
       @draw.stroke('transparent')
0
       @draw.fill(color)
0
- @draw.rectangle( i, @canvas.rows,
0
- i + step - 2, @canvas.rows - ( (r / @maximum_value) * @canvas.rows) )
0
+ bar_height_from_top = @canvas.rows - ( (r.to_f / max_normalized.to_f) * @canvas.rows)
0
+ @draw.rectangle( i, @canvas.rows, i + step - 2, bar_height_from_top )
0
       i += step
0
     end
0
 
0
+ unless target.nil?
0
+ normalized_target_value = ((target.to_f - @minimum_value)/(@maximum_value - @minimum_value)) * 100.0
0
+ adjusted_target_value = (height - 3 - normalized_target_value/(101.0/(height-4))).to_i
0
+ @draw.stroke(target_color)
0
+ open_ended_polyline([[-5, adjusted_target_value], [width + 5, adjusted_target_value]])
0
+ end
0
+
0
     @draw.draw(@canvas)
0
     @canvas
0
   end
0
@@ -415,28 +431,27 @@ class Sparklines
0
   # :target - A 1px horizontal line will be drawn at this value. Useful for showing an average.
0
   #
0
   # :target_color - Color of the target line. Defaults to white.
0
-
0
- def smooth
0
 
0
- step = @options[:step].to_f
0
- height = @options[:height].to_f
0
- width = ((@norm_data.size - 1) * step).to_f
0
+ def smooth
0
+ step = @options[:step].to_f
0
+ height = @options[:height].to_f
0
+ width = ((@norm_data.size - 1) * step).to_f
0
 
0
     background_color = @options[:background_color]
0
     create_canvas(width, height, background_color)
0
 
0
- min_color = @options[:min_color]
0
- max_color = @options[:max_color]
0
- last_color = @options[:last_color]
0
- has_min = @options[:has_min]
0
- has_max = @options[:has_max]
0
- has_last = @options[:has_last]
0
- line_color = @options[:line_color]
0
- has_std_dev = @options[:has_std_dev]
0
- std_dev_color = @options[:std_dev_color]
0
+ min_color = @options[:min_color]
0
+ max_color = @options[:max_color]
0
+ last_color = @options[:last_color]
0
+ has_min = @options[:has_min]
0
+ has_max = @options[:has_max]
0
+ has_last = @options[:has_last]
0
+ line_color = @options[:line_color]
0
+ has_std_dev = @options[:has_std_dev]
0
+ std_dev_color = @options[:std_dev_color]
0
 
0
- target = @options.has_key?(:target) ? @options[:target].to_f : nil
0
- target_color = @options[:target_color] || 'white'
0
+ target = @options.has_key?(:target) ? @options[:target].to_f : nil
0
+ target_color = @options[:target_color] || 'white'
0
 
0
     drawstddevbox(width,height,std_dev_color) if has_std_dev == true
0
 
0
@@ -541,7 +556,7 @@ class Sparklines
0
   # :height - Numeric. Defaults to 15. Should be a multiple of three.
0
   #
0
   # :width - This graph expands to any specified width. Defaults to 100.
0
- #
0
+ #
0
   # :bad - Numeric. A darker shade background will be drawn up to this point.
0
   #
0
   # :satisfactory - Numeric. A medium background will be drawn up to this point.
0
@@ -611,8 +626,15 @@ class Sparklines
0
   private
0
 
0
   def normalize_data
0
- @minimum_value = @data.min
0
+ case @options[:type].to_s
0
+ when 'bar'
0
+ @minimum_value = 0.0
0
+ else
0
+ @minimum_value = @data.min
0
+ end
0
+
0
     @maximum_value = @data.max
0
+
0
     case @options[:type].to_s
0
     when 'pie'
0
       @norm_data = @data
...
1
2
3
4
 
5
6
7
...
1
2
3
 
4
5
6
7
0
@@ -1,7 +1,7 @@
0
 
0
 Gem::Specification.new do |s|
0
   s.name = %q{sparklines}
0
- s.version = "0.5.0"
0
+ s.version = "0.5.1"
0
 
0
   s.specification_version = 2 if s.respond_to? :specification_version=
0
 

Comments

    No one has commented yet.