pfsc-gruff 0.3.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. data/History.txt +117 -0
  2. data/MIT-LICENSE +21 -0
  3. data/Manifest.txt +81 -0
  4. data/README.txt +40 -0
  5. data/Rakefile +55 -0
  6. data/assets/bubble.png +0 -0
  7. data/assets/city_scene/background/0000.png +0 -0
  8. data/assets/city_scene/background/0600.png +0 -0
  9. data/assets/city_scene/background/2000.png +0 -0
  10. data/assets/city_scene/clouds/cloudy.png +0 -0
  11. data/assets/city_scene/clouds/partly_cloudy.png +0 -0
  12. data/assets/city_scene/clouds/stormy.png +0 -0
  13. data/assets/city_scene/grass/default.png +0 -0
  14. data/assets/city_scene/haze/true.png +0 -0
  15. data/assets/city_scene/number_sample/1.png +0 -0
  16. data/assets/city_scene/number_sample/2.png +0 -0
  17. data/assets/city_scene/number_sample/default.png +0 -0
  18. data/assets/city_scene/sky/0000.png +0 -0
  19. data/assets/city_scene/sky/0200.png +0 -0
  20. data/assets/city_scene/sky/0400.png +0 -0
  21. data/assets/city_scene/sky/0600.png +0 -0
  22. data/assets/city_scene/sky/0800.png +0 -0
  23. data/assets/city_scene/sky/1000.png +0 -0
  24. data/assets/city_scene/sky/1200.png +0 -0
  25. data/assets/city_scene/sky/1400.png +0 -0
  26. data/assets/city_scene/sky/1500.png +0 -0
  27. data/assets/city_scene/sky/1700.png +0 -0
  28. data/assets/city_scene/sky/2000.png +0 -0
  29. data/assets/pc306715.jpg +0 -0
  30. data/assets/plastik/blue.png +0 -0
  31. data/assets/plastik/green.png +0 -0
  32. data/assets/plastik/red.png +0 -0
  33. data/init.rb +2 -0
  34. data/lib/gruff.rb +28 -0
  35. data/lib/gruff/accumulator_bar.rb +27 -0
  36. data/lib/gruff/area.rb +58 -0
  37. data/lib/gruff/bar.rb +87 -0
  38. data/lib/gruff/bar_conversion.rb +46 -0
  39. data/lib/gruff/base.rb +1123 -0
  40. data/lib/gruff/bullet.rb +109 -0
  41. data/lib/gruff/deprecated.rb +39 -0
  42. data/lib/gruff/dot.rb +113 -0
  43. data/lib/gruff/line.rb +135 -0
  44. data/lib/gruff/mini/bar.rb +37 -0
  45. data/lib/gruff/mini/legend.rb +109 -0
  46. data/lib/gruff/mini/pie.rb +36 -0
  47. data/lib/gruff/mini/side_bar.rb +35 -0
  48. data/lib/gruff/net.rb +140 -0
  49. data/lib/gruff/photo_bar.rb +100 -0
  50. data/lib/gruff/pie.rb +126 -0
  51. data/lib/gruff/scene.rb +209 -0
  52. data/lib/gruff/side_bar.rb +118 -0
  53. data/lib/gruff/side_stacked_bar.rb +77 -0
  54. data/lib/gruff/spider.rb +130 -0
  55. data/lib/gruff/stacked_area.rb +67 -0
  56. data/lib/gruff/stacked_bar.rb +57 -0
  57. data/lib/gruff/stacked_mixin.rb +23 -0
  58. data/rails_generators/gruff/gruff_generator.rb +63 -0
  59. data/rails_generators/gruff/templates/controller.rb +32 -0
  60. data/rails_generators/gruff/templates/functional_test.rb +24 -0
  61. data/test/gruff_test_case.rb +123 -0
  62. data/test/test_accumulator_bar.rb +50 -0
  63. data/test/test_area.rb +134 -0
  64. data/test/test_bar.rb +321 -0
  65. data/test/test_base.rb +8 -0
  66. data/test/test_bullet.rb +26 -0
  67. data/test/test_dot.rb +273 -0
  68. data/test/test_legend.rb +68 -0
  69. data/test/test_line.rb +556 -0
  70. data/test/test_mini_bar.rb +33 -0
  71. data/test/test_mini_pie.rb +26 -0
  72. data/test/test_mini_side_bar.rb +37 -0
  73. data/test/test_net.rb +230 -0
  74. data/test/test_photo.rb +41 -0
  75. data/test/test_pie.rb +154 -0
  76. data/test/test_scene.rb +100 -0
  77. data/test/test_side_bar.rb +29 -0
  78. data/test/test_sidestacked_bar.rb +89 -0
  79. data/test/test_spider.rb +216 -0
  80. data/test/test_stacked_area.rb +52 -0
  81. data/test/test_stacked_bar.rb +52 -0
  82. metadata +186 -0
@@ -0,0 +1,23 @@
1
+
2
+ module Gruff::Base::StackedMixin
3
+ # Used by StackedBar and child classes.
4
+ #
5
+ # tsal: moved from Base 03 FEB 2007
6
+ DATA_VALUES_INDEX = Gruff::Base::DATA_VALUES_INDEX
7
+ def get_maximum_by_stack
8
+ # Get sum of each stack
9
+ max_hash = {}
10
+ @data.each do |data_set|
11
+ data_set[DATA_VALUES_INDEX].each_with_index do |data_point, i|
12
+ max_hash[i] = 0.0 unless max_hash[i]
13
+ max_hash[i] += data_point.to_f
14
+ end
15
+ end
16
+
17
+ # @maximum_value = 0
18
+ max_hash.keys.each do |key|
19
+ @maximum_value = max_hash[key] if max_hash[key] > @maximum_value
20
+ end
21
+ @minimum_value = 0
22
+ end
23
+ end
@@ -0,0 +1,63 @@
1
+ class GruffGenerator < Rails::Generator::NamedBase
2
+
3
+ attr_reader :controller_name,
4
+ :controller_class_path,
5
+ :controller_file_path,
6
+ :controller_class_nesting,
7
+ :controller_class_nesting_depth,
8
+ :controller_class_name,
9
+ :controller_singular_name,
10
+ :controller_plural_name,
11
+ :parent_folder_for_require
12
+ alias_method :controller_file_name, :controller_singular_name
13
+ alias_method :controller_table_name, :controller_plural_name
14
+
15
+ def initialize(runtime_args, runtime_options = {})
16
+ super
17
+
18
+ # Take controller name from the next argument.
19
+ @controller_name = runtime_args.shift
20
+
21
+ base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@controller_name)
22
+ @controller_class_name_without_nesting, @controller_singular_name, @controller_plural_name = inflect_names(base_name)
23
+
24
+ if @controller_class_nesting.empty?
25
+ @controller_class_name = @controller_class_name_without_nesting
26
+ else
27
+ @controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}"
28
+ end
29
+ end
30
+
31
+ def manifest
32
+ record do |m|
33
+ # Check for class naming collisions.
34
+ m.class_collisions controller_class_path, "#{controller_class_name}Controller",
35
+ "#{controller_class_name}ControllerTest"
36
+
37
+ # Controller, helper, views, and test directories.
38
+ m.directory File.join('app/controllers', controller_class_path)
39
+ m.directory File.join('test/functional', controller_class_path)
40
+
41
+ m.template 'controller.rb',
42
+ File.join('app/controllers',
43
+ controller_class_path,
44
+ "#{controller_file_name}_controller.rb")
45
+
46
+ # For some reason this doesn't take effect if done in initialize()
47
+ @parent_folder_for_require = @controller_class_path.join('/').gsub(%r%app/controllers/?%, '')
48
+ @parent_folder_for_require += @parent_folder_for_require.blank? ? '' : '/'
49
+
50
+ m.template 'functional_test.rb',
51
+ File.join('test/functional',
52
+ controller_class_path,
53
+ "#{controller_file_name}_controller_test.rb")
54
+
55
+ end
56
+ end
57
+
58
+ protected
59
+ # Override with your own usage banner.
60
+ def banner
61
+ "Usage: #{$0} gruff ControllerName"
62
+ end
63
+ end
@@ -0,0 +1,32 @@
1
+ class <%= controller_class_name %>Controller < ApplicationController
2
+
3
+ # To make caching easier, add a line like this to config/routes.rb:
4
+ # map.graph "graph/:action/:id/image.png", :controller => "graph"
5
+ #
6
+ # Then reference it with the named route:
7
+ # image_tag graph_url(:action => 'show', :id => 42)
8
+
9
+ def show
10
+ g = Gruff::Line.new
11
+ # Uncomment to use your own theme or font
12
+ # See http://colourlovers.com or http://www.firewheeldesign.com/widgets/ for color ideas
13
+ # g.theme = {
14
+ # :colors => ['#663366', '#cccc99', '#cc6633', '#cc9966', '#99cc99'],
15
+ # :marker_color => 'white',
16
+ # :background_colors => ['black', '#333333']
17
+ # }
18
+ # g.font = File.expand_path('artwork/fonts/VeraBd.ttf', RAILS_ROOT)
19
+
20
+ g.title = "Gruff-o-Rama"
21
+
22
+ g.data("Apples", [1, 2, 3, 4, 4, 3])
23
+ g.data("Oranges", [4, 8, 7, 9, 8, 9])
24
+ g.data("Watermelon", [2, 3, 1, 5, 6, 8])
25
+ g.data("Peaches", [9, 9, 10, 8, 7, 9])
26
+
27
+ g.labels = {0 => '2004', 2 => '2005', 4 => '2006'}
28
+
29
+ send_data(g.to_blob, :disposition => 'inline', :type => 'image/png', :filename => "gruff.png")
30
+ end
31
+
32
+ end
@@ -0,0 +1,24 @@
1
+ require File.dirname(__FILE__) + '<%= '/..' * controller_class_name.split("::").length %>/test_helper'
2
+ require '<%= parent_folder_for_require %><%= controller_file_name %>_controller'
3
+
4
+ # Re-raise errors caught by the controller.
5
+ class <%= controller_class_name %>Controller; def rescue_action(e) raise e end; end
6
+
7
+ class <%= controller_class_name %>ControllerTest < Test::Unit::TestCase
8
+
9
+ #fixtures :data
10
+
11
+ def setup
12
+ @controller = <%= controller_class_name %>Controller.new
13
+ @request = ActionController::TestRequest.new
14
+ @response = ActionController::TestResponse.new
15
+ end
16
+
17
+ # TODO Replace this with your actual tests
18
+ def test_show
19
+ get :show
20
+ assert_response :success
21
+ assert_equal 'image/png', @response.headers['Content-Type']
22
+ end
23
+
24
+ end
@@ -0,0 +1,123 @@
1
+ $:.unshift(File.dirname(__FILE__) + "/../lib/")
2
+ # require 'rubygems'
3
+
4
+ require 'test/unit'
5
+ require 'gruff'
6
+ require 'fileutils'
7
+ # require 'test_timer'
8
+
9
+ TEST_OUTPUT_DIR = File.dirname(__FILE__) + "/output"
10
+ FileUtils.mkdir_p(TEST_OUTPUT_DIR)
11
+
12
+ class GruffTestCase < Test::Unit::TestCase
13
+
14
+ def setup
15
+ @datasets = [
16
+ [:Jimmy, [25, 36, 86, 39, 25, 31, 79, 88]],
17
+ [:Charles, [80, 54, 67, 54, 68, 70, 90, 95]],
18
+ [:Julie, [22, 29, 35, 38, 36, 40, 46, 57]],
19
+ [:Jane, [95, 95, 95, 90, 85, 80, 88, 100]],
20
+ [:Philip, [90, 34, 23, 12, 78, 89, 98, 88]],
21
+ ["Arthur", [5, 10, 13, 11, 6, 16, 22, 32]],
22
+ ]
23
+
24
+ @labels = {
25
+ 0 => '5/6',
26
+ 1 => '5/15',
27
+ 2 => '5/24',
28
+ 3 => '5/30',
29
+ 4 => '6/4',
30
+ 5 => '6/12',
31
+ 6 => '6/21',
32
+ 7 => '6/28',
33
+ }
34
+ end
35
+
36
+ def setup_single_dataset
37
+ @datasets = [
38
+ [:Jimmy, [25, 36, 86]]
39
+ ]
40
+
41
+ @labels = {
42
+ 0 => 'You',
43
+ 1 => 'Average',
44
+ 2 => 'Lifetime'
45
+ }
46
+ end
47
+
48
+ def setup_wide_dataset
49
+ @datasets = [
50
+ ["Auto", 25],
51
+ ["Food", 5],
52
+ ["Entertainment", 15]
53
+ ]
54
+
55
+ @labels = { 0 => 'This Month' }
56
+ end
57
+
58
+ def test_dummy
59
+ assert true
60
+ end
61
+
62
+ protected
63
+
64
+ # Generate graphs at several sizes.
65
+ #
66
+ # Also writes the graph to disk.
67
+ #
68
+ # graph_sized 'bar_basic' do |g|
69
+ # g.data('students', [1, 2, 3, 4])
70
+ # end
71
+ #
72
+ def graph_sized(filename, sizes=['', 400])
73
+ class_name = self.class.name.gsub(/^TestGruff/, '')
74
+ Array(sizes).each do |size|
75
+ g = instance_eval("Gruff::#{class_name}.new #{size}")
76
+ g.title = "#{class_name} Graph"
77
+ yield g
78
+ write_test_file g, "#{filename}_#{size}.png"
79
+ end
80
+ end
81
+
82
+ def write_test_file(graph, filename)
83
+ graph.write([TEST_OUTPUT_DIR, filename].join("/"))
84
+ end
85
+
86
+ ##
87
+ # Example:
88
+ #
89
+ # setup_basic_graph Gruff::Pie, 400
90
+ #
91
+ def setup_basic_graph(*args)
92
+ klass, size = Gruff::Bar, 400
93
+ # Allow args to be klass, size or just klass or just size.
94
+ #
95
+ # TODO Refactor
96
+ case args.length
97
+ when 1
98
+ case args[0]
99
+ when Fixnum
100
+ size = args[0]
101
+ klass = eval("Gruff::#{self.class.name.gsub(/^TestGruff/, '')}")
102
+ when String
103
+ size = args[0]
104
+ klass = eval("Gruff::#{self.class.name.gsub(/^TestGruff/, '')}")
105
+ else
106
+ klass = args[0]
107
+ end
108
+ when 2
109
+ klass, size = args[0], args[1]
110
+ end
111
+
112
+ g = klass.new(size)
113
+ g.title = "My Bar Graph"
114
+ g.labels = @labels
115
+
116
+
117
+ @datasets.each do |data|
118
+ g.data(data[0], data[1])
119
+ end
120
+ g
121
+ end
122
+
123
+ end
@@ -0,0 +1,50 @@
1
+ require File.dirname(__FILE__) + "/gruff_test_case"
2
+
3
+ class TestGruffAccumulatorBar < GruffTestCase
4
+
5
+ # TODO Delete old output files once when starting tests
6
+
7
+ def setup
8
+ @datasets = [
9
+ (1..20).to_a.map { rand(10) }
10
+ ]
11
+ end
12
+
13
+ def test_accumulator
14
+ g = Gruff::AccumulatorBar.new 500
15
+ g.title = "Your Savings"
16
+ g.hide_legend = true
17
+
18
+ # g.font = File.expand_path(File.dirname(__FILE__) + "/../assets/fonts/ATMA____.TTF")
19
+
20
+ g.marker_font_size = 18
21
+
22
+ g.theme = {
23
+ :colors => ['#aedaa9', '#12a702'], # 3077a9 blue, aedaa9 light green
24
+ :marker_color => '#dddddd',
25
+ :font_color => 'black',
26
+ :background_colors => "white"
27
+ # :background_image => File.expand_path(File.dirname(__FILE__) + "/../assets/backgrounds/43things.png")
28
+ }
29
+
30
+ # Attempt at negative numbers
31
+ # g.data 'Savings', (1..20).to_a.map { rand(10) * (rand(2) > 0 ? 1 : -1) }
32
+ g.data 'Savings', (1..12).to_a.map { rand(100) }
33
+ g.labels = (0..11).to_a.inject({}) {|memo, index| {index => '12-26'}.merge(memo)}
34
+
35
+ g.maximum_value = 1000
36
+ g.minimum_value = 0
37
+
38
+ g.write("test/output/accum_bar.png")
39
+ end
40
+
41
+ def test_too_many_args
42
+ assert_raise(Gruff::IncorrectNumberOfDatasetsException) {
43
+ g = Gruff::AccumulatorBar.new
44
+ g.data 'First', [1,1,1]
45
+ g.data 'Too Many', [1,1,1]
46
+ g.write("test/output/_SHOULD_NOT_ACTUALLY_BE_WRITTEN.png")
47
+ }
48
+ end
49
+
50
+ end
@@ -0,0 +1,134 @@
1
+ #!/usr/bin/ruby
2
+
3
+ require File.dirname(__FILE__) + "/gruff_test_case"
4
+
5
+ class TestGruffArea < GruffTestCase
6
+
7
+ def setup
8
+ @datasets = [
9
+ [:Jimmy, [25, 36, 86, 39, 25, 31, 79, 88]],
10
+ [:Charles, [80, 54, 67, 54, 68, 70, 90, 95]],
11
+ [:Julie, [22, 29, 35, 38, 36, 40, 46, 57]],
12
+ [:Jane, [95, 95, 95, 90, 85, 80, 88, 100]],
13
+ [:Philip, [90, 34, 23, 12, 78, 89, 98, 88]],
14
+ ["Arthur", [5, 10, 13, 11, 6, 16, 22, 32]],
15
+ ]
16
+ @sample_labels = {
17
+ 0 => '5/6',
18
+ 1 => '5/15',
19
+ 2 => '5/24',
20
+ 3 => '5/30',
21
+ 4 => '6/4',
22
+ 5 => '6/12',
23
+ 6 => '6/21',
24
+ 7 => '6/28',
25
+ }
26
+
27
+ end
28
+
29
+ def test_area_graph
30
+ g = Gruff::Area.new
31
+ g.title = "Visual Multi-Area Graph Test"
32
+ g.labels = {
33
+ 0 => '5/6',
34
+ 2 => '5/15',
35
+ 4 => '5/24',
36
+ 6 => '5/30',
37
+ }
38
+ @datasets.each do |data|
39
+ g.data(data[0], data[1])
40
+ end
41
+
42
+ # Default theme
43
+ g.write("test/output/area_keynote.png")
44
+ end
45
+
46
+ def test_resize
47
+ g = Gruff::Area.new(400)
48
+ g.title = "Small Size Multi-Area Graph Test"
49
+ g.labels = {
50
+ 0 => '5/6',
51
+ 2 => '5/15',
52
+ 4 => '5/24',
53
+ 6 => '5/30',
54
+ }
55
+ @datasets.each do |data|
56
+ g.data(data[0], data[1])
57
+ end
58
+
59
+ # Default theme
60
+ g.write("test/output/area_keynote_small.png")
61
+ end
62
+
63
+ def test_many_datapoints
64
+ g = Gruff::Area.new
65
+ g.title = "Many Multi-Area Graph Test"
66
+ g.labels = {
67
+ 0 => 'June',
68
+ 10 => 'July',
69
+ 30 => 'August',
70
+ 50 => 'September',
71
+ }
72
+ g.data('many points', (0..50).collect {|i| rand(100) })
73
+
74
+ # Default theme
75
+ g.write("test/output/area_many.png")
76
+ end
77
+
78
+ def test_many_areas_graph_small
79
+ g = Gruff::Area.new(400)
80
+ g.title = "Many Values Area Test 400px"
81
+ g.labels = {
82
+ 0 => '5/6',
83
+ 10 => '5/15',
84
+ 20 => '5/24',
85
+ 30 => '5/30',
86
+ 40 => '6/4',
87
+ 50 => '6/16'
88
+ }
89
+ %w{jimmy jane philip arthur julie bert}.each do |student_name|
90
+ g.data(student_name, (0..50).collect { |i| rand 100 })
91
+ end
92
+
93
+ # Default theme
94
+ g.write("test/output/area_many_areas_small.png")
95
+ end
96
+
97
+ def test_area_graph_tiny
98
+ g = Gruff::Area.new(300)
99
+ g.title = "Area Test 300px"
100
+ g.labels = {
101
+ 0 => '5/6',
102
+ 10 => '5/15',
103
+ 20 => '5/24',
104
+ 30 => '5/30',
105
+ 40 => '6/4',
106
+ 50 => '6/16'
107
+ }
108
+ %w{jimmy jane philip arthur julie bert}.each do |student_name|
109
+ g.data(student_name, (0..50).collect { |i| rand 100 })
110
+ end
111
+
112
+ # Default theme
113
+ g.write("test/output/area_tiny.png")
114
+ end
115
+
116
+ def test_wide
117
+ g = setup_basic_graph('800x400')
118
+ g.title = "Area Wide"
119
+ g.write("test/output/area_wide.png")
120
+ end
121
+
122
+ protected
123
+
124
+ def setup_basic_graph(size=800)
125
+ g = Gruff::Area.new(size)
126
+ g.title = "My Graph Title"
127
+ g.labels = @sample_labels
128
+ @datasets.each do |data|
129
+ g.data(data[0], data[1])
130
+ end
131
+ return g
132
+ end
133
+
134
+ end