sparklines 0.4.5 → 0.4.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. data/CHANGELOG +4 -0
  2. data/lib/sparklines.rb +27 -17
  3. data/test/test_all.rb +95 -84
  4. metadata +3 -3
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ == 0.4.6
2
+
3
+ * Added :underneath_color option to smooth sparkline. [Cory Forsyth]
4
+
1
5
  == 0.4.5
2
6
 
3
7
  * Several fixes by Rob Biedenharn
data/lib/sparklines.rb CHANGED
@@ -74,7 +74,7 @@ Licensed under the MIT license.
74
74
  =end
75
75
  class Sparklines
76
76
 
77
- VERSION = '0.4.5'
77
+ VERSION = '0.4.6'
78
78
 
79
79
  @@label_margin = 5.0
80
80
  @@pointsize = 10.0
@@ -102,12 +102,12 @@ class Sparklines
102
102
  :max_color => 'green',
103
103
  :last_color => 'red',
104
104
  :std_dev_color => '#efefef',
105
-
105
+
106
106
  :has_min => false,
107
107
  :has_max => false,
108
108
  :has_last => false,
109
109
  :has_std_dev => false,
110
-
110
+
111
111
  :label => nil
112
112
  }
113
113
 
@@ -284,7 +284,7 @@ class Sparklines
284
284
 
285
285
  width = @norm_data.size * step - 1
286
286
 
287
- create_canvas(width, height, background_color)
287
+ create_canvas(@norm_data.size * step - 1, height, background_color)
288
288
 
289
289
  below_color = @options[:below_color]
290
290
  above_color = @options[:above_color]
@@ -373,7 +373,6 @@ class Sparklines
373
373
  @canvas.to_blob
374
374
  end
375
375
 
376
-
377
376
  ##
378
377
  # Creates a smooth sparkline.
379
378
  #
@@ -396,15 +395,15 @@ class Sparklines
396
395
  # :last_color - A string or color code representing the color that the dot drawn at the last value will be displayed as. Defaults to red.
397
396
  #
398
397
  # :std_dev_color - A string or color code representing the color that the standard deviation bar behind the smooth graph will be displayed as. Defaults to #efefef
399
-
398
+ #
399
+ # :underneath_color - A string or color code representing the color that will be used to fill in the area underneath the line. Optional.
400
400
  def smooth
401
401
 
402
402
  step = @options[:step].to_f
403
403
  height = @options[:height].to_f
404
+ width = ((@norm_data.size - 1) * step).to_f
405
+
404
406
  background_color = @options[:background_color]
405
-
406
- width = (@norm_data.size - 1) * step + 4
407
-
408
407
  create_canvas(width, height, background_color)
409
408
 
410
409
  min_color = @options[:min_color]
@@ -423,23 +422,24 @@ class Sparklines
423
422
  coords = []
424
423
  i=0
425
424
  @norm_data.each do |r|
426
- coords.push [ 2 + i, (height - 3 - r/(101.0/(height-4))) ]
425
+ coords.push [ i, (height - 3 - r/(101.0/(height-4))) ]
427
426
  i += step
428
427
  end
429
428
 
430
- open_ended_polyline(coords)
429
+ if @options[:underneath_color]
430
+ closed_polygon(height, width, coords)
431
+ else
432
+ open_ended_polyline(coords)
433
+ end
431
434
 
432
435
  drawbox(coords[@norm_data.index(@norm_data.min)], 2, min_color) if has_min == true
433
-
434
436
  drawbox(coords[@norm_data.index(@norm_data.max)], 2, max_color) if has_max == true
435
-
436
437
  drawbox(coords[-1], 2, last_color) if has_last == true
437
438
 
438
439
  @draw.draw(@canvas)
439
440
  @canvas.to_blob
440
441
  end
441
-
442
-
442
+
443
443
  ##
444
444
  # Creates a whisker sparkline to track on/off type data. There are five states:
445
445
  # on, off, no value, exceptional on, exceptional off. On values create an up
@@ -533,7 +533,18 @@ private
533
533
  @draw.line(arr[i][0], arr[i][1], arr[i+1][0], arr[i+1][1])
534
534
  }
535
535
  end
536
-
536
+
537
+ # Fills in the area under the line (used for a smooth graph)
538
+ def closed_polygon(height, width, coords)
539
+ return if @options[:underneath_color].nil?
540
+ list = []
541
+ list << [0, height]
542
+ list << coords
543
+ list << [width, height]
544
+ @draw.fill( @options[:underneath_color] )
545
+ @draw.polygon( *list.flatten )
546
+ end
547
+
537
548
  ##
538
549
  # Create an image to draw on and a drawable to do the drawing with.
539
550
  #
@@ -646,4 +657,3 @@ private
646
657
 
647
658
 
648
659
  end
649
-
data/test/test_all.rb CHANGED
@@ -9,32 +9,32 @@ class SparklinesTest < Test::Unit::TestCase
9
9
  def setup
10
10
  @output_dir = "test/output"
11
11
  FileUtils.mkdir_p(@output_dir)
12
-
13
- @data = %w( 1 5 15 20 30 50 57 58 55 48
14
- 44 43 42 42 46 48 49 53 55 59
15
- 60 65 75 90 105 106 107 110 115 120
16
- 115 120 130 140 150 160 170 100 100 10).map {|i| i.to_f}
12
+
13
+ @data = %w( 1 5 15 20 30 50 57 58 55 48
14
+ 44 43 42 42 46 48 49 53 55 59
15
+ 60 65 75 90 105 106 107 110 115 120
16
+ 115 120 130 140 150 160 170 100 100 10).map {|i| i.to_f}
17
17
  end
18
18
 
19
19
  def test_each_graph
20
20
  %w{pie area discrete smooth bar}.each do |type|
21
- quick_graph("#{type}", :type => type)
21
+ quick_graph("#{type}", :type => type)
22
22
  end
23
23
  end
24
24
 
25
25
  def test_each_graph_with_label
26
26
  %w{pie area discrete smooth bar}.each do |type|
27
- quick_graph("labeled_#{type}", :type => type, :label => 'Glucose')
27
+ quick_graph("labeled_#{type}", :type => type, :label => 'Glucose')
28
28
  end
29
29
  end
30
30
 
31
31
  def test_whisker_decimals
32
32
  @data = (1..200).map {|n| n.to_f/100 }
33
- quick_graph("labeled_whisker_decimals",
34
- :height => 30,
35
- :type => 'smooth',
36
- :label => 'png'
37
- )
33
+ quick_graph("labeled_whisker_decimals", {
34
+ :height => 30,
35
+ :type => 'smooth',
36
+ :label => 'png'
37
+ })
38
38
  end
39
39
 
40
40
  def test_whisker_random
@@ -50,7 +50,7 @@ class SparklinesTest < Test::Unit::TestCase
50
50
 
51
51
  ##
52
52
  # Send random values in the range (-9..9)
53
-
53
+
54
54
  def test_whisker_junk
55
55
  @data = (1..40).map { |i| rand(10) * (rand(2) == 1 ? -1 : 1) }
56
56
  quick_graph("whisker_junk", :type => 'whisker')
@@ -59,85 +59,96 @@ class SparklinesTest < Test::Unit::TestCase
59
59
  def test_pie
60
60
  # Test extremes which previously did not work right
61
61
  [0, 1, 45, 95, 99, 100].each do |value|
62
- Sparklines.plot_to_file("#{@output_dir}/pie#{value}.png",
63
- value,
64
- :type => 'pie',
65
- :diameter => 128)
62
+ Sparklines.plot_to_file("#{@output_dir}/pie#{value}.png",
63
+ value, {
64
+ :type => 'pie',
65
+ :diameter => 128
66
+ })
66
67
  end
67
- Sparklines.plot_to_file("#{@output_dir}/pie_flat.png",
68
- [60],
69
- :type => 'pie')
68
+ Sparklines.plot_to_file("#{@output_dir}/pie_flat.png",
69
+ [60],
70
+ {
71
+ :type => 'pie'
72
+ })
70
73
  end
71
-
74
+
72
75
  def test_special_conditions
73
- tests = { 'smooth_colored' => {
74
- :type => 'smooth',
75
- :line_color => 'purple'
76
- },
77
- 'pie_large' => {
78
- :type => 'pie',
79
- :diameter => 200
80
- },
81
- 'area_high' => {
82
- :type => 'area',
83
- :upper => 80,
84
- :step => 4,
85
- :height => 20
86
- },
87
- 'discrete_wide' => {
88
- :type => 'discrete',
89
- :step => 8
90
- },
91
- 'bar_wide' => {
92
- :type => 'bar',
93
- :step => 8
94
- },
95
- 'bar_tall' => {
96
- :type => 'bar',
97
- :below_color => 'blue',
98
- :above_color => 'red',
99
- :upper => 90,
100
- :height => 50,
101
- :step => 8
102
- }
103
- }
76
+ tests = { 'smooth_colored' =>
77
+ {
78
+ :type => 'smooth',
79
+ :line_color => 'purple'
80
+ },
81
+ 'pie_large' => {
82
+ :type => 'pie',
83
+ :diameter => 200
84
+ },
85
+ 'area_high' => {
86
+ :type => 'area',
87
+ :upper => 80,
88
+ :step => 4,
89
+ :height => 20
90
+ },
91
+ 'discrete_wide' => {
92
+ :type => 'discrete',
93
+ :step => 8
94
+ },
95
+ 'bar_wide' => {
96
+ :type => 'bar',
97
+ :step => 8
98
+ },
99
+ 'bar_tall' => {
100
+ :type => 'bar',
101
+ :below_color => 'blue',
102
+ :above_color => 'red',
103
+ :upper => 90,
104
+ :height => 50,
105
+ :step => 8
106
+ }
107
+ }
104
108
  tests.each do |name, options|
105
- quick_graph(name, options)
109
+ quick_graph(name, options)
106
110
  end
107
111
  end
108
-
109
- # def test_smooth_graph
110
- #
111
- # end
112
-
112
+
113
113
  def test_bar_extreme_values
114
- Sparklines.plot_to_file("#{@output_dir}/bar_extreme_values.png",
115
- [0,1,100,2,99,3,98,4,97,5,96,6,95,7,94,8,93,9,92,10,91],
116
- :type => 'bar',
117
- :below_color => 'blue',
118
- :above_color => 'red',
119
- :upper => 90,
120
- :step => 4 )
121
- end
122
-
114
+ Sparklines.plot_to_file("#{@output_dir}/bar_extreme_values.png",
115
+ [0,1,100,2,99,3,98,4,97,5,96,6,95,7,94,8,93,9,92,10,91], {
116
+ :type => 'bar',
117
+ :below_color => 'blue',
118
+ :above_color => 'red',
119
+ :upper => 90,
120
+ :step => 4
121
+ })
122
+ end
123
+
123
124
  def test_string_args
124
- quick_graph("bar_string.png",
125
- 'type' => 'bar',
126
- 'below_color' => 'blue',
127
- 'above_color' => 'red',
128
- 'upper' => 50,
129
- 'height' => 50,
130
- 'step' => 8 )
125
+ quick_graph("bar_string.png", {
126
+ 'type' => 'bar',
127
+ 'below_color' => 'blue',
128
+ 'above_color' => 'red',
129
+ 'upper' => 50,
130
+ 'height' => 50,
131
+ 'step' => 8
132
+ })
131
133
  end
132
134
 
133
135
  def test_area_min_max
134
- quick_graph("area_min_max",
135
- :has_min => true,
136
- :has_max => true,
137
- :has_first => true,
138
- :has_last => true)
136
+ quick_graph("area_min_max", {
137
+ :has_min => true,
138
+ :has_max => true,
139
+ :has_first => true,
140
+ :has_last => true
141
+ })
139
142
  end
140
-
143
+
144
+ def test_smooth_underneath_color
145
+ quick_graph("smooth_underneath_color", {
146
+ :type => 'smooth',
147
+ :line_color => "#6699cc",
148
+ :underneath_color => "#ebf3f6"
149
+ })
150
+ end
151
+
141
152
 
142
153
  def test_no_type
143
154
  Sparklines.plot_to_file("#{@output_dir}/error.png", 0, :type => 'nonexistent')
@@ -150,7 +161,7 @@ class SparklinesTest < Test::Unit::TestCase
150
161
  :line_color => '#666',
151
162
  :has_std_dev => true,
152
163
  :std_dev_color => '#cccccc'
153
- })
164
+ })
154
165
  end
155
166
 
156
167
  def test_standard_deviation_tall
@@ -160,7 +171,7 @@ class SparklinesTest < Test::Unit::TestCase
160
171
  :line_color => '#666',
161
172
  :has_std_dev => true,
162
173
  :std_dev_color => '#cccccc'
163
- })
174
+ })
164
175
  end
165
176
 
166
177
  def test_standard_deviation_short
@@ -170,10 +181,10 @@ class SparklinesTest < Test::Unit::TestCase
170
181
  :line_color => '#666',
171
182
  :has_std_dev => true,
172
183
  :std_dev_color => '#cccccc'
173
- })
184
+ })
174
185
  end
175
186
 
176
- private
187
+ private
177
188
 
178
189
  def quick_graph(name, options)
179
190
  Sparklines.plot_to_file("#{@output_dir}/#{name}.png", @data, options)
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: sparklines
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.4.5
7
- date: 2007-06-18 00:00:00 -07:00
6
+ version: 0.4.6
7
+ date: 2007-10-12 00:00:00 -07:00
8
8
  summary: Tiny graphs.
9
9
  require_paths:
10
10
  - lib
@@ -71,5 +71,5 @@ dependencies:
71
71
  requirements:
72
72
  - - ">="
73
73
  - !ruby/object:Gem::Version
74
- version: 1.2.1
74
+ version: 1.3.0
75
75
  version: