gruff 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,5 +1,10 @@
1
1
  CHANGELOG
2
2
 
3
+ == 0.0.3
4
+
5
+ * Added option to draw line graphs without the lines (points only), thanks to Eric Hodel
6
+ * Removed font-minimum check so graphs look better at 300px width
7
+
3
8
  == 0.0.2
4
9
 
5
10
  * Fixed to_blob (thanks to Carlos Villela)
data/lib/gruff/base.rb CHANGED
@@ -15,7 +15,7 @@ end
15
15
 
16
16
  module Gruff
17
17
 
18
- VERSION = '0.0.2'
18
+ VERSION = '0.0.3'
19
19
 
20
20
  class Base
21
21
 
@@ -339,7 +339,7 @@ protected
339
339
 
340
340
  def scale_fontsize(value)
341
341
  new_fontsize = value * @scale
342
- return 10 if new_fontsize < 10
342
+ #return 10 if new_fontsize < 10
343
343
  return new_fontsize
344
344
  end
345
345
 
data/lib/gruff/line.rb CHANGED
@@ -1,62 +1,79 @@
1
-
2
1
  require 'gruff/base'
3
2
 
4
- module Gruff
5
- class Line < Base
6
-
7
- def draw
8
- super
9
-
10
- @x_increment = @graph_width / (@column_count - 1).to_f
11
- circle_radius = 5.0
12
-
13
- @d = @d.stroke_opacity 1.0
14
- @d = @d.stroke_width 5.0
15
-
16
- @norm_data.each do |data_row|
17
- prev_x = prev_y = 0.0
18
- @d = @d.stroke current_color
19
- @d = @d.fill current_color
20
-
21
- data_row[1].each_with_index do |data_point, index|
22
- # Use incremented x and scaled y
23
- new_x = @graph_left + (@x_increment * index)
24
- new_y = @graph_top + (@graph_height - data_point * @graph_height)
25
-
26
- @d = @d.line(prev_x, prev_y, new_x, new_y) if (prev_x > 0) && (prev_y > 0)
27
- @d = @d.circle(new_x, new_y, new_x - circle_radius, new_y)
28
-
29
- draw_label(new_x, index)
30
-
31
- prev_x = new_x
32
- prev_y = new_y
3
+ class Gruff::Line < Gruff::Base
4
+
5
+ #
6
+ # Call with target pixel width of graph (800, 400, 300), and/or 'false' to omit lines (points only).
7
+ #
8
+ # g = Gruff::Line.new(400) # 400px wide with lines
9
+ #
10
+ # g = Gruff::Line.new(400, false) # 400px wide, no lines
11
+ #
12
+ # g = Gruff::Line.new(false) # Defaults to 800px wide, no lines
13
+ def initialize(*args)
14
+ raise ArgumentError, "Wrong number of arguments" if args.length > 2
15
+ if args.empty? or not Numeric === args.first then
16
+ super()
17
+ else
18
+ super args.shift
19
+ end
20
+ @lines = args.empty? || args.shift # draw lines by default
21
+ end
22
+
23
+ def draw
24
+ super
25
+
26
+ @x_increment = @graph_width / (@column_count - 1).to_f
27
+ circle_radius = 5.0
28
+
29
+ @d = @d.stroke_opacity 1.0
30
+ @d = @d.stroke_width 5.0
31
+
32
+ @norm_data.each do |data_row|
33
+ prev_x = prev_y = 0.0
34
+ @d = @d.stroke current_color
35
+ @d = @d.fill current_color
36
+
37
+ data_row[1].each_with_index do |data_point, index|
38
+ # Use incremented x and scaled y
39
+ new_x = @graph_left + (@x_increment * index)
40
+ new_y = @graph_top + (@graph_height - data_point * @graph_height)
41
+
42
+ if @lines and prev_x > 0 and prev_y > 0 then
43
+ @d = @d.line(prev_x, prev_y, new_x, new_y)
33
44
  end
34
-
35
- increment_color()
45
+ @d = @d.circle(new_x, new_y, new_x - circle_radius, new_y)
46
+
47
+ draw_label(new_x, index)
48
+
49
+ prev_x = new_x
50
+ prev_y = new_y
36
51
  end
37
-
38
- @d.draw(@base_image)
52
+
53
+ increment_color()
39
54
  end
40
55
 
56
+ @d.draw(@base_image)
57
+ end
58
+
41
59
  private
42
60
 
43
- # Draws column labels below graph
44
- def draw_label(x_offset, index)
45
- if !@labels[index].nil? && @labels_seen[index].nil?
46
- @d.fill = @marker_color
47
- @d.font = @font
48
- @d.stroke = 'transparent'
49
- @d.font_weight = NormalWeight
50
- @d.pointsize = scale_fontsize(@marker_pointsize)
51
- @d.gravity = CenterGravity
52
- @d = @d.annotate_scaled(@base_image,
53
- 1, 1,
54
- #150, 30,
55
- x_offset, @raw_rows - 80,
56
- @labels[index], @scale)
57
- @labels_seen[index] = 1
58
- end
61
+ ##
62
+ # Draws column labels below graph
63
+ def draw_label(x_offset, index)
64
+ if !@labels[index].nil? && @labels_seen[index].nil?
65
+ @d.fill = @marker_color
66
+ @d.font = @font
67
+ @d.stroke = 'transparent'
68
+ @d.font_weight = NormalWeight
69
+ @d.pointsize = scale_fontsize(@marker_pointsize)
70
+ @d.gravity = CenterGravity
71
+ @d = @d.annotate_scaled(@base_image,
72
+ 1, 1,
73
+ x_offset, @raw_rows - 80,
74
+ @labels[index], @scale)
75
+ @labels_seen[index] = 1
59
76
  end
60
-
61
77
  end
62
- end
78
+
79
+ end
data/test/line_test.rb CHANGED
@@ -166,6 +166,67 @@ class TestGruffLine < Test::Unit::TestCase
166
166
  g.write("test/output/line_font.png")
167
167
  end
168
168
 
169
+ def test_dots_graph
170
+ g = Gruff::Line.new(false)
171
+ g.title = "Dots Test 800px"
172
+ g.labels = {
173
+ 0 => '5/6',
174
+ 10 => '5/15',
175
+ 20 => '5/24',
176
+ 30 => '5/30',
177
+ 40 => '6/4',
178
+ 50 => '6/16'
179
+ }
180
+ %w{jimmy jane philip arthur julie bert}.each do |student_name|
181
+ g.data(student_name, (0..50).collect { |i| rand 100 })
182
+ end
183
+
184
+ # Default theme
185
+ g.write("test/output/line_dots.png")
186
+ end
187
+
188
+
189
+ def test_dots_graph_small
190
+ g = Gruff::Line.new(400, false)
191
+ g.title = "Dots Test 400px"
192
+ g.labels = {
193
+ 0 => '5/6',
194
+ 10 => '5/15',
195
+ 20 => '5/24',
196
+ 30 => '5/30',
197
+ 40 => '6/4',
198
+ 50 => '6/16'
199
+ }
200
+ %w{jimmy jane philip arthur julie bert}.each do |student_name|
201
+ g.data(student_name, (0..50).collect { |i| rand 100 })
202
+ end
203
+
204
+ # Default theme
205
+ g.write("test/output/line_dots_small.png")
206
+ end
207
+
208
+
209
+ def test_dots_graph_tiny
210
+ g = Gruff::Line.new(300, false)
211
+ g.title = "Dots Test 300px"
212
+ g.labels = {
213
+ 0 => '5/6',
214
+ 10 => '5/15',
215
+ 20 => '5/24',
216
+ 30 => '5/30',
217
+ 40 => '6/4',
218
+ 50 => '6/16'
219
+ }
220
+ %w{jimmy jane philip arthur julie bert}.each do |student_name|
221
+ g.data(student_name, (0..50).collect { |i| rand 100 })
222
+ end
223
+
224
+ # Default theme
225
+ g.write("test/output/line_dots_tiny.png")
226
+ end
227
+
228
+
229
+
169
230
  =begin
170
231
  def test_very_small_graphs
171
232
  [300, 200, 100].each do |size|
metadata CHANGED
@@ -3,7 +3,7 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: gruff
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.0.2
6
+ version: 0.0.3
7
7
  date: 2005-10-26 00:00:00 -07:00
8
8
  summary: Beautiful graphs for one or multiple datasets.
9
9
  require_paths: