topfunky-sparklines 0.5.1

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 (65) hide show
  1. data/CHANGELOG +74 -0
  2. data/MIT-LICENSE +21 -0
  3. data/Manifest.txt +64 -0
  4. data/README.txt +20 -0
  5. data/Rakefile +22 -0
  6. data/lib/sparklines.rb +785 -0
  7. data/lib/sparklines_helper.rb +28 -0
  8. data/samples/area-high.png +0 -0
  9. data/samples/area.png +0 -0
  10. data/samples/discrete.png +0 -0
  11. data/samples/pie-large.png +0 -0
  12. data/samples/pie.png +0 -0
  13. data/samples/pie0.png +0 -0
  14. data/samples/pie1.png +0 -0
  15. data/samples/pie100.png +0 -0
  16. data/samples/pie45.png +0 -0
  17. data/samples/pie95.png +0 -0
  18. data/samples/pie99.png +0 -0
  19. data/samples/smooth-colored.png +0 -0
  20. data/samples/smooth.png +0 -0
  21. data/test/expected/area.png +0 -0
  22. data/test/expected/area_high.png +0 -0
  23. data/test/expected/area_min_max.png +0 -0
  24. data/test/expected/bar.png +0 -0
  25. data/test/expected/bar_extreme_values.png +0 -0
  26. data/test/expected/bar_string.png.png +0 -0
  27. data/test/expected/bar_tall.png +0 -0
  28. data/test/expected/bar_wide.png +0 -0
  29. data/test/expected/bullet_basic.png +0 -0
  30. data/test/expected/bullet_colorful.png +0 -0
  31. data/test/expected/bullet_full_featured.png +0 -0
  32. data/test/expected/bullet_tall.png +0 -0
  33. data/test/expected/bullet_wide.png +0 -0
  34. data/test/expected/discrete.png +0 -0
  35. data/test/expected/discrete_wide.png +0 -0
  36. data/test/expected/error.png +0 -0
  37. data/test/expected/labeled_area.png +0 -0
  38. data/test/expected/labeled_bar.png +0 -0
  39. data/test/expected/labeled_discrete.png +0 -0
  40. data/test/expected/labeled_pie.png +0 -0
  41. data/test/expected/labeled_smooth.png +0 -0
  42. data/test/expected/labeled_whisker_decimals.png +0 -0
  43. data/test/expected/pie.png +0 -0
  44. data/test/expected/pie0.png +0 -0
  45. data/test/expected/pie1.png +0 -0
  46. data/test/expected/pie100.png +0 -0
  47. data/test/expected/pie45.png +0 -0
  48. data/test/expected/pie95.png +0 -0
  49. data/test/expected/pie99.png +0 -0
  50. data/test/expected/pie_flat.png +0 -0
  51. data/test/expected/pie_large.png +0 -0
  52. data/test/expected/smooth.png +0 -0
  53. data/test/expected/smooth_colored.png +0 -0
  54. data/test/expected/smooth_similar_nonzero_values.png +0 -0
  55. data/test/expected/smooth_underneath_color.png +0 -0
  56. data/test/expected/smooth_with_target.png +0 -0
  57. data/test/expected/standard_deviation.png +0 -0
  58. data/test/expected/standard_deviation_short.png +0 -0
  59. data/test/expected/standard_deviation_tall.png +0 -0
  60. data/test/expected/whisker.png +0 -0
  61. data/test/expected/whisker_junk.png +0 -0
  62. data/test/expected/whisker_non_exceptional.png +0 -0
  63. data/test/expected/whisker_with_step.png +0 -0
  64. data/test/test_all.rb +302 -0
  65. metadata +126 -0
@@ -0,0 +1,28 @@
1
+ require 'base64'
2
+ # Provides a tag for embedding sparklines graphs into your Rails app.
3
+ #
4
+ module SparklinesHelper
5
+
6
+ # Call with an array of data and a hash of params for the Sparklines module.
7
+ #
8
+ # sparkline_tag [42, 37, 43, 182], :type => 'bar', :line_color => 'black'
9
+ #
10
+ # You can also pass :class => 'some_css_class' ('sparkline' by default).
11
+ def sparkline_tag(results=[], options={})
12
+ url = { :controller => 'sparklines' }
13
+ url[:results] = results.join(',') unless results.nil?
14
+ options = url.merge(options)
15
+ attributes = %(class="#{options[:class] || 'sparkline'}" alt="Sparkline Graph" )
16
+ attributes << %(title="#{options[:title]}" ) if options[:title]
17
+
18
+ # prefer to use a "data" URL scheme as described in {RFC 2397}[http://www.ietf.org/rfc/rfc2397.txt]
19
+ # data_url = "data:image/png;base64,#{Base64.encode64(Sparklines.plot(results,options))}"
20
+ # tag = %(<img src="#{data_url}" #{attributes}/>)
21
+
22
+ # # respect some limits noted in RFC 2397 since the data: url is supposed to be 'short'
23
+ # data_url.length <= 1024 && tag.length <= 2100 ? tag :
24
+ %(<img src="#{ url_for options }" #{attributes}/>)
25
+
26
+ end
27
+
28
+ end
Binary file
data/samples/area.png ADDED
Binary file
Binary file
Binary file
data/samples/pie.png ADDED
Binary file
data/samples/pie0.png ADDED
Binary file
data/samples/pie1.png ADDED
Binary file
Binary file
data/samples/pie45.png ADDED
Binary file
data/samples/pie95.png ADDED
Binary file
data/samples/pie99.png ADDED
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
data/test/test_all.rb ADDED
@@ -0,0 +1,302 @@
1
+ #!/usr/bin/ruby
2
+
3
+ require 'test/unit'
4
+ require 'lib/sparklines'
5
+ require 'fileutils'
6
+ require 'tidy_table'
7
+ require 'dust'
8
+
9
+ class SparklinesTest < Test::Unit::TestCase
10
+
11
+ def setup
12
+ @output_dir = "test/actual"
13
+ FileUtils.mkdir_p(@output_dir)
14
+
15
+ @data = %w( 1 5 15 20 30 50 57 58 55 48
16
+ 44 43 42 42 46 48 49 53 55 59
17
+ 60 65 75 90 105 106 107 110 115 120
18
+ 115 120 130 140 150 160 170 100 100 10).map {|i| i.to_f}
19
+ end
20
+
21
+ test "basic bullet" do
22
+ Sparklines.plot_to_file("#{@output_dir}/bullet_basic.png", 85, {
23
+ :type => "bullet",
24
+ :target => 80,
25
+ :good => 100,
26
+ :height => 15
27
+ })
28
+ end
29
+
30
+ test "full-featured bullet" do
31
+ Sparklines.plot_to_file("#{@output_dir}/bullet_full_featured.png", 85, {
32
+ :type => "bullet",
33
+ :target => 90,
34
+ :bad => 60,
35
+ :satisfactory => 80,
36
+ :good => 100,
37
+ :height => 15
38
+ })
39
+ end
40
+
41
+ test "colorful bullet" do
42
+ Sparklines.plot_to_file("#{@output_dir}/bullet_colorful.png", 85, {
43
+ :type => "bullet",
44
+ :target => 90,
45
+ :bad => 60,
46
+ :satisfactory => 80,
47
+ :good => 100,
48
+ :height => 15,
49
+ :bad_color => '#c3e3bf',
50
+ :satisfactory_color => '#96cf90',
51
+ :good_color => "#6ab162"
52
+ })
53
+ end
54
+
55
+ test "tall bullet" do
56
+ Sparklines.plot_to_file("#{@output_dir}/bullet_tall.png", 85, {
57
+ :type => "bullet",
58
+ :target => 90,
59
+ :bad => 60,
60
+ :satisfactory => 80,
61
+ :good => 100,
62
+ :height => 30
63
+ })
64
+ end
65
+
66
+ test "wide bullet" do
67
+ Sparklines.plot_to_file("#{@output_dir}/bullet_wide.png", 85, {
68
+ :type => "bullet",
69
+ :target => 90,
70
+ :bad => 60,
71
+ :satisfactory => 80,
72
+ :good => 100,
73
+ :height => 15,
74
+ :width => 200
75
+ })
76
+ end
77
+
78
+ test "smooth with target" do
79
+ quick_graph("smooth_with_target", {
80
+ :type => "smooth",
81
+ :target => 50,
82
+ :target_color => '#999999',
83
+ :line_color => "#6699cc",
84
+ :underneath_color => "#ebf3f6"
85
+ })
86
+ end
87
+
88
+ test "whisker with step" do
89
+ quick_graph("whisker_with_step", {
90
+ :type => "whisker",
91
+ :step => 5
92
+ })
93
+ end
94
+
95
+ def test_each_graph
96
+ %w{pie area discrete smooth bar}.each do |type|
97
+ quick_graph("#{type}", :type => type)
98
+ end
99
+ end
100
+
101
+ def test_each_graph_with_label
102
+ %w{pie area discrete smooth bar}.each do |type|
103
+ quick_graph("labeled_#{type}", :type => type, :label => 'Glucose')
104
+ end
105
+ end
106
+
107
+ def test_whisker_decimals
108
+ @data = (1..200).map {|n| n.to_f/100 }
109
+ quick_graph("labeled_whisker_decimals", {
110
+ :height => 30,
111
+ :type => 'smooth',
112
+ :label => 'png'
113
+ })
114
+ end
115
+
116
+ def test_whisker_random
117
+ # Need data ranging from -2 to +2
118
+ @data = (1..40).map { |i| rand(3) * (rand(2) == 1 ? -1 : 1) }
119
+ quick_graph("whisker", :type => 'whisker')
120
+ end
121
+
122
+ def test_whisker_non_exceptional
123
+ @data = [1,1,1,1,1,1,1,1,1,1,1,1,1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1]
124
+ quick_graph("whisker_non_exceptional", :type => 'whisker')
125
+ end
126
+
127
+ ##
128
+ # Send random values in the range (-9..9)
129
+
130
+ def test_whisker_junk
131
+ @data = (1..40).map { |i| rand(10) * (rand(2) == 1 ? -1 : 1) }
132
+ quick_graph("whisker_junk", :type => 'whisker')
133
+ end
134
+
135
+ def test_pie
136
+ # Test extremes which previously did not work right
137
+ [0, 1, 45, 95, 99, 100].each do |value|
138
+ Sparklines.plot_to_file("#{@output_dir}/pie#{value}.png",
139
+ value, {
140
+ :type => 'pie',
141
+ :diameter => 128
142
+ })
143
+ end
144
+ Sparklines.plot_to_file("#{@output_dir}/pie_flat.png",
145
+ [60],
146
+ {
147
+ :type => 'pie'
148
+ })
149
+ end
150
+
151
+ def test_special_conditions
152
+ tests = { 'smooth_colored' =>
153
+ {
154
+ :type => 'smooth',
155
+ :line_color => 'purple'
156
+ },
157
+ 'pie_large' => {
158
+ :type => 'pie',
159
+ :diameter => 200
160
+ },
161
+ 'area_high' => {
162
+ :type => 'area',
163
+ :upper => 80,
164
+ :step => 4,
165
+ :height => 20
166
+ },
167
+ 'discrete_wide' => {
168
+ :type => 'discrete',
169
+ :step => 8
170
+ },
171
+ 'bar_wide' => {
172
+ :type => 'bar',
173
+ :step => 8
174
+ },
175
+ 'bar_tall' => {
176
+ :type => 'bar',
177
+ :below_color => 'blue',
178
+ :above_color => 'red',
179
+ :upper => 90,
180
+ :height => 50,
181
+ :step => 8
182
+ }
183
+ }
184
+ tests.each do |name, options|
185
+ quick_graph(name, options)
186
+ end
187
+ end
188
+
189
+ def test_bar_extreme_values
190
+ Sparklines.plot_to_file("#{@output_dir}/bar_extreme_values.png",
191
+ [0,1,100,2,99,3,98,4,97,5,96,6,95,7,94,8,93,9,92,10,91], {
192
+ :type => 'bar',
193
+ :below_color => 'blue',
194
+ :above_color => 'red',
195
+ :upper => 90,
196
+ :step => 4
197
+ })
198
+ end
199
+
200
+ def test_string_args
201
+ quick_graph("bar_string.png", {
202
+ 'type' => 'bar',
203
+ 'below_color' => 'blue',
204
+ 'above_color' => 'red',
205
+ 'upper' => 50,
206
+ 'height' => 50,
207
+ 'step' => 8
208
+ })
209
+ end
210
+
211
+ def test_area_min_max
212
+ quick_graph("area_min_max", {
213
+ :has_min => true,
214
+ :has_max => true,
215
+ :has_first => true,
216
+ :has_last => true
217
+ })
218
+ end
219
+
220
+ def test_smooth_underneath_color
221
+ quick_graph("smooth_underneath_color", {
222
+ :type => 'smooth',
223
+ :line_color => "#6699cc",
224
+ :underneath_color => "#ebf3f6"
225
+ })
226
+ end
227
+
228
+ def test_close_values
229
+ Sparklines.plot_to_file("#{@output_dir}/smooth_similar_nonzero_values.png", [100, 90, 95, 99, 80, 90], {
230
+ :type => 'smooth',
231
+ :line_color => "#6699cc",
232
+ :underneath_color => "#ebf3f6"
233
+ })
234
+ end
235
+
236
+ def test_no_type
237
+ Sparklines.plot_to_file("#{@output_dir}/error.png", 0, :type => 'nonexistent')
238
+ end
239
+
240
+ def test_standard_deviation
241
+ quick_graph('standard_deviation', {
242
+ :type => 'smooth',
243
+ :height => 100,
244
+ :line_color => '#666',
245
+ :has_std_dev => true,
246
+ :std_dev_color => '#cccccc'
247
+ })
248
+ end
249
+
250
+ def test_standard_deviation_tall
251
+ quick_graph('standard_deviation_tall', {
252
+ :type => 'smooth',
253
+ :height => 300,
254
+ :line_color => '#666',
255
+ :has_std_dev => true,
256
+ :std_dev_color => '#cccccc'
257
+ })
258
+ end
259
+
260
+ def test_standard_deviation_short
261
+ quick_graph('standard_deviation_short', {
262
+ :type => 'smooth',
263
+ :height => 20,
264
+ :line_color => '#666',
265
+ :has_std_dev => true,
266
+ :std_dev_color => '#cccccc'
267
+ })
268
+ end
269
+
270
+ private
271
+
272
+ def quick_graph(name, options)
273
+ Sparklines.plot_to_file("#{@output_dir}/#{name}.png", @data, options)
274
+ end
275
+
276
+ end
277
+
278
+ # HACK Make reference HTML file for viewing output
279
+ END {
280
+ def image_tag(image_path)
281
+ %(<img src="#{image_path}" />)
282
+ end
283
+
284
+ reference_files = Dir['test/expected/*']
285
+ output = TidyTable.new(reference_files).to_html(%w(Expected Actual)) do |record|
286
+ [image_tag("../../" + record), image_tag("../../" + record.gsub('expected', 'actual'))]
287
+ end
288
+ FileUtils.mkdir_p("test/actual")
289
+ File.open("test/actual/result.html", "w") do |f|
290
+ f.write <<-EOL
291
+ <style>
292
+ .first_column {
293
+ text-align: right;
294
+ }
295
+ .last_column {
296
+ text-align: left;
297
+ }
298
+ </style>
299
+ EOL
300
+ f.write output
301
+ end
302
+ }
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: topfunky-sparklines
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.1
5
+ platform: ruby
6
+ authors:
7
+ - Geoffrey Grosenbach
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-05-19 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.5.1
23
+ version:
24
+ description: Tiny graphs.
25
+ email: boss@topfunky.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - Manifest.txt
32
+ - README.txt
33
+ files:
34
+ - CHANGELOG
35
+ - MIT-LICENSE
36
+ - Manifest.txt
37
+ - README.txt
38
+ - Rakefile
39
+ - lib/sparklines.rb
40
+ - lib/sparklines_helper.rb
41
+ - samples/area-high.png
42
+ - samples/area.png
43
+ - samples/discrete.png
44
+ - samples/pie-large.png
45
+ - samples/pie.png
46
+ - samples/pie0.png
47
+ - samples/pie1.png
48
+ - samples/pie100.png
49
+ - samples/pie45.png
50
+ - samples/pie95.png
51
+ - samples/pie99.png
52
+ - samples/smooth-colored.png
53
+ - samples/smooth.png
54
+ - test/expected/area.png
55
+ - test/expected/area_high.png
56
+ - test/expected/area_min_max.png
57
+ - test/expected/bar.png
58
+ - test/expected/bar_extreme_values.png
59
+ - test/expected/bar_string.png.png
60
+ - test/expected/bar_tall.png
61
+ - test/expected/bar_wide.png
62
+ - test/expected/bullet_basic.png
63
+ - test/expected/bullet_colorful.png
64
+ - test/expected/bullet_full_featured.png
65
+ - test/expected/bullet_tall.png
66
+ - test/expected/bullet_wide.png
67
+ - test/expected/discrete.png
68
+ - test/expected/discrete_wide.png
69
+ - test/expected/error.png
70
+ - test/expected/labeled_area.png
71
+ - test/expected/labeled_bar.png
72
+ - test/expected/labeled_discrete.png
73
+ - test/expected/labeled_pie.png
74
+ - test/expected/labeled_smooth.png
75
+ - test/expected/labeled_whisker_decimals.png
76
+ - test/expected/pie.png
77
+ - test/expected/pie0.png
78
+ - test/expected/pie1.png
79
+ - test/expected/pie100.png
80
+ - test/expected/pie45.png
81
+ - test/expected/pie95.png
82
+ - test/expected/pie99.png
83
+ - test/expected/pie_flat.png
84
+ - test/expected/pie_large.png
85
+ - test/expected/smooth.png
86
+ - test/expected/smooth_colored.png
87
+ - test/expected/smooth_similar_nonzero_values.png
88
+ - test/expected/smooth_underneath_color.png
89
+ - test/expected/smooth_with_target.png
90
+ - test/expected/standard_deviation.png
91
+ - test/expected/standard_deviation_short.png
92
+ - test/expected/standard_deviation_tall.png
93
+ - test/expected/whisker.png
94
+ - test/expected/whisker_junk.png
95
+ - test/expected/whisker_non_exceptional.png
96
+ - test/expected/whisker_with_step.png
97
+ - test/test_all.rb
98
+ has_rdoc: true
99
+ homepage: http://nubyonrails.com/pages/sparklines
100
+ post_install_message:
101
+ rdoc_options:
102
+ - --main
103
+ - README.txt
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: "0"
111
+ version:
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: "0"
117
+ version:
118
+ requirements: []
119
+
120
+ rubyforge_project: sparklines
121
+ rubygems_version: 1.0.1
122
+ signing_key:
123
+ specification_version: 2
124
+ summary: Tiny graphs.
125
+ test_files:
126
+ - test/test_all.rb