gruff 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/gruff.rb CHANGED
@@ -10,6 +10,6 @@ require File.dirname(__FILE__) + '/gruff/spider'
10
10
  require File.dirname(__FILE__) + '/gruff/net'
11
11
  require File.dirname(__FILE__) + '/gruff/stacked_bar'
12
12
  require File.dirname(__FILE__) + '/gruff/side_stacked_bar'
13
- require File.dirname(__FILE__) + '/gruff/photo_bar'
13
+ # require File.dirname(__FILE__) + '/gruff/photo_bar'
14
14
 
15
15
  require File.dirname(__FILE__) + '/gruff/scene'
data/lib/gruff/bar.rb CHANGED
@@ -5,6 +5,11 @@ require File.dirname(__FILE__) + '/bar_conversion'
5
5
  class Gruff::Bar < Gruff::Base
6
6
 
7
7
  def draw
8
+ # Labels will be centered over the left of the bar if
9
+ # there are more labels than columns. This is basically the same
10
+ # as where it would be for a line graph.
11
+ @center_labels_over_point = (@labels.keys.length > @column_count ? true : false)
12
+
8
13
  super
9
14
  return unless @has_data
10
15
 
@@ -21,10 +26,6 @@ class Gruff::Bar < Gruff::Base
21
26
  conversion.graph_height = @graph_height
22
27
  conversion.graph_top = @graph_top
23
28
 
24
- # Labels will be centered over the left of the bar if
25
- # there are more labels than columns.
26
- center_labels_left = (@labels.keys.length > @column_count ? true : false)
27
-
28
29
  # Set up the right mode [1,2,3] see BarConversion for further explanation
29
30
  if @minimum_value >= 0 then
30
31
  # all bars go from zero to positiv
@@ -44,7 +45,6 @@ class Gruff::Bar < Gruff::Base
44
45
 
45
46
  # iterate over all normalised data
46
47
  @norm_data.each_with_index do |data_row, row_index|
47
- @d = @d.fill data_row[DATA_COLOR_INDEX]
48
48
 
49
49
  data_row[1].each_with_index do |data_point, point_index|
50
50
  # Use incremented x and scaled y
@@ -56,6 +56,7 @@ class Gruff::Bar < Gruff::Base
56
56
  conversion.getLeftYRightYscaled( data_point, conv )
57
57
 
58
58
  # create new bar
59
+ @d = @d.fill data_row[DATA_COLOR_INDEX]
59
60
  @d = @d.rectangle(left_x, conv[0], right_x, conv[1])
60
61
 
61
62
  # Calculate center based on bar_width and current row
@@ -63,13 +64,13 @@ class Gruff::Bar < Gruff::Base
63
64
  (@data.length * @bar_width * point_index) +
64
65
  (@data.length * @bar_width / 2.0)
65
66
  # Subtract half a bar width to center left if requested
66
- draw_label(label_center - (center_labels_left ? @bar_width / 2.0 : 0.0), point_index)
67
+ draw_label(label_center - (@center_labels_over_point ? @bar_width / 2.0 : 0.0), point_index)
67
68
  end
68
69
 
69
70
  end
70
71
 
71
72
  # Draw the last label if requested
72
- draw_label(@graph_right, @column_count) if center_labels_left
73
+ draw_label(@graph_right, @column_count) if @center_labels_over_point
73
74
 
74
75
  @d.draw(@base_image)
75
76
  end
data/lib/gruff/base.rb CHANGED
@@ -20,7 +20,7 @@ require File.dirname(__FILE__) + '/deprecated'
20
20
 
21
21
  module Gruff
22
22
 
23
- VERSION = '0.2.3'
23
+ VERSION = '0.2.4'
24
24
 
25
25
  class Base
26
26
 
@@ -48,6 +48,11 @@ module Gruff
48
48
  # Example: 0 => 2005, 3 => 2006, 5 => 2007, 7 => 2008
49
49
  attr_accessor :labels
50
50
 
51
+ # Used internally for spacing.
52
+ #
53
+ # By default, labels are centered over the point they represent.
54
+ attr_accessor :center_labels_over_point
55
+
51
56
  # A label for the bottom of the graph
52
57
  attr_accessor :x_axis_label
53
58
 
@@ -70,10 +75,10 @@ module Gruff
70
75
  #
71
76
  # Tries to find Bitstream Vera (Vera.ttf) in the location specified by
72
77
  # ENV['MAGICK_FONT_PATH']. Uses default RMagick font otherwise.
73
- attr_accessor :font
78
+ attr_reader :font
74
79
 
75
80
  # Hide various elements
76
- attr_accessor :hide_line_markers, :hide_legend, :hide_title
81
+ attr_accessor :hide_line_markers, :hide_legend, :hide_title, :hide_line_numbers
77
82
 
78
83
  # Message shown when there is no data. Fits up to 20 characters. Defaults to "No Data."
79
84
  attr_accessor :no_data_message
@@ -104,7 +109,10 @@ module Gruff
104
109
  #
105
110
  # If you use this, you must set it after you have given all your data to the graph object.
106
111
  attr_accessor :maximum_value
107
-
112
+
113
+ # Set to false if you don't want the data to be sorted with largest avg values at the back.
114
+ attr_accessor :sort
115
+
108
116
  # Experimental
109
117
  attr_accessor :additional_line_values
110
118
 
@@ -138,6 +146,8 @@ module Gruff
138
146
  @data = Array.new
139
147
  @labels = Hash.new
140
148
  @labels_seen = Hash.new
149
+ @sort = true
150
+ @title = nil
141
151
 
142
152
  @scale = @columns / @raw_columns
143
153
 
@@ -150,31 +160,22 @@ module Gruff
150
160
 
151
161
  @no_data_message = "No Data"
152
162
 
153
- @hide_line_markers = @hide_legend = @hide_title = false
163
+ @hide_line_markers = @hide_legend = @hide_title = @hide_line_numbers = false
164
+ @center_labels_over_point = true
154
165
 
155
166
  @additional_line_values = []
156
167
  @additional_line_colors = []
168
+
169
+ @x_axis_label = @y_axis_label = nil
170
+ @y_axis_increment = nil
171
+ @stacked = nil
172
+ @norm_data = nil
157
173
 
158
174
  reset_themes()
159
175
  theme_keynote()
160
176
  end
161
177
 
162
- # Add a color to the list of available colors for lines.
163
- #
164
- # Example:
165
- # add_color('#c0e9d3')
166
- def add_color(colorname)
167
- @colors << colorname
168
- end
169
178
 
170
- # Replace the entire color list with a new array of colors. You need to have one more color
171
- # than the number of datasets you intend to draw. Also aliased as the colors= setter method.
172
- #
173
- # Example:
174
- # replace_colors('#cc99cc', '#d9e043', '#34d8a2')
175
- def replace_colors(color_list=[])
176
- @colors = color_list
177
- end
178
179
 
179
180
  # You can set a theme manually. Assign a hash to this method before you send your data.
180
181
  #
@@ -204,9 +205,9 @@ module Gruff
204
205
  @marker_color = options[:marker_color]
205
206
  @additional_line_colors = options[:additional_line_colors]
206
207
  if not options[:background_colors].nil?
207
- @base_image = render_gradiated_background *options[:background_colors]
208
+ @base_image = render_gradiated_background(*options[:background_colors])
208
209
  else
209
- @base_image = render_image_background *options[:background_image]
210
+ @base_image = render_image_background(*options[:background_image])
210
211
  end
211
212
  end
212
213
 
@@ -223,6 +224,7 @@ module Gruff
223
224
  @colors << colorname
224
225
  end
225
226
 
227
+
226
228
  # Replace the entire color list with a new array of colors. You need to have one more color
227
229
  # than the number of datasets you intend to draw. Also aliased as the colors= setter method.
228
230
  #
@@ -231,6 +233,7 @@ module Gruff
231
233
  def replace_colors(color_list=[])
232
234
  @colors = color_list
233
235
  end
236
+
234
237
 
235
238
  # A color scheme similar to the popular presentation software.
236
239
  def theme_keynote
@@ -375,11 +378,13 @@ protected
375
378
  make_stacked if @stacked
376
379
  setup_drawing()
377
380
 
378
- debug { @d.rectangle( LEFT_MARGIN, TOP_MARGIN,
379
- @raw_columns - RIGHT_MARGIN, @raw_rows - BOTTOM_MARGIN) }
380
-
381
- # Subclasses will do some drawing here...
382
- #@d.draw(@base_image)
381
+ debug do
382
+ # Outer margin
383
+ @d.rectangle( LEFT_MARGIN, TOP_MARGIN,
384
+ @raw_columns - RIGHT_MARGIN, @raw_rows - BOTTOM_MARGIN)
385
+ # Graph area box
386
+ @d.rectangle( @graph_left, @graph_top, @graph_right, @graph_bottom)
387
+ end
383
388
  end
384
389
 
385
390
  # Draws the decorations.
@@ -395,7 +400,7 @@ protected
395
400
 
396
401
  normalize()
397
402
  setup_graph_measurements()
398
- sort_norm_data() # Sort norm_data with avg largest values set first (for display)
403
+ sort_norm_data() if @sort # Sort norm_data with avg largest values set first (for display)
399
404
 
400
405
  draw_legend()
401
406
  draw_line_markers()
@@ -440,15 +445,22 @@ protected
440
445
  @graph_right_margin,
441
446
  @graph_bottom_margin) = [LEFT_MARGIN, RIGHT_MARGIN, BOTTOM_MARGIN]
442
447
  else
443
- @graph_left = LEFT_MARGIN +
444
- calculate_width(@marker_font_size, label(@maximum_value.to_f)) +
445
- LABEL_MARGIN * 2 +
446
- (@y_axis_label.nil? ? 0.0 : @marker_caps_height + LABEL_MARGIN * 2)
448
+ # Shift graph if left line numbers are hidden
449
+ line_number_width = @hide_line_numbers ?
450
+ 0.0 :
451
+ (calculate_width(@marker_font_size, label(@maximum_value.to_f)) + LABEL_MARGIN * 2)
452
+
453
+ @graph_left = LEFT_MARGIN +
454
+ line_number_width +
455
+ (@y_axis_label.nil? ? 0.0 : @marker_caps_height + LABEL_MARGIN * 2)
447
456
  # Make space for half the width of the rightmost column label.
448
457
  # Might be greater than the number of columns if between-style bar markers are used.
449
458
  last_label = @labels.keys.sort.last.to_i
450
- @graph_right_margin = RIGHT_MARGIN +
451
- (last_label >= (@column_count-1) ? calculate_width(@marker_font_size, @labels[last_label])/2.0 : 0)
459
+ extra_room_for_long_label = (last_label >= (@column_count-1) && @center_labels_over_point) ?
460
+ calculate_width(@marker_font_size, @labels[last_label])/2.0 :
461
+ 0
462
+ @graph_right_margin = RIGHT_MARGIN + extra_room_for_long_label
463
+
452
464
  @graph_bottom_margin = BOTTOM_MARGIN +
453
465
  @marker_caps_height + LABEL_MARGIN
454
466
  end
@@ -541,17 +553,19 @@ protected
541
553
 
542
554
  marker_label = index * @increment + @minimum_value.to_f
543
555
 
544
- @d.fill = @marker_color
545
- @d.font = @font if @font
546
- @d.stroke = 'transparent'
547
- @d.pointsize = scale_fontsize(@marker_font_size)
548
- @d.gravity = EastGravity
556
+ unless @hide_line_numbers
557
+ @d.fill = @marker_color
558
+ @d.font = @font if @font
559
+ @d.stroke = 'transparent'
560
+ @d.pointsize = scale_fontsize(@marker_font_size)
561
+ @d.gravity = EastGravity
549
562
 
550
- # Vertically center with 1.0 for the height
551
- @d = @d.annotate_scaled( @base_image,
552
- @graph_left - LABEL_MARGIN, 1.0,
553
- 0.0, y,
554
- label(marker_label), @scale)
563
+ # Vertically center with 1.0 for the height
564
+ @d = @d.annotate_scaled( @base_image,
565
+ @graph_left - LABEL_MARGIN, 1.0,
566
+ 0.0, y,
567
+ label(marker_label), @scale)
568
+ end
555
569
  end
556
570
 
557
571
  # Submitted by a contibutor...the utility escapes me
data/lib/gruff/line.rb CHANGED
@@ -31,6 +31,7 @@ class Gruff::Line < Gruff::Base
31
31
 
32
32
  @hide_dots = @hide_lines = false
33
33
  @baseline_color = 'red'
34
+ @baseline_value = nil
34
35
  end
35
36
 
36
37
  def draw
data/lib/gruff/net.rb CHANGED
@@ -4,6 +4,15 @@ require File.dirname(__FILE__) + '/base'
4
4
  # Experimental!!! See also the Spider graph.
5
5
  class Gruff::Net < Gruff::Base
6
6
 
7
+ # Hide parts of the graph to fit more datapoints, or for a different appearance.
8
+ attr_accessor :hide_dots
9
+
10
+ def initialize(*args)
11
+ super
12
+
13
+ @hide_dots = false
14
+ end
15
+
7
16
  def draw
8
17
 
9
18
  super
@@ -121,13 +130,13 @@ private
121
130
 
122
131
  end
123
132
 
124
-
125
- class Float
126
- # Used for degree => radian conversions
127
- def deg2rad
128
- self * (Math::PI/180.0)
129
- end
130
- end
133
+ # # This method is already in Float
134
+ # class Float
135
+ # # Used for degree => radian conversions
136
+ # def deg2rad
137
+ # self * (Math::PI/180.0)
138
+ # end
139
+ # end
131
140
 
132
141
 
133
142
 
@@ -16,7 +16,7 @@ class Gruff::PhotoBar < Gruff::Base
16
16
  #
17
17
 
18
18
  # The name of a pre-packaged photo-based theme.
19
- attr_accessor :theme
19
+ attr_reader :theme
20
20
 
21
21
  # def initialize(target_width=800)
22
22
  # super
data/lib/gruff/scene.rb CHANGED
@@ -33,7 +33,7 @@ class Gruff::Scene < Gruff::Base
33
33
  #
34
34
  # g.layers = %w(sky clouds buildings street people)
35
35
  #
36
- attr_accessor :layers
36
+ attr_reader :layers
37
37
 
38
38
  def initialize(target_width, base_dir)
39
39
  @base_dir = base_dir
data/lib/gruff/spider.rb CHANGED
@@ -7,9 +7,9 @@ require File.dirname(__FILE__) + '/base'
7
7
  class Gruff::Spider < Gruff::Base
8
8
 
9
9
  # Hide all text
10
- attr_accessor :hide_text
10
+ attr_reader :hide_text
11
11
  attr_accessor :hide_axes
12
- attr_accessor :transparent_background
12
+ attr_reader :transparent_background
13
13
 
14
14
  def transparent_background=(value)
15
15
  @transparent_background = value
File without changes
File without changes
File without changes
File without changes
@@ -58,7 +58,7 @@ class TestGruffLine < GruffTestCase
58
58
  end
59
59
 
60
60
 
61
- def test_fix_hang
61
+ def test_should_not_hang_with_0_0_100
62
62
  g = Gruff::Line.new(320)
63
63
  g.title = "Hang Value Graph Test"
64
64
  g.data('test', [0,0,100])
@@ -66,15 +66,16 @@ class TestGruffLine < GruffTestCase
66
66
  g.write("test/output/line_hang_value.png")
67
67
  end
68
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
69
+ # TODO
70
+ # def test_fix_crash
71
+ # g = Gruff::Line.new(370)
72
+ # g.title = "Crash Test"
73
+ # g.data "ichi", [5]
74
+ # g.data "ni", [0]
75
+ # g.data "san", [0]
76
+ # g.data "shi", [0]
77
+ # g.write("test/output/line_crash_fix_test.png")
78
+ # end
78
79
 
79
80
 
80
81
  def test_line_small_values
@@ -424,6 +425,16 @@ class TestGruffLine < GruffTestCase
424
425
  g.write('test/output/line_many_numbers.png')
425
426
  end
426
427
 
428
+ def test_no_hide_line_no_labels
429
+ g = Gruff::Line.new
430
+ g.title = "No Hide Line No Labels"
431
+ @datasets.each do |data|
432
+ g.data(data[0], data[1])
433
+ end
434
+ g.hide_line_markers = false
435
+ g.write('test/output/line_no_hide.png')
436
+ end
437
+
427
438
  protected
428
439
 
429
440
  # TODO Reset data after each theme
File without changes
@@ -0,0 +1,41 @@
1
+ #!/usr/bin/ruby
2
+
3
+ require File.dirname(__FILE__) + "/gruff_test_case"
4
+
5
+ class TestGruffPhotoBar < GruffTestCase
6
+
7
+ # def setup
8
+ # @datasets = [
9
+ # [:Jimmy, [25, 36, 86, 39]],
10
+ # [:Charles, [80, 54, 67, 54]],
11
+ # # [:Charity, [0, nil, 100, 90]],
12
+ # ]
13
+ # end
14
+ #
15
+ # def test_bar_graph
16
+ # bar_graph_sized
17
+ # bar_graph_sized(400)
18
+ # end
19
+ #
20
+ #
21
+ # protected
22
+ #
23
+ # def bar_graph_sized(size=800)
24
+ # g = Gruff::PhotoBar.new(size)
25
+ # g.title = "Photo Bar Graph Test #{size}px"
26
+ # g.labels = {
27
+ # 0 => '5/6',
28
+ # 1 => '5/15',
29
+ # 2 => '5/24',
30
+ # 3 => '5/30',
31
+ # }
32
+ # @datasets.each do |data|
33
+ # g.data(*data)
34
+ # end
35
+ #
36
+ # g.theme = 'plastik'
37
+ #
38
+ # g.write("test/output/photo_plastik_#{size}.png")
39
+ # end
40
+
41
+ end