gruff 0.1.2 → 0.2.3

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.
@@ -0,0 +1,39 @@
1
+
2
+ ##
3
+ # A mixin for methods that need to be deleted or have been
4
+ # replaced by cleaner code.
5
+
6
+ module Gruff
7
+ module Deprecated
8
+
9
+ def scale_measurements
10
+ setup_graph_measurements
11
+ end
12
+
13
+ def total_height
14
+ @rows + 10
15
+ end
16
+
17
+ def graph_top
18
+ @graph_top * @scale
19
+ end
20
+
21
+ def graph_height
22
+ @graph_height * @scale
23
+ end
24
+
25
+ def graph_left
26
+ @graph_left * @scale
27
+ end
28
+
29
+ def graph_width
30
+ @graph_width * @scale
31
+ end
32
+
33
+ # TODO Should be calculate_graph_height
34
+ # def setup_graph_height
35
+ # @graph_height = @graph_bottom - @graph_top
36
+ # end
37
+
38
+ end
39
+ end
@@ -37,8 +37,10 @@ class Gruff::Line < Gruff::Base
37
37
  super
38
38
 
39
39
  return unless @has_data
40
-
41
- @x_increment = @graph_width / (@column_count - 1).to_f
40
+
41
+ # Check to see if more than one datapoint was given. NaN can result otherwise.
42
+ @x_increment = (@column_count > 1) ? (@graph_width / (@column_count - 1).to_f) : @graph_width
43
+
42
44
  circle_radius = clip_value_if_greater_than(@columns / (@norm_data.first[1].size * 2.5), 5.0)
43
45
 
44
46
  @d = @d.stroke_opacity 1.0
@@ -11,7 +11,7 @@ class Gruff::Pie < Gruff::Base
11
11
  return unless @has_data
12
12
 
13
13
  diameter = @graph_height
14
- radius = [@graph_width, @graph_height].min / 2.0
14
+ radius = ([@graph_width, @graph_height].min / 2.0) * 0.8
15
15
  top_x = @graph_left + (@graph_width - diameter) / 2.0
16
16
  center_x = @graph_left + (@graph_width / 2.0)
17
17
  center_y = @graph_top + (@graph_height / 2.0) - 10 # Move graph up a bit
@@ -2,10 +2,7 @@
2
2
  require "observer"
3
3
  require File.dirname(__FILE__) + '/base'
4
4
 
5
- # EXPERIMENTAL!
6
- #
7
- # Started by Geoffrey Grosenbach at Canada on Rails, April 2006.
8
- #
5
+ ##
9
6
  # A scene is a non-linear graph that assembles layers together to tell a story.
10
7
  # Layers are folders with appropriately named files (see below). You can group
11
8
  # layers and control them together or just set their values individually.
@@ -147,6 +144,8 @@ class Gruff::Layer
147
144
  else
148
145
  select_default
149
146
  end
147
+ # Finally, try to use 'default' if we're still blank
148
+ @selected_filename ||= select_default
150
149
  end
151
150
 
152
151
  # Returns the full path to the selected image, or a blank string
@@ -185,12 +184,14 @@ private
185
184
  end
186
185
 
187
186
  def select_default
188
- file_exists_or_blank "default"
187
+ @filenames.include?("default.png") ? "default.png" : ''
189
188
  end
190
189
 
191
- # Returns the string "#{filename}.png", if it exists
190
+ # Returns the string "#{filename}.png", if it exists.
191
+ #
192
+ # Failing that, it returns default.png, or '' if that doesn't exist.
192
193
  def file_exists_or_blank(filename)
193
- @filenames.include?("#{filename}.png") ? "#{filename}.png" : ''
194
+ @filenames.include?("#{filename}.png") ? "#{filename}.png" : select_default
194
195
  end
195
196
 
196
197
  end
@@ -18,45 +18,18 @@ class TestGruffBar < GruffTestCase
18
18
  end
19
19
 
20
20
  def test_bar_graph
21
- g = Gruff::Bar.new
22
- g.title = "Visual Multi-Line Bar Graph Test"
23
- g.labels = {
24
- 0 => '5/6',
25
- 1 => '5/15',
26
- 2 => '5/24',
27
- 3 => '5/30',
28
- }
29
- @datasets.each do |data|
30
- g.data(data[0], data[1])
31
- end
21
+ g = setup_basic_graph
22
+ g.title = "Bar Graph Test"
32
23
  g.write("test/output/bar_keynote.png")
33
24
 
34
- g = Gruff::Bar.new
25
+ g = setup_basic_graph
35
26
  g.title = "Visual Multi-Line Bar Graph Test"
36
- g.labels = {
37
- 0 => '5/6',
38
- 1 => '5/15',
39
- 2 => '5/24',
40
- 3 => '5/30',
41
- }
42
27
  g.theme_rails_keynote
43
- @datasets.each do |data|
44
- g.data(data[0], data[1])
45
- end
46
28
  g.write("test/output/bar_rails_keynote.png")
47
29
 
48
- g = Gruff::Bar.new
30
+ g = setup_basic_graph
49
31
  g.title = "Visual Multi-Line Bar Graph Test"
50
- g.labels = {
51
- 0 => '5/6',
52
- 1 => '5/15',
53
- 2 => '5/24',
54
- 3 => '5/30',
55
- }
56
32
  g.theme_odeo
57
- @datasets.each do |data|
58
- g.data(data[0], data[1])
59
- end
60
33
  g.write("test/output/bar_odeo.png")
61
34
  end
62
35
 
@@ -94,13 +67,13 @@ class TestGruffBar < GruffTestCase
94
67
  g.write("test/output/bar_keynote_small.png")
95
68
  end
96
69
 
97
-
98
- def test_nil_font
99
- g = setup_basic_graph 400
100
- g.title = "Nil Font"
101
- g.font = nil
102
- g.write "test/output/bar_nil_font.png"
103
- end
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
104
77
 
105
78
 
106
79
  def test_no_line_markers
@@ -110,6 +83,44 @@ class TestGruffBar < GruffTestCase
110
83
  g.write("test/output/bar_no_line_markers.png")
111
84
  end
112
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_x_y_labels
116
+ g = setup_basic_graph(400)
117
+ g.title = "X Y Labels"
118
+ g.x_axis_label = 'Score (%)'
119
+ g.y_axis_label = "Students"
120
+ g.write("test/output/bar_x_y_labels.png")
121
+ end
122
+
123
+
113
124
  def test_wide_graph
114
125
  g = setup_basic_graph('800x400')
115
126
  g.title = "Wide Graph"
@@ -121,6 +132,17 @@ class TestGruffBar < GruffTestCase
121
132
  end
122
133
 
123
134
 
135
+ def test_tall_graph
136
+ g = setup_basic_graph('400x600')
137
+ g.title = "Tall Graph"
138
+ g.write("test/output/bar_tall_graph.png")
139
+
140
+ g = setup_basic_graph('200x400')
141
+ g.title = "Tall Graph Small"
142
+ g.write("test/output/bar_tall_graph_small.png")
143
+ end
144
+
145
+
124
146
  def test_one_value
125
147
  g = Gruff::Bar.new
126
148
  g.title = "One Value Graph Test"
@@ -163,13 +185,36 @@ class TestGruffBar < GruffTestCase
163
185
  g.data(:peaches, [4, 3, 2, 1])
164
186
  g.minimum_value = 0
165
187
  g.maximum_value = 10
166
- g.write("test/output/bar_nearly_zero.png")
188
+ g.write("test/output/bar_nearly_zero_max_10.png")
189
+ end
190
+
191
+ def test_y_axis_increment
192
+ generate_with_y_axis_increment 2.0
193
+ generate_with_y_axis_increment 1
194
+ generate_with_y_axis_increment 5
195
+ generate_with_y_axis_increment 20
196
+ end
197
+
198
+ def generate_with_y_axis_increment(increment)
199
+ g = Gruff::Bar.new
200
+ g.title = "Y Axis Set to #{increment}"
201
+ g.labels = {
202
+ 0 => '5/6',
203
+ 1 => '5/15',
204
+ 2 => '5/24',
205
+ 3 => '5/30',
206
+ }
207
+ g.y_axis_increment = increment
208
+ g.data(:apples, [1, 0.2, 0.5, 0.7])
209
+ g.data(:peaches, [2.5, 2.3, 2, 6.1])
210
+ g.write("test/output/bar_y_increment_#{increment}.png")
167
211
  end
168
212
 
169
213
 
170
214
  def test_custom_theme
171
215
  g = Gruff::Bar.new
172
216
  g.title = "Custom Theme"
217
+ g.font = File.expand_path('CREABBRG.TTF', ENV['MAGICK_FONT_PATH'])
173
218
  g.title_font_size = 60
174
219
  g.legend_font_size = 32
175
220
  g.marker_font_size = 32
@@ -192,6 +237,17 @@ class TestGruffBar < GruffTestCase
192
237
  g.write("test/output/bar_themed.png")
193
238
  end
194
239
 
240
+ def test_july_enhancements
241
+ g = Gruff::Bar.new(600)
242
+ g.hide_legend = true
243
+ g.title = "Full speed ahead"
244
+ g.labels = (0..10).inject({}) { |memo, i| memo.merge({ i => (i*10).to_s}) }
245
+ g.data(:apples, (0..9).map { rand(20)/10.0 })
246
+ g.y_axis_increment = 1.0
247
+ g.x_axis_label = 'Score (%)'
248
+ g.y_axis_label = 'Students'
249
+ write_test_file g, 'enhancements.png'
250
+ end
195
251
 
196
252
 
197
253
  protected
@@ -9,5 +9,27 @@ class GruffTestCase < Test::Unit::TestCase
9
9
  assert true
10
10
  end
11
11
 
12
+ protected
13
+
14
+ # Generate graphs at several sizes.
15
+ #
16
+ # Also writes the graph to disk.
17
+ #
18
+ # graph_sized 'bar_basic' do |g|
19
+ # g.data('students', [1, 2, 3, 4])
20
+ # end
21
+ #
22
+ def graph_sized(filename, &block)
23
+ class_name = self.class.name.gsub(/^TestGruff/, '')
24
+ ['', 400].each do |size|
25
+ g = instance_eval("Gruff::#{class_name}.new #{size}")
26
+ block.call g
27
+ write_test_file g, "#{filename}#{size}.png"
28
+ end
29
+ end
30
+
31
+ def write_test_file(graph, filename)
32
+ graph.write(File.dirname(__FILE__) + "/output/#{filename}")
33
+ end
12
34
 
13
35
  end
@@ -33,6 +33,50 @@ class TestGruffLine < GruffTestCase
33
33
  line_graph_with_themes(400)
34
34
  end
35
35
 
36
+ def test_one_value
37
+ g = Gruff::Line.new
38
+ g.title = "One Value Graph Test"
39
+ g.labels = {
40
+ 0 => '1',
41
+ 1 => '2'
42
+ }
43
+ g.data('one', 1)
44
+
45
+ g.write("test/output/line_one_value.png")
46
+ end
47
+
48
+ def test_one_value_array
49
+ g = Gruff::Line.new
50
+ g.title = "One Value Graph Test"
51
+ g.labels = {
52
+ 0 => '1',
53
+ 1 => '2'
54
+ }
55
+ g.data('one', [1])
56
+
57
+ g.write("test/output/line_one_value_array.png")
58
+ end
59
+
60
+
61
+ def test_fix_hang
62
+ g = Gruff::Line.new(320)
63
+ g.title = "Hang Value Graph Test"
64
+ g.data('test', [0,0,100])
65
+
66
+ g.write("test/output/line_hang_value.png")
67
+ end
68
+
69
+ # def test_fix_crash
70
+ # g = Gruff::Line.new(370)
71
+ # g.title = "Crash Test"
72
+ # g.data "ichi", [5]
73
+ # g.data "ni", [0]
74
+ # g.data "san", [0]
75
+ # g.data "shi", [0]
76
+ # g.write("test/output/line_crash_fix_test.png")
77
+ # end
78
+
79
+
36
80
  def test_line_small_values
37
81
  @datasets = [
38
82
  [:small, [0.1, 0.14356, 0.0, 0.5674839, 0.456]],
@@ -121,6 +165,7 @@ class TestGruffLine < GruffTestCase
121
165
  50 => 'September',
122
166
  }
123
167
  g.data('many points', (0..50).collect {|i| rand(100) })
168
+ g.x_axis_label = "Months"
124
169
 
125
170
  # Default theme
126
171
  g.write("test/output/line_many.png")
@@ -137,6 +182,7 @@ class TestGruffLine < GruffTestCase
137
182
  g.title = "Similar High End Values With Floor"
138
183
  g.data('similar points', %w(29.43 29.459 29.498 29.53 29.548 29.589 29.619 29.66 29.689 29.849 29.878 29.74 29.769 29.79 29.808 29.828).collect {|i| i.to_f} )
139
184
  g.minimum_value = 0
185
+ g.y_axis_label = "Barometric Pressure"
140
186
  g.write("test/output/line_similar_high_end_values_with_floor.png")
141
187
  end
142
188
 
@@ -378,7 +424,6 @@ class TestGruffLine < GruffTestCase
378
424
  g.write('test/output/line_many_numbers.png')
379
425
  end
380
426
 
381
-
382
427
  protected
383
428
 
384
429
  # TODO Reset data after each theme
@@ -449,6 +494,7 @@ protected
449
494
  return g
450
495
  end
451
496
 
497
+
452
498
  def setup_all_neg(size=800)
453
499
  g = Gruff::Line.new(size)
454
500
  g.title = "All Neg Line Graph Test"
@@ -70,6 +70,18 @@ class TestGruffPie < GruffTestCase
70
70
  g.write("test/output/pie_zero.png")
71
71
  end
72
72
 
73
+
74
+ def test_pie_graph_one_val
75
+ g = Gruff::Pie.new
76
+ g.title = "Pie Graph One Val"
77
+
78
+ g.data(:Bert, 53)
79
+ g.data(:Adam, 29)
80
+
81
+ g.write("test/output/pie_one_val.png")
82
+ end
83
+
84
+
73
85
  def test_wide
74
86
  g = setup_basic_graph('800x400')
75
87
  g.title = "Wide Pie"
@@ -79,6 +79,12 @@ class TestGruffScene < GruffTestCase
79
79
  l = LayerStub.new(File.expand_path("../assets/city_scene", File.dirname(__FILE__)), "sky")
80
80
  l.update Time.mktime(2006, 7, 4, 4, 00) # 04:00, July 4, 2006
81
81
  assert_equal '0400.png', l.selected_filename
82
+
83
+ # TODO Need number_sample folder
84
+ # l = LayerStub.new(File.expand_path("../assets/city_scene", File.dirname(__FILE__)), "number_sample")
85
+ # assert_equal %w(1.png 2.png default.png), l.filenames
86
+ # l.update 3
87
+ # assert_equal 'default.png', l.selected_filename
82
88
  end
83
89
 
84
90
  private
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: gruff
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.2
7
- date: 2006-04-20 00:00:00 -07:00
6
+ version: 0.2.3
7
+ date: 2006-09-21 00:00:00 -07:00
8
8
  summary: Beautiful graphs for one or multiple datasets.
9
9
  require_paths:
10
10
  - lib
@@ -32,12 +32,14 @@ files:
32
32
  - README
33
33
  - CHANGELOG
34
34
  - MIT-LICENSE
35
+ - lib/CocoaMagick.rb
35
36
  - lib/gruff
36
37
  - lib/gruff.rb
37
38
  - lib/gruff/area.rb
38
39
  - lib/gruff/bar.rb
39
40
  - lib/gruff/bar_conversion.rb
40
41
  - lib/gruff/base.rb
42
+ - lib/gruff/deprecated.rb
41
43
  - lib/gruff/line.rb
42
44
  - lib/gruff/net.rb
43
45
  - lib/gruff/photo_bar.rb