gruff 0.0.2 → 0.0.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.
- data/CHANGELOG +5 -0
- data/lib/gruff/base.rb +2 -2
- data/lib/gruff/line.rb +69 -52
- data/test/line_test.rb +61 -0
- metadata +1 -1
data/CHANGELOG
CHANGED
data/lib/gruff/base.rb
CHANGED
@@ -15,7 +15,7 @@ end
|
|
15
15
|
|
16
16
|
module Gruff
|
17
17
|
|
18
|
-
VERSION = '0.0.
|
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
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
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
|
-
|
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
|
-
|
52
|
+
|
53
|
+
increment_color()
|
39
54
|
end
|
40
55
|
|
56
|
+
@d.draw(@base_image)
|
57
|
+
end
|
58
|
+
|
41
59
|
private
|
42
60
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
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
|
-
|
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|
|