sparklines 0.4.1 → 0.4.2
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.
- data/lib/sparklines.rb +121 -60
- data/test/test_all.rb +33 -0
- metadata +8 -8
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.
|
77
|
+
VERSION = '0.4.2'
|
78
78
|
|
79
79
|
@@label_margin = 5.0
|
80
80
|
@@pointsize = 10.0
|
@@ -101,10 +101,12 @@ class Sparklines
|
|
101
101
|
:min_color => 'blue',
|
102
102
|
:max_color => 'green',
|
103
103
|
:last_color => 'red',
|
104
|
+
:std_dev_color => '#efefef',
|
104
105
|
|
105
106
|
:has_min => false,
|
106
107
|
:has_max => false,
|
107
108
|
:has_last => false,
|
109
|
+
:has_std_dev => false,
|
108
110
|
|
109
111
|
:label => nil
|
110
112
|
}
|
@@ -144,37 +146,37 @@ class Sparklines
|
|
144
146
|
##
|
145
147
|
# Creates a continuous area sparkline. Relevant options.
|
146
148
|
#
|
147
|
-
#
|
148
|
-
#
|
149
|
-
#
|
150
|
-
#
|
151
|
-
#
|
152
|
-
#
|
153
|
-
#
|
154
|
-
#
|
155
|
-
#
|
156
|
-
#
|
157
|
-
#
|
158
|
-
#
|
159
|
-
#
|
160
|
-
#
|
161
|
-
#
|
162
|
-
#
|
163
|
-
#
|
164
|
-
#
|
165
|
-
#
|
166
|
-
#
|
167
|
-
#
|
149
|
+
# :step - An integer that determines the distance between each point on the sparkline. Defaults to 2.
|
150
|
+
#
|
151
|
+
# :height - An integer that determines what the height of the sparkline will be. Defaults to 14
|
152
|
+
#
|
153
|
+
# :upper - An integer that determines the threshold for colorization purposes. Any value less than upper will be colored using below_color, anything above and equal to upper will use above_color. Defaults to 50.
|
154
|
+
#
|
155
|
+
# :has_min - Determines whether a dot will be drawn at the lowest value or not. Defaults to false.
|
156
|
+
#
|
157
|
+
# :has_max - Determines whether a dot will be drawn at the highest value or not. Defaults to false.
|
158
|
+
#
|
159
|
+
# :has_last - Determines whether a dot will be drawn at the last value or not. Defaults to false.
|
160
|
+
#
|
161
|
+
# :min_color - A string or color code representing the color that the dot drawn at the smallest value will be displayed as. Defaults to blue.
|
162
|
+
#
|
163
|
+
# :max_color - A string or color code representing the color that the dot drawn at the largest value will be displayed as. Defaults to green.
|
164
|
+
#
|
165
|
+
# :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.
|
166
|
+
#
|
167
|
+
# :above_color - A string or color code representing the color to draw values above or equal the upper value. Defaults to red.
|
168
|
+
#
|
169
|
+
# :below_color - A string or color code representing the color to draw values below the upper value. Defaults to gray.
|
168
170
|
|
169
171
|
def area
|
170
172
|
|
171
|
-
step = @options[:step].
|
172
|
-
height = @options[:height].
|
173
|
+
step = @options[:step].to_f
|
174
|
+
height = @options[:height].to_f
|
173
175
|
background_color = @options[:background_color]
|
174
176
|
|
175
177
|
create_canvas((@norm_data.size - 1) * step + 4, height, background_color)
|
176
178
|
|
177
|
-
upper = @options[:upper].
|
179
|
+
upper = @options[:upper].to_f
|
178
180
|
|
179
181
|
has_min = @options[:has_min]
|
180
182
|
has_max = @options[:has_max]
|
@@ -203,7 +205,7 @@ class Sparklines
|
|
203
205
|
@draw.rectangle(0,0,(@norm_data.size - 1) * step + 4,(height - 3 - upper/(101.0/(height-4))))
|
204
206
|
end
|
205
207
|
@draw.clip_path('top')
|
206
|
-
@draw.polygon
|
208
|
+
@draw.polygon(*coords.flatten)
|
207
209
|
|
208
210
|
# Block off the top half of the image and draw the sparkline
|
209
211
|
@draw.fill(below_color)
|
@@ -211,7 +213,7 @@ class Sparklines
|
|
211
213
|
@draw.rectangle(0,(height - 3 - upper/(101.0/(height-4))),(@norm_data.size - 1) * step + 4,height)
|
212
214
|
end
|
213
215
|
@draw.clip_path('bottom')
|
214
|
-
@draw.polygon
|
216
|
+
@draw.polygon(*coords.flatten)
|
215
217
|
|
216
218
|
# The sparkline looks kinda nasty if either the above_color or below_color gets the center line
|
217
219
|
@draw.fill('black')
|
@@ -237,13 +239,13 @@ class Sparklines
|
|
237
239
|
# A bar graph.
|
238
240
|
|
239
241
|
def bar
|
240
|
-
step = @options[:step].
|
242
|
+
step = @options[:step].to_f
|
241
243
|
height = @options[:height].to_f
|
242
244
|
background_color = @options[:background_color]
|
243
245
|
|
244
246
|
create_canvas(@norm_data.length * step + 2, height, background_color)
|
245
247
|
|
246
|
-
upper = @options[:upper].
|
248
|
+
upper = @options[:upper].to_f
|
247
249
|
below_color = @options[:below_color]
|
248
250
|
above_color = @options[:above_color]
|
249
251
|
|
@@ -265,32 +267,37 @@ class Sparklines
|
|
265
267
|
##
|
266
268
|
# Creates a discretized sparkline
|
267
269
|
#
|
268
|
-
#
|
270
|
+
# :height - An integer that determines what the height of the sparkline will be. Defaults to 14
|
269
271
|
#
|
270
|
-
#
|
272
|
+
# :upper - An integer that determines the threshold for colorization purposes. Any value less than upper will be colored using below_color, anything above and equal to upper will use above_color. Defaults to 50.
|
271
273
|
#
|
272
|
-
#
|
274
|
+
# :above_color - A string or color code representing the color to draw values above or equal the upper value. Defaults to red.
|
273
275
|
#
|
274
|
-
#
|
276
|
+
# :below_color - A string or color code representing the color to draw values below the upper value. Defaults to gray.
|
275
277
|
|
276
278
|
def discrete
|
277
279
|
|
278
|
-
height = @options[:height].
|
279
|
-
upper = @options[:upper].
|
280
|
+
height = @options[:height].to_f
|
281
|
+
upper = @options[:upper].to_f
|
280
282
|
background_color = @options[:background_color]
|
281
|
-
step = @options[:step].
|
283
|
+
step = @options[:step].to_f
|
284
|
+
|
285
|
+
width = @norm_data.size * step - 1
|
282
286
|
|
283
|
-
create_canvas(
|
287
|
+
create_canvas(width, height, background_color)
|
284
288
|
|
285
289
|
below_color = @options[:below_color]
|
286
290
|
above_color = @options[:above_color]
|
291
|
+
std_dev_color = @options[:std_dev_color]
|
292
|
+
|
293
|
+
drawstddevbox(width,height,std_dev_color) if @options[:has_std_dev] == true
|
287
294
|
|
288
295
|
i = 0
|
289
296
|
@norm_data.each do |r|
|
290
297
|
color = (r >= upper) ? above_color : below_color
|
291
298
|
@draw.stroke(color)
|
292
|
-
@draw.line(i, (@canvas.rows - r/(101.0/(height-4))-4).
|
293
|
-
i, (@canvas.rows - r/(101.0/(height-4))).
|
299
|
+
@draw.line(i, (@canvas.rows - r/(101.0/(height-4))-4).to_f,
|
300
|
+
i, (@canvas.rows - r/(101.0/(height-4))).to_f)
|
294
301
|
i += step
|
295
302
|
end
|
296
303
|
|
@@ -302,14 +309,14 @@ class Sparklines
|
|
302
309
|
##
|
303
310
|
# Creates a pie-chart sparkline
|
304
311
|
#
|
305
|
-
#
|
312
|
+
# :diameter - An integer that determines what the size of the sparkline will be. Defaults to 20
|
306
313
|
#
|
307
|
-
#
|
314
|
+
# :share_color - A string or color code representing the color to draw the share of the pie represented by percent. Defaults to red.
|
308
315
|
#
|
309
|
-
#
|
316
|
+
# :remain_color - A string or color code representing the color to draw the pie not taken by the share color. Defaults to lightgrey.
|
310
317
|
|
311
318
|
def pie
|
312
|
-
diameter = @options[:diameter].
|
319
|
+
diameter = @options[:diameter].to_f
|
313
320
|
background_color = @options[:background_color]
|
314
321
|
|
315
322
|
create_canvas(diameter, diameter, background_color)
|
@@ -370,29 +377,35 @@ class Sparklines
|
|
370
377
|
##
|
371
378
|
# Creates a smooth sparkline.
|
372
379
|
#
|
373
|
-
#
|
380
|
+
# :step - An integer that determines the distance between each point on the sparkline. Defaults to 2.
|
381
|
+
#
|
382
|
+
# :height - An integer that determines what the height of the sparkline will be. Defaults to 14
|
374
383
|
#
|
375
|
-
#
|
384
|
+
# :has_min - Determines whether a dot will be drawn at the lowest value or not. Defaults to false.
|
376
385
|
#
|
377
|
-
#
|
386
|
+
# :has_max - Determines whether a dot will be drawn at the highest value or not. Defaults to false.
|
378
387
|
#
|
379
|
-
#
|
388
|
+
# :has_last - Determines whether a dot will be drawn at the last value or not. Defaults to false.
|
380
389
|
#
|
381
|
-
#
|
390
|
+
# :has_std_dev - Determines whether there will be a standard deviation bar behind the smooth graph or not. Defaults to false.
|
382
391
|
#
|
383
|
-
#
|
392
|
+
# :min_color - A string or color code representing the color that the dot drawn at the smallest value will be displayed as. Defaults to blue.
|
384
393
|
#
|
385
|
-
#
|
394
|
+
# :max_color - A string or color code representing the color that the dot drawn at the largest value will be displayed as. Defaults to green.
|
386
395
|
#
|
387
|
-
#
|
396
|
+
# :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
|
+
#
|
398
|
+
# :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
|
388
399
|
|
389
400
|
def smooth
|
390
401
|
|
391
|
-
step = @options[:step].
|
392
|
-
height = @options[:height].
|
402
|
+
step = @options[:step].to_f
|
403
|
+
height = @options[:height].to_f
|
393
404
|
background_color = @options[:background_color]
|
394
405
|
|
395
|
-
|
406
|
+
width = (@norm_data.size - 1) * step + 4
|
407
|
+
|
408
|
+
create_canvas(width, height, background_color)
|
396
409
|
|
397
410
|
min_color = @options[:min_color]
|
398
411
|
max_color = @options[:max_color]
|
@@ -401,6 +414,10 @@ class Sparklines
|
|
401
414
|
has_max = @options[:has_max]
|
402
415
|
has_last = @options[:has_last]
|
403
416
|
line_color = @options[:line_color]
|
417
|
+
has_std_dev = @options[:has_std_dev]
|
418
|
+
std_dev_color = @options[:std_dev_color]
|
419
|
+
|
420
|
+
drawstddevbox(width,height,std_dev_color) if has_std_dev == true
|
404
421
|
|
405
422
|
@draw.stroke(line_color)
|
406
423
|
coords = []
|
@@ -434,16 +451,16 @@ class Sparklines
|
|
434
451
|
# down, 1 is regular down, 0 is no value, 1 is up, and 2 is exceptional up.
|
435
452
|
# * options - a hash that takes parameters
|
436
453
|
#
|
437
|
-
#
|
454
|
+
# :height - height of the sparkline
|
438
455
|
#
|
439
|
-
#
|
456
|
+
# :whisker_color - the color of regular whiskers; defaults to black
|
440
457
|
#
|
441
|
-
#
|
458
|
+
# :exception_color - the color of exceptional whiskers; defaults to red
|
442
459
|
|
443
460
|
def whisker
|
444
461
|
|
445
|
-
# step = @options[:step].
|
446
|
-
height = @options[:height].
|
462
|
+
# step = @options[:step].to_f
|
463
|
+
height = @options[:height].to_f
|
447
464
|
background_color = @options[:background_color]
|
448
465
|
|
449
466
|
create_canvas((@data.size - 1) * 2, height, background_color)
|
@@ -505,7 +522,7 @@ private
|
|
505
522
|
end
|
506
523
|
|
507
524
|
##
|
508
|
-
#
|
525
|
+
# :arr - an array of points (represented as two element arrays)
|
509
526
|
|
510
527
|
def open_ended_polyline(arr)
|
511
528
|
0.upto(arr.length - 2) { |i|
|
@@ -521,6 +538,7 @@ private
|
|
521
538
|
def create_canvas(w, h, bkg_col)
|
522
539
|
@draw = Magick::Draw.new
|
523
540
|
@draw.pointsize = @@pointsize # TODO Use height
|
541
|
+
@draw.pointsize = @options[:font_size] if @options.has_key?(:font_size)
|
524
542
|
@canvas = Magick::Image.new(w , h) { self.background_color = bkg_col }
|
525
543
|
|
526
544
|
# Make room for label and last value
|
@@ -544,6 +562,7 @@ private
|
|
544
562
|
else
|
545
563
|
@font = nil
|
546
564
|
end
|
565
|
+
@font = @options[:font] if @options.has_key?(:font)
|
547
566
|
|
548
567
|
@draw.fill = 'black'
|
549
568
|
@draw.font = @font if @font
|
@@ -571,6 +590,17 @@ private
|
|
571
590
|
@draw.rectangle(pt[0]-offset, pt[1]-offset, pt[0]+offset, pt[1]+offset)
|
572
591
|
end
|
573
592
|
|
593
|
+
##
|
594
|
+
# Utility to draw the standard deviation box
|
595
|
+
#
|
596
|
+
def drawstddevbox(width,height,color)
|
597
|
+
mid=@norm_data.inject(0) {|sum,v| sum+=v}/@norm_data.size
|
598
|
+
std_dev = standard_deviation(@norm_data)/100*height
|
599
|
+
@draw.stroke 'transparent'
|
600
|
+
@draw.fill(color)
|
601
|
+
@draw.rectangle(0, mid-std_dev, width, mid+std_dev)
|
602
|
+
end
|
603
|
+
|
574
604
|
def calculate_width(text)
|
575
605
|
@draw.get_type_metrics(@canvas, text.to_s).width
|
576
606
|
end
|
@@ -579,4 +609,35 @@ private
|
|
579
609
|
@draw.get_type_metrics(@canvas, 'X').height
|
580
610
|
end
|
581
611
|
|
612
|
+
##
|
613
|
+
# Calculation helper for standard deviation.
|
614
|
+
#
|
615
|
+
# Thanks to Warren Seen
|
616
|
+
# http://warrenseen.com/blog/2006/03/13/how-to-calculate-standard-deviation/
|
617
|
+
def variance(population)
|
618
|
+
n = 0
|
619
|
+
mean = 0.0
|
620
|
+
s = 0.0
|
621
|
+
population.each { |x|
|
622
|
+
n = n + 1
|
623
|
+
delta = x - mean
|
624
|
+
mean = mean + (delta / n)
|
625
|
+
s = s + delta * (x - mean)
|
626
|
+
}
|
627
|
+
# if you want to calculate std deviation
|
628
|
+
# of a sample change this to "s / (n-1)"
|
629
|
+
return s / n
|
630
|
+
end
|
631
|
+
|
632
|
+
##
|
633
|
+
# Calculate the standard deviation of a population
|
634
|
+
#
|
635
|
+
# accepts: an array, the population
|
636
|
+
# returns: the standard deviation
|
637
|
+
def standard_deviation(population)
|
638
|
+
Math.sqrt(variance(population))
|
639
|
+
end
|
640
|
+
|
641
|
+
|
582
642
|
end
|
643
|
+
|
data/test/test_all.rb
CHANGED
@@ -25,6 +25,15 @@ class SparklinesTest < Test::Unit::TestCase
|
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
+
def test_whisker_decimals
|
29
|
+
@data = (1..200).map {|n| n.to_f/100 }
|
30
|
+
quick_graph("labeled_whisker_decimals",
|
31
|
+
:height => 30,
|
32
|
+
:type => 'smooth',
|
33
|
+
:label => 'png'
|
34
|
+
)
|
35
|
+
end
|
36
|
+
|
28
37
|
def test_whisker_random
|
29
38
|
# Need data ranging from -2 to +2
|
30
39
|
@data = (1..40).map { |i| rand(3) * (rand(2) == 1 ? -1 : 1) }
|
@@ -131,6 +140,30 @@ class SparklinesTest < Test::Unit::TestCase
|
|
131
140
|
Sparklines.plot_to_file("#{@output_dir}/error.png", 0, :type => 'nonexistent')
|
132
141
|
end
|
133
142
|
|
143
|
+
def test_standard_deviation
|
144
|
+
# values = (1..100).map { |v| rand(100) }
|
145
|
+
#
|
146
|
+
# File.open("test.png","w+") do |f|
|
147
|
+
# f.puts Sparklines.plot(
|
148
|
+
# values,
|
149
|
+
# :type => 'smooth',
|
150
|
+
# :height => 100,
|
151
|
+
# :line_color => '#666',
|
152
|
+
# :has_std_dev => true,
|
153
|
+
# :std_dev_color => '#efffef'
|
154
|
+
# )
|
155
|
+
# end
|
156
|
+
|
157
|
+
quick_graph('standard_deviation', {
|
158
|
+
:type => 'smooth',
|
159
|
+
:height => 100,
|
160
|
+
:line_color => '#666',
|
161
|
+
:has_std_dev => true,
|
162
|
+
:std_dev_color => '#cccccc'
|
163
|
+
})
|
164
|
+
|
165
|
+
end
|
166
|
+
|
134
167
|
private
|
135
168
|
|
136
169
|
def quick_graph(name, options)
|
metadata
CHANGED
@@ -3,12 +3,11 @@ 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.
|
7
|
-
date:
|
6
|
+
version: 0.4.2
|
7
|
+
date: 2007-06-15 00:00:00 -07:00
|
8
8
|
summary: Tiny graphs.
|
9
9
|
require_paths:
|
10
10
|
- lib
|
11
|
-
- test
|
12
11
|
email: boss@topfunky.com
|
13
12
|
homepage: http://nubyonrails.com/pages/sparklines
|
14
13
|
rubyforge_project: sparklines
|
@@ -39,10 +38,11 @@ files:
|
|
39
38
|
- test/output
|
40
39
|
test_files:
|
41
40
|
- test/test_all.rb
|
42
|
-
rdoc_options:
|
43
|
-
|
44
|
-
|
45
|
-
|
41
|
+
rdoc_options:
|
42
|
+
- --main
|
43
|
+
- README.txt
|
44
|
+
extra_rdoc_files:
|
45
|
+
- Manifest.txt
|
46
46
|
executables: []
|
47
47
|
|
48
48
|
extensions: []
|
@@ -57,5 +57,5 @@ dependencies:
|
|
57
57
|
requirements:
|
58
58
|
- - ">="
|
59
59
|
- !ruby/object:Gem::Version
|
60
|
-
version: 1.
|
60
|
+
version: 1.2.1
|
61
61
|
version:
|