sparklines 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/CHANGELOG +5 -0
  2. data/lib/sparklines.rb +76 -52
  3. metadata +5 -5
data/CHANGELOG CHANGED
@@ -1,3 +1,8 @@
1
+ == 0.5.1
2
+
3
+ * Fixed drawing and normalization of bar graph
4
+ * Added :target and :target_color to bar graph (draws a horizontal line at a value)
5
+
1
6
  == 0.5.0
2
7
 
3
8
  * Documentation for bullet graph.
@@ -75,7 +75,7 @@ Licensed under the MIT license.
75
75
  =end
76
76
  class Sparklines
77
77
 
78
- VERSION = '0.5.0'
78
+ VERSION = '0.5.1'
79
79
 
80
80
  @@label_margin = 5.0
81
81
  @@pointsize = 10.0
@@ -87,29 +87,29 @@ class Sparklines
87
87
 
88
88
  def plot_to_image(data=[], options={})
89
89
  defaults = {
90
- :type => 'smooth',
91
- :height => 14,
92
- :upper => 50,
93
- :diameter => 20,
94
- :step => 2,
95
- :line_color => 'lightgrey',
96
-
97
- :above_color => 'red',
98
- :below_color => 'grey',
90
+ :type => 'smooth',
91
+ :height => 14,
92
+ :upper => 50,
93
+ :diameter => 20,
94
+ :step => 2,
95
+ :line_color => 'lightgrey',
96
+
97
+ :above_color => 'red',
98
+ :below_color => 'grey',
99
99
  :background_color => 'white',
100
- :share_color => 'red',
101
- :remain_color => 'lightgrey',
102
- :min_color => 'blue',
103
- :max_color => 'green',
104
- :last_color => 'red',
105
- :std_dev_color => '#efefef',
106
-
107
- :has_min => false,
108
- :has_max => false,
109
- :has_last => false,
110
- :has_std_dev => false,
111
-
112
- :label => nil
100
+ :share_color => 'red',
101
+ :remain_color => 'lightgrey',
102
+ :min_color => 'blue',
103
+ :max_color => 'green',
104
+ :last_color => 'red',
105
+ :std_dev_color => '#efefef',
106
+
107
+ :has_min => false,
108
+ :has_max => false,
109
+ :has_last => false,
110
+ :has_std_dev => false,
111
+
112
+ :label => nil
113
113
  }
114
114
 
115
115
  # HACK for HashWithIndifferentAccess
@@ -139,10 +139,10 @@ class Sparklines
139
139
  def plot(data=[], options={})
140
140
  plot_to_image(data, options).to_blob
141
141
  end
142
-
142
+
143
143
  ##
144
144
  # Writes a graph to disk with the specified filename, or "sparklines.png".
145
-
145
+
146
146
  def plot_to_file(filename="sparklines.png", data=[], options={})
147
147
  File.open( filename, 'wb' ) do |png|
148
148
  png << self.plot( data, options)
@@ -251,28 +251,44 @@ class Sparklines
251
251
 
252
252
  ##
253
253
  # A bar graph.
254
+ #
255
+ # Also takes :target option (a line will be drawn) and :upper (values
256
+ # under will be drawn in :below_color, above in :above_color).
254
257
 
255
258
  def bar
256
- step = @options[:step].to_f
257
- height = @options[:height].to_f
259
+ step = @options[:step].to_f
260
+ height = @options[:height].to_f
261
+ width = ((@norm_data.size - 1) * step).to_f
258
262
  background_color = @options[:background_color]
259
263
 
260
264
  create_canvas(@norm_data.length * step + 2, height, background_color)
261
265
 
262
- upper = @options[:upper].to_f
263
- below_color = @options[:below_color]
264
- above_color = @options[:above_color]
266
+ upper = @options[:upper].to_f
267
+ below_color = @options[:below_color]
268
+ above_color = @options[:above_color]
269
+
270
+ target = @options.has_key?(:target) ? @options[:target].to_f : nil
271
+ target_color = @options[:target_color] || 'white'
265
272
 
266
273
  i = 1
274
+ # raise @norm_data.to_yaml
275
+ max_normalized = @norm_data.max
267
276
  @norm_data.each_with_index do |r, index|
268
- color = (r >= upper) ? above_color : below_color
277
+ color = (@data[index] >= upper) ? above_color : below_color
269
278
  @draw.stroke('transparent')
270
279
  @draw.fill(color)
271
- @draw.rectangle( i, @canvas.rows,
272
- i + step - 2, @canvas.rows - ( (r / @maximum_value) * @canvas.rows) )
280
+ bar_height_from_top = @canvas.rows - ( (r.to_f / max_normalized.to_f) * @canvas.rows)
281
+ @draw.rectangle( i, @canvas.rows, i + step - 2, bar_height_from_top )
273
282
  i += step
274
283
  end
275
284
 
285
+ unless target.nil?
286
+ normalized_target_value = ((target.to_f - @minimum_value)/(@maximum_value - @minimum_value)) * 100.0
287
+ adjusted_target_value = (height - 3 - normalized_target_value/(101.0/(height-4))).to_i
288
+ @draw.stroke(target_color)
289
+ open_ended_polyline([[-5, adjusted_target_value], [width + 5, adjusted_target_value]])
290
+ end
291
+
276
292
  @draw.draw(@canvas)
277
293
  @canvas
278
294
  end
@@ -415,28 +431,27 @@ class Sparklines
415
431
  # :target - A 1px horizontal line will be drawn at this value. Useful for showing an average.
416
432
  #
417
433
  # :target_color - Color of the target line. Defaults to white.
418
-
419
- def smooth
420
434
 
421
- step = @options[:step].to_f
422
- height = @options[:height].to_f
423
- width = ((@norm_data.size - 1) * step).to_f
435
+ def smooth
436
+ step = @options[:step].to_f
437
+ height = @options[:height].to_f
438
+ width = ((@norm_data.size - 1) * step).to_f
424
439
 
425
440
  background_color = @options[:background_color]
426
441
  create_canvas(width, height, background_color)
427
442
 
428
- min_color = @options[:min_color]
429
- max_color = @options[:max_color]
430
- last_color = @options[:last_color]
431
- has_min = @options[:has_min]
432
- has_max = @options[:has_max]
433
- has_last = @options[:has_last]
434
- line_color = @options[:line_color]
435
- has_std_dev = @options[:has_std_dev]
436
- std_dev_color = @options[:std_dev_color]
443
+ min_color = @options[:min_color]
444
+ max_color = @options[:max_color]
445
+ last_color = @options[:last_color]
446
+ has_min = @options[:has_min]
447
+ has_max = @options[:has_max]
448
+ has_last = @options[:has_last]
449
+ line_color = @options[:line_color]
450
+ has_std_dev = @options[:has_std_dev]
451
+ std_dev_color = @options[:std_dev_color]
437
452
 
438
- target = @options.has_key?(:target) ? @options[:target].to_f : nil
439
- target_color = @options[:target_color] || 'white'
453
+ target = @options.has_key?(:target) ? @options[:target].to_f : nil
454
+ target_color = @options[:target_color] || 'white'
440
455
 
441
456
  drawstddevbox(width,height,std_dev_color) if has_std_dev == true
442
457
 
@@ -477,7 +492,7 @@ class Sparklines
477
492
  # No value produces an empty row to indicate a tie.
478
493
  #
479
494
  # * results - an array of integer values between -2 and 2. -2 is exceptional
480
- # down, 1 is regular down, 0 is no value, 1 is up, and 2 is exceptional up.
495
+ # down, -1 is regular down, 0 is no value, 1 is up, and 2 is exceptional up.
481
496
  # * options - a hash that takes parameters
482
497
  #
483
498
  # :height - height of the sparkline
@@ -485,6 +500,8 @@ class Sparklines
485
500
  # :whisker_color - the color of regular whiskers; defaults to black
486
501
  #
487
502
  # :exception_color - the color of exceptional whiskers; defaults to red
503
+ #
504
+ # :step - Spacing for whiskers. Includes the whisker itself. Default 2.
488
505
 
489
506
  def whisker
490
507
 
@@ -539,7 +556,7 @@ class Sparklines
539
556
  # :height - Numeric. Defaults to 15. Should be a multiple of three.
540
557
  #
541
558
  # :width - This graph expands to any specified width. Defaults to 100.
542
- #
559
+ #
543
560
  # :bad - Numeric. A darker shade background will be drawn up to this point.
544
561
  #
545
562
  # :satisfactory - Numeric. A medium background will be drawn up to this point.
@@ -609,8 +626,15 @@ class Sparklines
609
626
  private
610
627
 
611
628
  def normalize_data
612
- @minimum_value = @data.min
629
+ case @options[:type].to_s
630
+ when 'bar'
631
+ @minimum_value = 0.0
632
+ else
633
+ @minimum_value = @data.min
634
+ end
635
+
613
636
  @maximum_value = @data.max
637
+
614
638
  case @options[:type].to_s
615
639
  when 'pie'
616
640
  @norm_data = @data
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sparklines
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
5
- platform: ""
4
+ version: 0.5.1
5
+ platform: ruby
6
6
  authors:
7
7
  - Geoffrey Grosenbach
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-01-20 00:00:00 -08:00
12
+ date: 2008-05-19 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -19,7 +19,7 @@ dependencies:
19
19
  requirements:
20
20
  - - ">="
21
21
  - !ruby/object:Gem::Version
22
- version: 1.4.0
22
+ version: 1.5.1
23
23
  version:
24
24
  description: Tiny graphs.
25
25
  email: boss@topfunky.com
@@ -118,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
118
  requirements: []
119
119
 
120
120
  rubyforge_project: sparklines
121
- rubygems_version: 0.9.5
121
+ rubygems_version: 1.1.1
122
122
  signing_key:
123
123
  specification_version: 2
124
124
  summary: Tiny graphs.