Added the Gruff library to the lib/ directory of the the rails folder, and the
[selectricity] / lib / gruff-0.2.8 / test / test_bar.rb
1 #!/usr/bin/ruby
2
3 require File.dirname(__FILE__) + "/gruff_test_case"
4
5 class TestGruffBar < GruffTestCase
6
7   # TODO Delete old output files once when starting tests
8
9   def setup
10     @datasets = [
11       [:Jimmy, [25, 36, 86, 39]],
12       [:Charles, [80, 54, 67, 54]],
13       [:Julie, [22, 29, 35, 38]],
14       #[:Jane, [95, 95, 95, 90, 85, 80, 88, 100]],
15       #[:Philip, [90, 34, 23, 12, 78, 89, 98, 88]],
16       #["Arthur", [5, 10, 13, 11, 6, 16, 22, 32]],
17       ]
18   end
19
20   def test_bar_graph
21     g = setup_basic_graph
22     g.title = "Bar Graph Test"
23     g.write("test/output/bar_keynote.png")
24         
25     g = setup_basic_graph
26     g.title = "Visual Multi-Line Bar Graph Test"
27     g.theme_rails_keynote
28     g.write("test/output/bar_rails_keynote.png")
29     
30     g = setup_basic_graph
31     g.title = "Visual Multi-Line Bar Graph Test"
32     g.theme_odeo
33     g.write("test/output/bar_odeo.png")
34   end
35
36   def test_bar_graph_set_colors
37     g = Gruff::Bar.new
38     g.title = "Bar Graph With Manual Colors"
39     g.labels = {
40       0 => '5/6', 
41       1 => '5/15', 
42       2 => '5/24', 
43       3 => '5/30', 
44     }
45     g.data(:Art, [0, 5, 8, 15], '#990000')
46     g.data(:Philosophy, [10, 3, 2, 8], '#009900')
47     g.data(:Science, [2, 15, 8, 11], '#990099')
48
49     g.minimum_value = 0
50         
51     g.write("test/output/bar_manual_colors.png")
52   end
53
54   def test_bar_graph_small
55     g = Gruff::Bar.new(400)
56     g.title = "Visual Multi-Line Bar Graph Test"
57     g.labels = {
58       0 => '5/6', 
59       1 => '5/15', 
60       2 => '5/24', 
61       3 => '5/30', 
62     }
63     @datasets.each do |data|
64       g.data(data[0], data[1])
65     end
66
67     g.write("test/output/bar_keynote_small.png")
68   end
69
70   # Somewhat worthless test. Should an error be thrown?
71   # def test_nil_font
72   #   g = setup_basic_graph 400
73   #   g.title = "Nil Font"
74   #   g.font = nil
75   #   g.write "test/output/bar_nil_font.png"
76   # end
77
78
79   def test_no_line_markers
80     g = setup_basic_graph(400)
81     g.title = "No Line Markers"
82     g.hide_line_markers = true
83     g.write("test/output/bar_no_line_markers.png")    
84   end
85
86   def test_no_legend
87     g = setup_basic_graph(400)
88     g.title = "No Legend"
89     g.hide_legend = true
90     g.write("test/output/bar_no_legend.png")    
91   end
92
93   def test_no_title
94     g = setup_basic_graph(400)
95     g.title = "No Title"
96     g.hide_title = true
97     g.write("test/output/bar_no_title.png")    
98   end
99   
100   def test_no_title_or_legend
101     g = setup_basic_graph(400)
102     g.title = "No Title or Legend"
103     g.hide_legend = true
104     g.hide_title = true
105     g.write("test/output/bar_no_title_or_legend.png")    
106   end
107
108   def test_set_marker_count
109     g = setup_basic_graph(400)
110     g.title = "Set marker"
111     g.marker_count = 10
112     g.write("test/output/bar_set_marker.png")
113   end
114
115   def test_set_legend_box_size
116     g = setup_basic_graph(400)
117     g.title = "Set Legend Box Size"
118     g.legend_box_size = 10.0
119     g.write("test/output/bar_set_legend_box_size_sm.png")
120     
121     g = setup_basic_graph(400)
122     g.title = "Set Legend Box Size"
123     g.legend_box_size = 50.0
124     g.write("test/output/bar_set_legend_box_size_lg.png")
125   end
126
127   def test_x_y_labels
128     g = setup_basic_graph(400)
129     g.title = "X Y Labels"
130     g.x_axis_label = 'Score (%)'
131     g.y_axis_label = "Students"
132     g.write("test/output/bar_x_y_labels.png")    
133   end
134
135   def test_wide_graph
136     g = setup_basic_graph('800x400')
137     g.title = "Wide Graph"
138     g.write("test/output/bar_wide_graph.png")    
139
140     g = setup_basic_graph('400x200')
141     g.title = "Wide Graph Small"
142     g.write("test/output/bar_wide_graph_small.png")
143   end
144
145
146   def test_tall_graph
147     g = setup_basic_graph('400x600')
148     g.title = "Tall Graph"
149     g.write("test/output/bar_tall_graph.png")
150
151     g = setup_basic_graph('200x400')
152     g.title = "Tall Graph Small"
153     g.write("test/output/bar_tall_graph_small.png")
154   end
155
156
157   def test_one_value
158     g = Gruff::Bar.new
159     g.title = "One Value Graph Test"
160     g.labels = {
161       0 => '1', 
162       1 => '2'
163     }
164     g.data('one', [1,1])
165
166     g.write("test/output/bar_one_value.png")
167   end
168
169
170   def test_negative
171     g = Gruff::Bar.new
172     g.title = "Pos/Neg Bar Graph Test"
173     g.labels = {
174       0 => '5/6', 
175       1 => '5/15', 
176       2 => '5/24', 
177       3 => '5/30', 
178     }
179     g.data(:apples, [-1, 0, 4, -4])
180     g.data(:peaches, [10, 8, 6, 3])
181
182     g.write("test/output/bar_pos_neg.png")
183   end
184
185
186   def test_nearly_zero
187     g = Gruff::Bar.new
188     g.title = "Nearly Zero Graph"
189     g.labels = {
190       0 => '5/6', 
191       1 => '5/15', 
192       2 => '5/24', 
193       3 => '5/30', 
194     }
195     g.data(:apples, [1, 2, 3, 4])
196     g.data(:peaches, [4, 3, 2, 1])
197     g.minimum_value = 0
198     g.maximum_value = 10
199     g.write("test/output/bar_nearly_zero_max_10.png")
200   end
201
202   def test_y_axis_increment
203     generate_with_y_axis_increment 2.0
204     generate_with_y_axis_increment 1
205     generate_with_y_axis_increment 5
206     generate_with_y_axis_increment 20
207   end
208
209   def generate_with_y_axis_increment(increment)
210     g = Gruff::Bar.new
211     g.title = "Y Axis Set to #{increment}"
212     g.labels = {
213       0 => '5/6', 
214       1 => '5/15', 
215       2 => '5/24', 
216       3 => '5/30', 
217     }
218     g.y_axis_increment = increment
219     g.data(:apples, [1, 0.2, 0.5, 0.7])
220     g.data(:peaches, [2.5, 2.3, 2, 6.1])
221     g.write("test/output/bar_y_increment_#{increment}.png")
222   end
223
224
225   def test_custom_theme
226     g = Gruff::Bar.new
227     g.title = "Custom Theme"
228     g.font = File.expand_path('CREABBRG.TTF', ENV['MAGICK_FONT_PATH'])
229     g.title_font_size = 60
230     g.legend_font_size = 32
231     g.marker_font_size = 32
232     g.theme = {
233       :colors => %w(#efd250 #666699 #e5573f #9595e2),
234       :marker_color => 'white',
235       :font_color => 'blue',
236       :background_image => "assets/pc306715.jpg"
237     }
238     g.labels = {
239       0 => '5/6', 
240       1 => '5/15', 
241       2 => '5/24', 
242       3 => '5/30', 
243     }
244     g.data(:vancouver, [1, 2, 3, 4])
245     g.data(:seattle, [2, 4, 6, 8])
246     g.data(:portland, [3, 1, 7, 3])
247     g.data(:victoria, [4, 3, 5, 7])
248     g.minimum_value = 0
249     g.write("test/output/bar_themed.png")
250   end
251
252   def test_july_enhancements
253     g = Gruff::Bar.new(600)
254     g.hide_legend = true
255     g.title = "Full speed ahead"
256     g.labels = (0..10).inject({}) { |memo, i| memo.merge({ i => (i*10).to_s}) }
257     g.data(:apples, (0..9).map { rand(20)/10.0 })
258     g.y_axis_increment = 1.0
259     g.x_axis_label = 'Score (%)'
260     g.y_axis_label = 'Students'
261     write_test_file g, 'enhancements.png'
262   end
263
264
265 protected
266
267   def setup_basic_graph(size=800)
268     g = Gruff::Bar.new(size)
269     g.title = "My Bar Graph"
270     g.labels = {
271       0 => '5/6', 
272       1 => '5/15', 
273       2 => '5/24', 
274       3 => '5/30', 
275     }
276     @datasets.each do |data|
277       g.data(data[0], data[1])
278     end
279     g
280   end
281
282   
283 end
284

Benjamin Mako Hill || Want to submit a patch?