sparklines 0.4.8 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,11 @@
1
+ == 0.5.0
2
+
3
+ * Documentation for bullet graph.
4
+ * More bullet options for good_color, satisfactory_color, and bad_color.
5
+ * Smooth options for target line value and target_color.
6
+ * Sparklinks.plot_to_image returns a Magick::Image object for further manipulation.
7
+ * Step option for wider whisker.
8
+
1
9
  == 0.4.8
2
10
 
3
11
  * Added bullet graph. See http://en.wikipedia.org/wiki/Bullet_graph or Stephen Few's book _Information Dashboard Design_.
data/Manifest.txt CHANGED
@@ -1,7 +1,7 @@
1
1
  CHANGELOG
2
2
  MIT-LICENSE
3
3
  Manifest.txt
4
- README
4
+ README.txt
5
5
  Rakefile
6
6
  lib/sparklines.rb
7
7
  lib/sparklines_helper.rb
@@ -27,6 +27,7 @@ test/expected/bar_string.png.png
27
27
  test/expected/bar_tall.png
28
28
  test/expected/bar_wide.png
29
29
  test/expected/bullet_basic.png
30
+ test/expected/bullet_colorful.png
30
31
  test/expected/bullet_full_featured.png
31
32
  test/expected/bullet_tall.png
32
33
  test/expected/bullet_wide.png
@@ -52,10 +53,12 @@ test/expected/smooth.png
52
53
  test/expected/smooth_colored.png
53
54
  test/expected/smooth_similar_nonzero_values.png
54
55
  test/expected/smooth_underneath_color.png
56
+ test/expected/smooth_with_target.png
55
57
  test/expected/standard_deviation.png
56
58
  test/expected/standard_deviation_short.png
57
59
  test/expected/standard_deviation_tall.png
58
60
  test/expected/whisker.png
59
61
  test/expected/whisker_junk.png
60
62
  test/expected/whisker_non_exceptional.png
63
+ test/expected/whisker_with_step.png
61
64
  test/test_all.rb
@@ -18,5 +18,3 @@ http://nubyonrails.com/pages/sparklines
18
18
 
19
19
  Daniel Nugent
20
20
  nugend@gmail.com
21
-
22
-
data/lib/sparklines.rb CHANGED
@@ -55,6 +55,7 @@ Graph types:
55
55
  pie
56
56
  smooth
57
57
  bar
58
+ bullet
58
59
  whisker
59
60
 
60
61
  General Defaults:
@@ -74,17 +75,17 @@ Licensed under the MIT license.
74
75
  =end
75
76
  class Sparklines
76
77
 
77
- VERSION = '0.4.8'
78
+ VERSION = '0.5.0'
78
79
 
79
80
  @@label_margin = 5.0
80
81
  @@pointsize = 10.0
81
82
 
82
83
  class << self
83
84
 
84
- # Does the actual plotting of the graph.
85
- # Calls the appropriate subclass based on the :type argument.
86
- # Defaults to 'smooth'
87
- def plot(data=[], options={})
85
+ ##
86
+ # Plots a sparkline and returns a Magic::Image object.
87
+
88
+ def plot_to_image(data=[], options={})
88
89
  defaults = {
89
90
  :type => 'smooth',
90
91
  :height => 14,
@@ -128,7 +129,20 @@ class Sparklines
128
129
  end
129
130
  end
130
131
 
131
- # Writes a graph to disk with the specified filename, or "sparklines.png"
132
+ ##
133
+ # Does the actual plotting of the graph.
134
+ # Calls the appropriate subclass based on the :type argument.
135
+ # Defaults to 'smooth'.
136
+ #
137
+ # Returns a blob.
138
+
139
+ def plot(data=[], options={})
140
+ plot_to_image(data, options).to_blob
141
+ end
142
+
143
+ ##
144
+ # Writes a graph to disk with the specified filename, or "sparklines.png".
145
+
132
146
  def plot_to_file(filename="sparklines.png", data=[], options={})
133
147
  File.open( filename, 'wb' ) do |png|
134
148
  png << self.plot( data, options)
@@ -232,7 +246,7 @@ class Sparklines
232
246
  drawbox(coords[-2], 1, last_color) if has_last == true
233
247
 
234
248
  @draw.draw(@canvas)
235
- @canvas.to_blob
249
+ @canvas
236
250
  end
237
251
 
238
252
  ##
@@ -260,7 +274,7 @@ class Sparklines
260
274
  end
261
275
 
262
276
  @draw.draw(@canvas)
263
- @canvas.to_blob
277
+ @canvas
264
278
  end
265
279
 
266
280
 
@@ -302,7 +316,7 @@ class Sparklines
302
316
  end
303
317
 
304
318
  @draw.draw(@canvas)
305
- @canvas.to_blob
319
+ @canvas
306
320
  end
307
321
 
308
322
 
@@ -335,12 +349,12 @@ class Sparklines
335
349
  if percent == 0
336
350
  # For 0% return blank
337
351
  @draw.draw(@canvas)
338
- return @canvas.to_blob
352
+ return @canvas
339
353
  elsif percent == 100
340
354
  # For 100% just draw a full circle
341
355
  @draw.ellipse(r + 2, r + 2, r , r , 0, 360)
342
356
  @draw.draw(@canvas)
343
- return @canvas.to_blob
357
+ return @canvas
344
358
  end
345
359
 
346
360
  # Okay, this part is as confusing as hell, so pay attention:
@@ -370,11 +384,11 @@ class Sparklines
370
384
  @draw.path(path)
371
385
 
372
386
  @draw.draw(@canvas)
373
- @canvas.to_blob
387
+ @canvas
374
388
  end
375
389
 
376
390
  ##
377
- # Creates a smooth sparkline.
391
+ # Creates a smooth line graph sparkline.
378
392
  #
379
393
  # :step - An integer that determines the distance between each point on the sparkline. Defaults to 2.
380
394
  #
@@ -397,6 +411,11 @@ class Sparklines
397
411
  # :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
398
412
  #
399
413
  # :underneath_color - A string or color code representing the color that will be used to fill in the area underneath the line. Optional.
414
+ #
415
+ # :target - A 1px horizontal line will be drawn at this value. Useful for showing an average.
416
+ #
417
+ # :target_color - Color of the target line. Defaults to white.
418
+
400
419
  def smooth
401
420
 
402
421
  step = @options[:step].to_f
@@ -416,6 +435,9 @@ class Sparklines
416
435
  has_std_dev = @options[:has_std_dev]
417
436
  std_dev_color = @options[:std_dev_color]
418
437
 
438
+ target = @options.has_key?(:target) ? @options[:target].to_f : nil
439
+ target_color = @options[:target_color] || 'white'
440
+
419
441
  drawstddevbox(width,height,std_dev_color) if has_std_dev == true
420
442
 
421
443
  @draw.stroke(line_color)
@@ -432,12 +454,19 @@ class Sparklines
432
454
  open_ended_polyline(coords)
433
455
  end
434
456
 
457
+ unless target.nil?
458
+ normalized_target_value = ((target.to_f - @minimum_value)/(@maximum_value - @minimum_value)) * 100.0
459
+ adjusted_target_value = (height - 3 - normalized_target_value/(101.0/(height-4))).to_i
460
+ @draw.stroke(target_color)
461
+ open_ended_polyline([[-5, adjusted_target_value], [width + 5, adjusted_target_value]])
462
+ end
463
+
435
464
  drawbox(coords[@norm_data.index(@norm_data.min)], 2, min_color) if has_min == true
436
465
  drawbox(coords[@norm_data.index(@norm_data.max)], 2, max_color) if has_max == true
437
466
  drawbox(coords[-1], 2, last_color) if has_last == true
438
467
 
439
468
  @draw.draw(@canvas)
440
- @canvas.to_blob
469
+ @canvas
441
470
  end
442
471
 
443
472
  ##
@@ -459,11 +488,11 @@ class Sparklines
459
488
 
460
489
  def whisker
461
490
 
462
- # step = @options[:step].to_f
491
+ step = @options[:step].to_i
463
492
  height = @options[:height].to_f
464
493
  background_color = @options[:background_color]
465
494
 
466
- create_canvas(@data.size * 2 - 1, height, background_color)
495
+ create_canvas(@data.size * step - 1, height, background_color)
467
496
 
468
497
  whisker_color = @options[:whisker_color] || 'black'
469
498
  exception_color = @options[:exception_color] || 'red'
@@ -491,27 +520,55 @@ class Sparklines
491
520
 
492
521
  @draw.stroke( color )
493
522
  @draw.line( i, y_mid_point, i, y_end_point )
494
- i += 2
523
+ i += step
495
524
  end
496
525
 
497
526
  @draw.draw(@canvas)
498
- @canvas.to_blob
527
+ @canvas
499
528
  end
500
529
 
530
+ ##
531
+ # A bullet graph, a la Stephen Few in "Information Dashboard Design."
532
+ #
533
+ # * data - A single value for the thermometer part of the bullet.
534
+ # Represents the current value.
535
+ # * options - a hash
536
+ #
537
+ # :good - Numeric. Maximum value that will be shown on the graph. Required.
538
+ #
539
+ # :height - Numeric. Defaults to 15. Should be a multiple of three.
540
+ #
541
+ # :width - This graph expands to any specified width. Defaults to 100.
542
+ #
543
+ # :bad - Numeric. A darker shade background will be drawn up to this point.
544
+ #
545
+ # :satisfactory - Numeric. A medium background will be drawn up to this point.
546
+ #
547
+ # :target - Numeric value. A thin vertical bar will be drawn.
548
+ #
549
+ # :good_color - Color for the rightmost section of the bullet.
550
+ #
551
+ # :satisfactory_color - Color for the middle background of the bullet.
552
+ #
553
+ # :bad_color - Color for the lowest, leftmost section.
554
+
501
555
  def bullet
502
556
  height = @options[:height].to_f
503
- @graph_width = @options.has_key?(:width) ? @options[:width].to_f : 100.0
504
- background_color = '#eeeeee' # @options[:background_color]
557
+ @graph_width = @options.has_key?(:width) ? @options[:width].to_f : 100.0
558
+ good_color = @options.has_key?(:good_color) ? @options[:good_color] : '#eeeeee'
559
+ satisfactory_color = @options.has_key?(:satisfactory_color) ? @options[:satisfactory_color] : '#bbbbbb'
560
+ bad_color = @options.has_key?(:bad_color) ? @options[:bad_color] : '#999999'
561
+ bullet_color = @options.has_key?(:bullet_color) ? @options[:bullet_color] : 'black'
505
562
  @thickness = height/3.0
506
563
 
507
- create_canvas(@graph_width, height, background_color)
564
+ create_canvas(@graph_width, height, good_color)
508
565
 
509
566
  @value = @norm_data
510
567
  @good_value = @options[:good].to_f
511
-
568
+
512
569
  @graph_height = @options[:height]
513
570
 
514
- qualitative_range_colors = ['#bbbbbb', '#999999']
571
+ qualitative_range_colors = [satisfactory_color, bad_color]
515
572
  [:satisfactory, :bad].each_with_index do |indicator, index|
516
573
  next unless @options.has_key?(indicator)
517
574
  @draw = @draw.fill(qualitative_range_colors[index])
@@ -520,7 +577,7 @@ class Sparklines
520
577
  end
521
578
 
522
579
  if @options.has_key?(:target)
523
- @draw = @draw.fill 'black'
580
+ @draw = @draw.fill(bullet_color)
524
581
  target_x = @graph_width * (@options[:target].to_f / @good_value)
525
582
  half_thickness = (@thickness / 2.0).to_i
526
583
  bar_width = 1.0
@@ -528,11 +585,11 @@ class Sparklines
528
585
  end
529
586
 
530
587
  # Value
531
- @draw = @draw.fill 'black'
588
+ @draw = @draw.fill(bullet_color)
532
589
  @draw = @draw.rectangle(0, @thickness.to_i, @graph_width * (@data.first.to_f / @good_value), (@thickness * 2.0).to_i)
533
590
 
534
591
  @draw.draw(@canvas)
535
- @canvas.to_blob
592
+ @canvas
536
593
  end
537
594
 
538
595
  ##
@@ -546,7 +603,7 @@ class Sparklines
546
603
  @draw.line(0,15,40,0)
547
604
 
548
605
  @draw.draw(@canvas)
549
- @canvas.to_blob
606
+ @canvas
550
607
  end
551
608
 
552
609
  private
Binary file
Binary file
data/test/test_all.rb CHANGED
@@ -38,6 +38,20 @@ class SparklinesTest < Test::Unit::TestCase
38
38
  })
39
39
  end
40
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
+
41
55
  test "tall bullet" do
42
56
  Sparklines.plot_to_file("#{@output_dir}/bullet_tall.png", 85, {
43
57
  :type => "bullet",
@@ -61,6 +75,23 @@ class SparklinesTest < Test::Unit::TestCase
61
75
  })
62
76
  end
63
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
+
64
95
  def test_each_graph
65
96
  %w{pie area discrete smooth bar}.each do |type|
66
97
  quick_graph("#{type}", :type => type)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sparklines
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.8
4
+ version: 0.5.0
5
5
  platform: ""
6
6
  authors:
7
7
  - Geoffrey Grosenbach
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-01-18 00:00:00 -08:00
12
+ date: 2008-01-20 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -29,11 +29,12 @@ extensions: []
29
29
 
30
30
  extra_rdoc_files:
31
31
  - Manifest.txt
32
+ - README.txt
32
33
  files:
33
34
  - CHANGELOG
34
35
  - MIT-LICENSE
35
36
  - Manifest.txt
36
- - README
37
+ - README.txt
37
38
  - Rakefile
38
39
  - lib/sparklines.rb
39
40
  - lib/sparklines_helper.rb
@@ -59,6 +60,7 @@ files:
59
60
  - test/expected/bar_tall.png
60
61
  - test/expected/bar_wide.png
61
62
  - test/expected/bullet_basic.png
63
+ - test/expected/bullet_colorful.png
62
64
  - test/expected/bullet_full_featured.png
63
65
  - test/expected/bullet_tall.png
64
66
  - test/expected/bullet_wide.png
@@ -84,12 +86,14 @@ files:
84
86
  - test/expected/smooth_colored.png
85
87
  - test/expected/smooth_similar_nonzero_values.png
86
88
  - test/expected/smooth_underneath_color.png
89
+ - test/expected/smooth_with_target.png
87
90
  - test/expected/standard_deviation.png
88
91
  - test/expected/standard_deviation_short.png
89
92
  - test/expected/standard_deviation_tall.png
90
93
  - test/expected/whisker.png
91
94
  - test/expected/whisker_junk.png
92
95
  - test/expected/whisker_non_exceptional.png
96
+ - test/expected/whisker_with_step.png
93
97
  - test/test_all.rb
94
98
  has_rdoc: true
95
99
  homepage: http://nubyonrails.com/pages/sparklines