gruff 0.8.0-java → 0.9.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +4 -4
  2. data/.github/ISSUE_TEMPLATE.md +18 -0
  3. data/.gitignore +3 -0
  4. data/.rubocop.yml +93 -0
  5. data/.rubocop_todo.yml +23 -810
  6. data/.travis.yml +4 -4
  7. data/.yardopts +1 -0
  8. data/CHANGELOG.md +22 -0
  9. data/Gemfile +3 -1
  10. data/README.md +44 -21
  11. data/Rakefile +2 -206
  12. data/docker/Dockerfile +14 -0
  13. data/docker/build.sh +4 -0
  14. data/docker/launch.sh +4 -0
  15. data/gruff.gemspec +11 -8
  16. data/init.rb +2 -0
  17. data/lib/gruff.rb +23 -0
  18. data/lib/gruff/accumulator_bar.rb +6 -6
  19. data/lib/gruff/area.rb +13 -17
  20. data/lib/gruff/bar.rb +58 -41
  21. data/lib/gruff/base.rb +243 -566
  22. data/lib/gruff/bezier.rb +12 -14
  23. data/lib/gruff/bullet.rb +39 -57
  24. data/lib/gruff/dot.rb +25 -59
  25. data/lib/gruff/{bar_conversion.rb → helper/bar_conversion.rb} +13 -12
  26. data/lib/gruff/helper/bar_value_label_mixin.rb +30 -0
  27. data/lib/gruff/{stacked_mixin.rb → helper/stacked_mixin.rb} +7 -6
  28. data/lib/gruff/line.rb +95 -177
  29. data/lib/gruff/mini/bar.rb +6 -7
  30. data/lib/gruff/mini/legend.rb +16 -32
  31. data/lib/gruff/mini/pie.rb +6 -7
  32. data/lib/gruff/mini/side_bar.rb +4 -5
  33. data/lib/gruff/net.rb +37 -65
  34. data/lib/gruff/patch/rmagick.rb +33 -0
  35. data/lib/gruff/patch/string.rb +8 -0
  36. data/lib/gruff/photo_bar.rb +19 -19
  37. data/lib/gruff/pie.rb +22 -73
  38. data/lib/gruff/renderer/bezier.rb +21 -0
  39. data/lib/gruff/renderer/circle.rb +21 -0
  40. data/lib/gruff/renderer/dash_line.rb +22 -0
  41. data/lib/gruff/renderer/dot.rb +39 -0
  42. data/lib/gruff/renderer/ellipse.rb +21 -0
  43. data/lib/gruff/renderer/line.rb +34 -0
  44. data/lib/gruff/renderer/polygon.rb +23 -0
  45. data/lib/gruff/renderer/polyline.rb +21 -0
  46. data/lib/gruff/renderer/rectangle.rb +19 -0
  47. data/lib/gruff/renderer/renderer.rb +127 -0
  48. data/lib/gruff/renderer/text.rb +42 -0
  49. data/lib/gruff/scatter.rb +85 -156
  50. data/lib/gruff/scene.rb +22 -30
  51. data/lib/gruff/side_bar.rb +62 -58
  52. data/lib/gruff/side_stacked_bar.rb +47 -43
  53. data/lib/gruff/spider.rb +19 -36
  54. data/lib/gruff/stacked_area.rb +17 -21
  55. data/lib/gruff/stacked_bar.rb +50 -24
  56. data/lib/gruff/store/base_data.rb +34 -0
  57. data/lib/gruff/store/custom_data.rb +34 -0
  58. data/lib/gruff/store/store.rb +80 -0
  59. data/lib/gruff/store/xy_data.rb +55 -0
  60. data/lib/gruff/themes.rb +3 -3
  61. data/lib/gruff/version.rb +3 -1
  62. metadata +41 -30
  63. data/Manifest.txt +0 -81
  64. data/assets/bubble.png +0 -0
  65. data/assets/city_scene/background/0000.png +0 -0
  66. data/assets/city_scene/background/0600.png +0 -0
  67. data/assets/city_scene/background/2000.png +0 -0
  68. data/assets/city_scene/clouds/cloudy.png +0 -0
  69. data/assets/city_scene/clouds/partly_cloudy.png +0 -0
  70. data/assets/city_scene/clouds/stormy.png +0 -0
  71. data/assets/city_scene/grass/default.png +0 -0
  72. data/assets/city_scene/haze/true.png +0 -0
  73. data/assets/city_scene/number_sample/1.png +0 -0
  74. data/assets/city_scene/number_sample/2.png +0 -0
  75. data/assets/city_scene/number_sample/default.png +0 -0
  76. data/assets/city_scene/sky/0000.png +0 -0
  77. data/assets/city_scene/sky/0200.png +0 -0
  78. data/assets/city_scene/sky/0400.png +0 -0
  79. data/assets/city_scene/sky/0600.png +0 -0
  80. data/assets/city_scene/sky/0800.png +0 -0
  81. data/assets/city_scene/sky/1000.png +0 -0
  82. data/assets/city_scene/sky/1200.png +0 -0
  83. data/assets/city_scene/sky/1400.png +0 -0
  84. data/assets/city_scene/sky/1500.png +0 -0
  85. data/assets/city_scene/sky/1700.png +0 -0
  86. data/assets/city_scene/sky/2000.png +0 -0
  87. data/assets/pc306715.jpg +0 -0
  88. data/lib/gruff/deprecated.rb +0 -38
@@ -1,20 +1,22 @@
1
1
  # -*- encoding: utf-8 -*-
2
- lib = File.expand_path('../lib', __FILE__)
2
+ # frozen_string_literal: true
3
+
4
+ lib = File.expand_path('lib')
3
5
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
6
  require 'date'
5
7
  require 'gruff/version'
6
8
 
7
9
  Gem::Specification.new do |s|
8
- s.name = %q(gruff)
10
+ s.name = 'gruff'
9
11
  s.version = Gruff::VERSION
10
12
  s.authors = ['Geoffrey Grosenbach', 'Uwe Kubosch']
11
13
  s.date = Date.today.to_s
12
- s.description = %q(Beautiful graphs for one or multiple datasets. Can be used on websites or in documents.)
13
- s.email = %q(boss@topfunky.com)
14
+ s.description = 'Beautiful graphs for one or multiple datasets. Can be used on websites or in documents.'
15
+ s.email = 'boss@topfunky.com'
14
16
  s.files = `git ls-files`.split($/).reject { |f| f =~ /^test/ }
15
- s.homepage = %q(https://github.com/topfunky/gruff)
17
+ s.homepage = 'https://github.com/topfunky/gruff'
16
18
  s.require_paths = %w[lib]
17
- s.summary = %q(Beautiful graphs for one or multiple datasets.)
19
+ s.summary = 'Beautiful graphs for one or multiple datasets.'
18
20
  s.license = 'MIT'
19
21
  s.test_files = s.files.grep(%r{^(test|spec|features)/})
20
22
  s.executables = s.files.grep(%r{^bin/}).map { |f| File.basename(f) }
@@ -29,6 +31,7 @@ Gem::Specification.new do |s|
29
31
  end
30
32
  s.required_ruby_version = '>= 1.9.3'
31
33
 
32
- s.add_development_dependency('rake')
33
- s.add_development_dependency('minitest-reporters')
34
+ s.add_development_dependency 'rake'
35
+ s.add_development_dependency 'minitest-reporters'
36
+ s.add_development_dependency 'yard', '~> 0.9.25'
34
37
  end
data/init.rb CHANGED
@@ -1,2 +1,4 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # For Rails
2
4
  require 'gruff'
@@ -1,8 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rmagick'
1
4
  require 'gruff/version'
2
5
 
3
6
  # Extra full path added to fix loading errors on some installations.
4
7
 
5
8
  %w[
9
+ patch/rmagick
10
+ patch/string
11
+
6
12
  themes
7
13
  base
8
14
  area
@@ -23,6 +29,23 @@ require 'gruff/version'
23
29
 
24
30
  scene
25
31
 
32
+ renderer/renderer
33
+ renderer/rectangle
34
+ renderer/circle
35
+ renderer/dash_line
36
+ renderer/line
37
+ renderer/polyline
38
+ renderer/polygon
39
+ renderer/bezier
40
+ renderer/ellipse
41
+ renderer/dot
42
+ renderer/text
43
+
44
+ store/store
45
+ store/base_data
46
+ store/custom_data
47
+ store/xy_data
48
+
26
49
  mini/legend
27
50
  mini/bar
28
51
  mini/pie
@@ -1,18 +1,18 @@
1
- require File.dirname(__FILE__) + '/base'
1
+ # frozen_string_literal: true
2
+
3
+ require 'gruff/base'
2
4
 
3
- ##
4
5
  # A special bar graph that shows a single dataset as a set of
5
6
  # stacked bars. The bottom bar shows the running total and
6
7
  # the top bar shows the new value being added to the array.
7
-
8
8
  class Gruff::AccumulatorBar < Gruff::StackedBar
9
9
  def draw
10
- raise(Gruff::IncorrectNumberOfDatasetsException) unless @data.length == 1
10
+ raise(Gruff::IncorrectNumberOfDatasetsException) unless store.length == 1
11
11
 
12
- accum_array = @data.first[DATA_VALUES_INDEX][0..-2].inject([0]) { |a, v| a << a.last + v }
12
+ accum_array = store.data.first.points[0..-2].reduce([0]) { |a, v| a << a.last + v }
13
13
  data 'Accumulator', accum_array
14
14
  set_colors
15
- @data.reverse!
15
+ store.reverse!
16
16
  super
17
17
  end
18
18
  end
@@ -1,36 +1,33 @@
1
- require File.dirname(__FILE__) + '/base'
1
+ # frozen_string_literal: true
2
+
3
+ require 'gruff/base'
2
4
 
3
5
  class Gruff::Area < Gruff::Base
4
- def initialize(*)
6
+ def initialize_ivars
5
7
  super
6
8
  @sorted_drawing = true
7
9
  end
10
+ private :initialize_ivars
8
11
 
9
12
  def draw
10
13
  super
11
14
 
12
- return unless @has_data
15
+ return unless data_given?
13
16
 
14
- @x_increment = @graph_width / (@column_count - 1).to_f
15
- @d = @d.stroke 'transparent'
17
+ x_increment = @graph_width / (column_count - 1).to_f
16
18
 
17
- @norm_data.each do |data_row|
18
- poly_points = Array.new
19
- prev_x = prev_y = 0.0
20
- @d = @d.fill data_row[DATA_COLOR_INDEX]
19
+ store.norm_data.each do |data_row|
20
+ poly_points = []
21
21
 
22
- data_row[DATA_VALUES_INDEX].each_with_index do |data_point, index|
22
+ data_row.points.each_with_index do |data_point, index|
23
23
  # Use incremented x and scaled y
24
- new_x = @graph_left + (@x_increment * index)
24
+ new_x = @graph_left + (x_increment * index)
25
25
  new_y = @graph_top + (@graph_height - data_point * @graph_height)
26
26
 
27
27
  poly_points << new_x
28
28
  poly_points << new_y
29
29
 
30
30
  draw_label(new_x, index)
31
-
32
- prev_x = new_x
33
- prev_y = new_y
34
31
  end
35
32
 
36
33
  # Add closing points, draw polygon
@@ -39,10 +36,9 @@ class Gruff::Area < Gruff::Base
39
36
  poly_points << @graph_left
40
37
  poly_points << @graph_bottom - 1
41
38
 
42
- @d = @d.polyline(*poly_points)
39
+ Gruff::Renderer::Polygon.new(color: data_row.color).render(poly_points)
43
40
  end
44
41
 
45
- @d.draw(@base_image)
42
+ Gruff::Renderer.finish
46
43
  end
47
-
48
44
  end
@@ -1,24 +1,39 @@
1
- require File.dirname(__FILE__) + '/base'
2
- require File.dirname(__FILE__) + '/bar_conversion'
1
+ # frozen_string_literal: true
3
2
 
4
- class Gruff::Bar < Gruff::Base
3
+ require 'gruff/base'
4
+ require 'gruff/helper/bar_conversion'
5
5
 
6
- # Spacing factor applied between bars
6
+ class Gruff::Bar < Gruff::Base
7
+ # Spacing factor applied between bars.
7
8
  attr_accessor :bar_spacing
8
9
 
9
- def initialize(*args)
10
+ # Spacing factor applied between a group of bars belonging to the same label.
11
+ attr_accessor :group_spacing
12
+
13
+ # Set the number output format for labels using sprintf.
14
+ # Default is +"%.2f"+.
15
+ attr_accessor :label_formatting
16
+
17
+ # Output the values for the bars on a bar graph.
18
+ # Default is +false+.
19
+ attr_accessor :show_labels_for_bar_values
20
+
21
+ def initialize_ivars
10
22
  super
11
23
  @spacing_factor = 0.9
24
+ @label_formatting = nil
25
+ @show_labels_for_bar_values = false
12
26
  end
27
+ private :initialize_ivars
13
28
 
14
29
  def draw
15
30
  # Labels will be centered over the left of the bar if
16
31
  # there are more labels than columns. This is basically the same
17
32
  # as where it would be for a line graph.
18
- @center_labels_over_point = (@labels.keys.length > @column_count)
33
+ @center_labels_over_point = (@labels.keys.length > column_count)
19
34
 
20
35
  super
21
- return unless @has_data
36
+ return unless data_given?
22
37
 
23
38
  draw_bars
24
39
  end
@@ -28,9 +43,9 @@ class Gruff::Bar < Gruff::Base
28
43
  # and 1 means that each bars' width is nearly 0 (so each bar is a simple
29
44
  # line with no x dimension).
30
45
  #
31
- # Default value is 0.9.
46
+ # Default value is +0.9+.
32
47
  def spacing_factor=(space_percent)
33
- raise ArgumentError, 'spacing_factor must be between 0.00 and 1.00' unless space_percent >= 0 and space_percent <= 1
48
+ raise ArgumentError, 'spacing_factor must be between 0.00 and 1.00' unless (space_percent >= 0) && (space_percent <= 1)
34
49
 
35
50
  @spacing_factor = (1 - space_percent)
36
51
  end
@@ -42,66 +57,68 @@ protected
42
57
  #
43
58
  # Columns sit side-by-side.
44
59
  @bar_spacing ||= @spacing_factor # space between the bars
45
- @bar_width = @graph_width / (@column_count * @data.length).to_f
46
- padding = (@bar_width * (1 - @bar_spacing)) / 2
60
+ @group_spacing ||= 10
47
61
 
48
- @d = @d.stroke_opacity 0.0
62
+ bar_width = (@graph_width - calculate_spacing) / (column_count * store.length).to_f
63
+ padding = (bar_width * (1 - @bar_spacing)) / 2
49
64
 
50
65
  # Setup the BarConversion Object
51
- conversion = Gruff::BarConversion.new()
66
+ conversion = Gruff::BarConversion.new
52
67
  conversion.graph_height = @graph_height
53
68
  conversion.graph_top = @graph_top
54
69
 
55
70
  # Set up the right mode [1,2,3] see BarConversion for further explanation
56
- if @minimum_value >= 0
57
- # all bars go from zero to positiv
71
+ if minimum_value >= 0
72
+ # all bars go from zero to positive
58
73
  conversion.mode = 1
74
+ elsif maximum_value <= 0
75
+ # all bars go from 0 to negative
76
+ conversion.mode = 2
59
77
  else
60
- # all bars go from 0 to negativ
61
- if @maximum_value <= 0
62
- conversion.mode = 2
63
- else
64
- # bars either go from zero to negativ or to positiv
65
- conversion.mode = 3
66
- conversion.spread = @spread
67
- conversion.minimum_value = @minimum_value
68
- conversion.zero = -@minimum_value / @spread
69
- end
78
+ # bars either go from zero to negative or to positive
79
+ conversion.mode = 3
80
+ conversion.spread = @spread
81
+ conversion.minimum_value = minimum_value
82
+ conversion.zero = -minimum_value / @spread
70
83
  end
71
84
 
72
85
  # iterate over all normalised data
73
- @norm_data.each_with_index do |data_row, row_index|
74
- data_row[DATA_VALUES_INDEX].each_with_index do |data_point, point_index|
86
+ store.norm_data.each_with_index do |data_row, row_index|
87
+ data_row.points.each_with_index do |data_point, point_index|
88
+ group_spacing = @group_spacing * @scale * point_index
89
+
75
90
  # Use incremented x and scaled y
76
91
  # x
77
- left_x = @graph_left + (@bar_width * (row_index + point_index + ((@data.length - 1) * point_index))) + padding
78
- right_x = left_x + @bar_width * @bar_spacing
92
+ left_x = @graph_left + (bar_width * (row_index + point_index + ((store.length - 1) * point_index))) + padding + group_spacing
93
+ right_x = left_x + bar_width * @bar_spacing
79
94
  # y
80
- conv = []
81
- conversion.get_left_y_right_y_scaled(data_point, conv)
95
+ left_y, right_y = conversion.get_left_y_right_y_scaled(data_point)
82
96
 
83
97
  # create new bar
84
- @d = @d.fill data_row[DATA_COLOR_INDEX]
85
- @d = @d.rectangle(left_x, conv[0], right_x, conv[1])
98
+ rect_renderer = Gruff::Renderer::Rectangle.new(color: data_row.color)
99
+ rect_renderer.render(left_x, left_y, right_x, right_y)
86
100
 
87
101
  # Calculate center based on bar_width and current row
88
- label_center = @graph_left +
89
- (@data.length * @bar_width * point_index) +
90
- (@data.length * @bar_width / 2.0)
102
+ label_center = @graph_left + group_spacing + (store.length * bar_width * point_index) + (store.length * bar_width / 2.0)
91
103
 
92
104
  # Subtract half a bar width to center left if requested
93
- draw_label(label_center - (@center_labels_over_point ? @bar_width / 2.0 : 0.0), point_index)
105
+ draw_label(label_center, point_index)
94
106
  if @show_labels_for_bar_values
95
- val = (@label_formatting || '%.2f') % @norm_data[row_index][3][point_index]
96
- draw_value_label(left_x + (right_x - left_x) / 2, conv[0] - 30, val.commify, true)
107
+ raw_value = store.data[row_index].points[point_index]
108
+ val = (@label_formatting || '%.2f') % raw_value
109
+ y = raw_value >= 0 ? left_y - 30 : left_y + 12
110
+ draw_value_label(left_x + (right_x - left_x) / 2, y, val.commify, true)
97
111
  end
98
112
  end
99
113
  end
100
114
 
101
115
  # Draw the last label if requested
102
- draw_label(@graph_right, @column_count) if @center_labels_over_point
116
+ draw_label(@graph_right, column_count, Magick::NorthWestGravity) if @center_labels_over_point
103
117
 
104
- @d.draw(@base_image)
118
+ Gruff::Renderer.finish
105
119
  end
106
120
 
121
+ def calculate_spacing
122
+ @scale * @group_spacing * (column_count - 1)
123
+ end
107
124
  end
@@ -1,9 +1,8 @@
1
- require 'rubygems'
1
+ # frozen_string_literal: true
2
+
2
3
  require 'rmagick'
3
4
  require 'bigdecimal'
4
5
 
5
- require File.dirname(__FILE__) + '/deprecated'
6
-
7
6
  ##
8
7
  # = Gruff. Graphs.
9
8
  #
@@ -16,48 +15,34 @@ require File.dirname(__FILE__) + '/deprecated'
16
15
  # David Stokar, Paul Rogers, Dave Woodward, Frank Oxener, Kevin Clark, Cies
17
16
  # Breijs, Richard Cowin, and a cast of thousands.
18
17
  #
19
- # See Gruff::Base#theme= for setting themes.
20
-
18
+ # See {Gruff::Base#theme=} for setting themes.
21
19
  module Gruff
22
20
  class Base
23
-
24
- include Magick
25
- include Deprecated
26
-
27
- # Draw extra lines showing where the margins and text centers are
28
- DEBUG = false
29
-
30
- # Used for navigating the array of data to plot
31
- DATA_LABEL_INDEX = 0
32
- DATA_VALUES_INDEX = 1
33
- DATA_COLOR_INDEX = 2
34
- DATA_VALUES_X_INDEX = 3
35
-
36
- # Space around text elements. Mostly used for vertical spacing
21
+ # Space around text elements. Mostly used for vertical spacing.
37
22
  LEGEND_MARGIN = TITLE_MARGIN = 20.0
38
23
  LABEL_MARGIN = 10.0
39
24
  DEFAULT_MARGIN = 20.0
40
25
 
41
- DEFAULT_TARGET_WIDTH = 800
26
+ DEFAULT_TARGET_WIDTH = 800.0
42
27
 
43
28
  THOUSAND_SEPARATOR = ','
44
29
 
45
- # Blank space above the graph
30
+ # Blank space above the graph.
46
31
  attr_accessor :top_margin
47
32
 
48
- # Blank space below the graph
33
+ # Blank space below the graph.
49
34
  attr_accessor :bottom_margin
50
35
 
51
- # Blank space to the right of the graph
36
+ # Blank space to the right of the graph.
52
37
  attr_accessor :right_margin
53
38
 
54
- # Blank space to the left of the graph
39
+ # Blank space to the left of the graph.
55
40
  attr_accessor :left_margin
56
41
 
57
- # Blank space below the title
42
+ # Blank space below the title.
58
43
  attr_accessor :title_margin
59
44
 
60
- # Blank space below the legend
45
+ # Blank space below the legend.
61
46
  attr_accessor :legend_margin
62
47
 
63
48
  # A hash of names for the individual columns, where the key is the array
@@ -65,7 +50,8 @@ module Gruff
65
50
  #
66
51
  # Not all columns need to be named.
67
52
  #
68
- # Example: 0 => 2005, 3 => 2006, 5 => 2007, 7 => 2008
53
+ # @example
54
+ # { 0 => 2005, 3 => 2006, 5 => 2007, 7 => 2008 }
69
55
  attr_accessor :labels
70
56
 
71
57
  # Used internally for spacing.
@@ -76,46 +62,40 @@ module Gruff
76
62
  # Used internally for horizontal graph types.
77
63
  attr_accessor :has_left_labels
78
64
 
79
- # A label for the bottom of the graph
65
+ # A label for the bottom of the graph.
80
66
  attr_accessor :x_axis_label
81
67
 
82
- # A label for the left side of the graph
68
+ # A label for the left side of the graph.
83
69
  attr_accessor :y_axis_label
84
70
 
85
- # Manually set increment of the vertical marking lines
71
+ # Manually set increment of the vertical marking lines.
86
72
  attr_accessor :x_axis_increment
87
73
 
88
- # Manually set increment of the horizontal marking lines
74
+ # Manually set increment of the horizontal marking lines.
89
75
  attr_accessor :y_axis_increment
90
76
 
91
- # Height of staggering between labels (Bar graph only)
77
+ # Height of staggering between labels (Bar graph only).
92
78
  attr_accessor :label_stagger_height
93
79
 
94
- # Truncates labels if longer than max specified
80
+ # Truncates labels if longer than max specified.
95
81
  attr_accessor :label_max_size
96
82
 
97
- # How truncated labels visually appear if they exceed label_max_size
98
- # :absolute - does not show trailing dots to indicate truncation. This is
99
- # the default.
100
- # :trailing_dots - shows trailing dots to indicate truncation (note
101
- # that label_max_size must be greater than 3).
83
+ # How truncated labels visually appear if they exceed {#label_max_size}.
84
+ #
85
+ # - +:absolute+ - does not show trailing dots to indicate truncation. This is the default.
86
+ # - +:trailing_dots+ - shows trailing dots to indicate truncation (note that {#label_max_size}
87
+ # must be greater than 3).
102
88
  attr_accessor :label_truncation_style
103
89
 
104
90
  # Get or set the list of colors that will be used to draw the bars or lines.
105
91
  attr_accessor :colors
106
92
 
107
- # The large title of the graph displayed at the top
93
+ # The large title of the graph displayed at the top.
108
94
  attr_accessor :title
109
95
 
110
96
  # Font used for titles, labels, etc. Works best if you provide the full
111
97
  # path to the TTF font file. RMagick must be built with the Freetype
112
98
  # libraries for this to work properly.
113
- #
114
- # Tries to find Bitstream Vera (Vera.ttf) in the location specified by
115
- # ENV['MAGICK_FONT_PATH']. Uses default RMagick font otherwise.
116
- #
117
- # The font= method below fulfills the role of the writer, so we only need
118
- # a reader here.
119
99
  attr_reader :font
120
100
 
121
101
  # Same as font but for the title.
@@ -126,57 +106,44 @@ module Gruff
126
106
 
127
107
  attr_accessor :font_color
128
108
 
129
- # Prevent drawing of line markers
109
+ # Prevent drawing of line markers.
130
110
  attr_accessor :hide_line_markers
131
111
 
132
- # Prevent drawing of the legend
112
+ # Prevent drawing of the legend.
133
113
  attr_accessor :hide_legend
134
114
 
135
- # Prevent drawing of the title
115
+ # Prevent drawing of the title.
136
116
  attr_accessor :hide_title
137
117
 
138
- # Prevent drawing of line numbers
118
+ # Prevent drawing of line numbers.
139
119
  attr_accessor :hide_line_numbers
140
120
 
141
121
  # Message shown when there is no data. Fits up to 20 characters. Defaults
142
- # to "No Data."
122
+ # to +"No Data."+.
143
123
  attr_accessor :no_data_message
144
124
 
145
- # The font size of the large title at the top of the graph
125
+ # The font size of the large title at the top of the graph.
146
126
  attr_accessor :title_font_size
147
127
 
148
128
  # Optionally set the size of the font. Based on an 800x600px graph.
149
- # Default is 20.
129
+ # Default is +20+.
150
130
  #
151
131
  # Will be scaled down if the graph is smaller than 800px wide.
152
132
  attr_accessor :legend_font_size
153
133
 
154
- # Display the legend under the graph
134
+ # Display the legend under the graph.
155
135
  attr_accessor :legend_at_bottom
156
136
 
157
- # The font size of the labels around the graph
137
+ # The font size of the labels around the graph.
158
138
  attr_accessor :marker_font_size
159
139
 
160
- # The color of the auxiliary lines
140
+ # The color of the auxiliary lines.
161
141
  attr_accessor :marker_color
162
142
  attr_accessor :marker_shadow_color
163
143
 
164
- # The number of horizontal lines shown for reference
144
+ # The number of horizontal lines shown for reference.
165
145
  attr_accessor :marker_count
166
146
 
167
- # You can manually set a minimum value instead of having the values
168
- # guessed for you.
169
- #
170
- # Set it after you have given all your data to the graph object.
171
- attr_accessor :minimum_value
172
-
173
- # You can manually set a maximum value, such as a percentage-based graph
174
- # that always goes to 100.
175
- #
176
- # If you use this, you must set it after you have given all your data to
177
- # the graph object.
178
- attr_accessor :maximum_value
179
-
180
147
  # Set to true if you want the data sets sorted with largest avg values drawn
181
148
  # first.
182
149
  attr_accessor :sort
@@ -188,47 +155,35 @@ module Gruff
188
155
  # Experimental
189
156
  attr_accessor :additional_line_values
190
157
 
191
- # Experimental
192
- attr_accessor :stacked
193
-
194
158
  # Optionally set the size of the colored box by each item in the legend.
195
- # Default is 20.0
159
+ # Default is +20.0+.
196
160
  #
197
161
  # Will be scaled down if graph is smaller than 800px wide.
198
162
  attr_accessor :legend_box_size
199
163
 
200
- # Output the values for the bars on a bar graph
201
- # Default is false
202
- attr_accessor :show_labels_for_bar_values
203
-
204
- # Set the number output format for labels using sprintf
205
- # Default is "%.2f"
206
- attr_accessor :label_formatting
207
-
208
- # With Side Bars use the data label for the marker value to the left of the bar
209
- # Default is false
164
+ # With Side Bars use the data label for the marker value to the left of the bar.
165
+ # Default is +false+.
210
166
  attr_accessor :use_data_label
167
+
211
168
  # If one numerical argument is given, the graph is drawn at 4/3 ratio
212
169
  # according to the given width (800 results in 800x600, 400 gives 400x300,
213
170
  # etc.).
214
171
  #
215
172
  # Or, send a geometry string for other ratios ('800x400', '400x225').
216
- #
217
- # Looks for Bitstream Vera as the default font. Expects an environment var
218
- # of MAGICK_FONT_PATH to be set. (Uses RMagick's default font otherwise.)
219
173
  def initialize(target_width = DEFAULT_TARGET_WIDTH)
220
- if Numeric === target_width
221
- @columns = target_width.to_f
222
- @rows = target_width.to_f * 0.75
223
- else
174
+ if target_width.is_a?(String)
224
175
  geometric_width, geometric_height = target_width.split('x')
225
176
  @columns = geometric_width.to_f
226
177
  @rows = geometric_height.to_f
178
+ else
179
+ @columns = target_width.to_f
180
+ @rows = target_width.to_f * 0.75
227
181
  end
182
+ @columns.freeze
183
+ @rows.freeze
228
184
 
229
185
  initialize_ivars
230
186
 
231
- reset_themes
232
187
  self.theme = Themes::KEYNOTE
233
188
  end
234
189
 
@@ -240,17 +195,16 @@ module Gruff
240
195
  # developers to change this values in their program.
241
196
  def initialize_ivars
242
197
  # Internal for calculations
243
- @raw_columns = 800.0
244
- @raw_rows = 800.0 * (@rows / @columns)
245
- @column_count = 0
246
- @data = Array.new
198
+ @raw_columns = DEFAULT_TARGET_WIDTH
199
+ @raw_rows = DEFAULT_TARGET_WIDTH * (@rows / @columns)
200
+ @raw_columns.freeze
201
+ @raw_rows.freeze
202
+
247
203
  @marker_count = nil
248
204
  @maximum_value = @minimum_value = nil
249
- @has_data = false
250
205
  @increment = nil
251
- @labels = Hash.new
252
- @label_formatting = nil
253
- @labels_seen = Hash.new
206
+ @labels = {}
207
+ @labels_seen = {}
254
208
  @sort = false
255
209
  @sorted_drawing = false
256
210
  @title = nil
@@ -258,8 +212,7 @@ module Gruff
258
212
 
259
213
  @scale = @columns / @raw_columns
260
214
 
261
- vera_font_path = File.expand_path('Vera.ttf', ENV['MAGICK_FONT_PATH'])
262
- @font = File.exist?(vera_font_path) ? vera_font_path : nil
215
+ @font = nil
263
216
  @bold_title = true
264
217
 
265
218
  @marker_font_size = 21.0
@@ -274,7 +227,7 @@ module Gruff
274
227
 
275
228
  @no_data_message = 'No Data'
276
229
 
277
- @hide_line_markers = @hide_legend = @hide_title = @hide_line_numbers = @legend_at_bottom = @show_labels_for_bar_values = false
230
+ @hide_line_markers = @hide_legend = @hide_title = @hide_line_numbers = @legend_at_bottom = false
278
231
  @center_labels_over_point = true
279
232
  @has_left_labels = false
280
233
  @label_stagger_height = 0
@@ -289,9 +242,10 @@ module Gruff
289
242
  @x_axis_increment = nil
290
243
  @x_axis_label = @y_axis_label = nil
291
244
  @y_axis_increment = nil
292
- @stacked = nil
293
- @norm_data = nil
245
+
246
+ @store = Gruff::Store.new(Gruff::Store::BaseData)
294
247
  end
248
+ protected :initialize_ivars
295
249
 
296
250
  # Sets the top, bottom, left and right margins to +margin+.
297
251
  def margins=(margin)
@@ -301,46 +255,44 @@ module Gruff
301
255
  # Sets the font for graph text to the font at +font_path+.
302
256
  def font=(font_path)
303
257
  @font = font_path
304
- @d.font = @font
258
+ Gruff::Renderer.font = @font
305
259
  end
306
260
 
307
261
  # Add a color to the list of available colors for lines.
308
262
  #
309
- # Example:
310
- # add_color('#c0e9d3')
263
+ # @example
264
+ # add_color('#c0e9d3')
311
265
  def add_color(colorname)
312
266
  @colors << colorname
313
267
  end
314
268
 
315
269
  # Replace the entire color list with a new array of colors. Also
316
- # aliased as the colors= setter method.
270
+ # aliased as the {#colors=} setter method.
317
271
  #
318
272
  # If you specify fewer colors than the number of datasets you intend
319
- # to draw, 'increment_color' will cycle through the array, reusing
320
- # colors as needed.
273
+ # to draw, it will cycle through the array, reusing colors as needed.
321
274
  #
322
- # Note that (as with the 'theme' method), you should set up your color
323
- # list before you send your data (via the 'data' method). Calls to the
324
- # 'data' method made prior to this call will use whatever color scheme
275
+ # Note that (as with the {#theme=} method), you should set up your color
276
+ # list before you send your data (via the {#data} method). Calls to the
277
+ # {#data} method made prior to this call will use whatever color scheme
325
278
  # was in place at the time data was called.
326
279
  #
327
- # Example:
328
- # replace_colors ['#cc99cc', '#d9e043', '#34d8a2']
280
+ # @example
281
+ # replace_colors ['#cc99cc', '#d9e043', '#34d8a2']
329
282
  def replace_colors(color_list = [])
330
283
  @colors = color_list
331
- @color_index = 0
332
284
  end
333
285
 
334
286
  # You can set a theme manually. Assign a hash to this method before you
335
287
  # send your data.
336
288
  #
337
- # graph.theme = {
338
- # :colors => %w(orange purple green white red),
339
- # :marker_color => 'blue',
340
- # :background_colors => ['black', 'grey', :top_bottom]
341
- # }
289
+ # graph.theme = {
290
+ # colors: %w(orange purple green white red),
291
+ # marker_color: 'blue',
292
+ # background_colors: ['black', 'grey', :top_bottom]
293
+ # }
342
294
  #
343
- # :background_image => 'squirrel.png' is also possible.
295
+ # +background_image: 'squirrel.png'+ is also possible.
344
296
  #
345
297
  # (Or hopefully something better looking than that.)
346
298
  #
@@ -364,7 +316,7 @@ module Gruff
364
316
  @font_color = @theme_options[:font_color] || @marker_color
365
317
  @additional_line_colors = @theme_options[:additional_line_colors]
366
318
 
367
- render_background
319
+ Gruff::Renderer.setup(@columns, @rows, @font, @scale, @theme_options)
368
320
  end
369
321
 
370
322
  def theme_keynote
@@ -400,51 +352,47 @@ module Gruff
400
352
  # If the color argument is nil, the next color from the default theme will
401
353
  # be used.
402
354
  #
403
- # NOTE: If you want to use a preset theme, you must set it before calling
404
- # data().
355
+ # @note
356
+ # If you want to use a preset theme, you must set it before calling {#data}.
405
357
  #
406
- # Example:
358
+ # @example
407
359
  # data("Bart S.", [95, 45, 78, 89, 88, 76], '#ffcc00')
408
360
  def data(name, data_points = [], color = nil)
409
- data_points = Array(data_points) # make sure it's an array
410
- @data << [name, data_points, color]
411
- # Set column count if this is larger than previous counts
412
- @column_count = (data_points.length > @column_count) ? data_points.length : @column_count
413
-
414
- # Pre-normalize
415
- data_points.each do |data_point|
416
- next if data_point.nil?
417
-
418
- # Setup max/min so spread starts at the low end of the data points
419
- if @maximum_value.nil? && @minimum_value.nil?
420
- @maximum_value = @minimum_value = data_point
421
- end
361
+ store.add(name, data_points, color)
362
+ end
422
363
 
423
- # TODO Doesn't work with stacked bar graphs
424
- # Original: @maximum_value = larger_than_max?(data_point, index) ? max(data_point, index) : @maximum_value
425
- @maximum_value = larger_than_max?(data_point) ? data_point : @maximum_value
426
- @has_data = true if @maximum_value >= 0
364
+ # You can manually set a minimum value instead of having the values
365
+ # guessed for you.
366
+ #
367
+ # Set it after you have given all your data to the graph object.
368
+ def minimum_value
369
+ @minimum_value || store.min
370
+ end
371
+ attr_writer :minimum_value
427
372
 
428
- @minimum_value = less_than_min?(data_point) ? data_point : @minimum_value
429
- @has_data = true if @minimum_value < 0
430
- end
373
+ # You can manually set a maximum value, such as a percentage-based graph
374
+ # that always goes to 100.
375
+ #
376
+ # If you use this, you must set it after you have given all your data to
377
+ # the graph object.
378
+ def maximum_value
379
+ @maximum_value || store.max
431
380
  end
381
+ attr_writer :maximum_value
432
382
 
433
- # Writes the graph to a file. Defaults to 'graph.png'
383
+ # Writes the graph to a file. Defaults to +'graph.png'+
434
384
  #
435
- # Example:
385
+ # @example
436
386
  # write('graphs/my_pretty_graph.png')
437
- def write(filename = 'graph.png')
387
+ def write(file_name = 'graph.png')
438
388
  draw
439
- @base_image.write(filename)
389
+ Gruff::Renderer.write(file_name)
440
390
  end
441
391
 
442
392
  # Return the graph as a rendered binary blob.
443
- def to_blob(fileformat = 'PNG')
393
+ def to_blob(file_format = 'PNG')
444
394
  draw
445
- @base_image.to_blob do
446
- self.format = fileformat
447
- end
395
+ Gruff::Renderer.to_blob(file_format)
448
396
  end
449
397
 
450
398
  protected
@@ -454,7 +402,7 @@ module Gruff
454
402
  # Subclasses should start by calling super() for this method.
455
403
  def draw
456
404
  # Maybe should be done in one of the following functions for more granularity.
457
- unless @has_data
405
+ unless data_given?
458
406
  draw_no_data
459
407
  return
460
408
  end
@@ -462,14 +410,6 @@ module Gruff
462
410
  setup_data
463
411
  setup_drawing
464
412
 
465
- debug {
466
- # Outer margin
467
- @d.rectangle(@left_margin, @top_margin,
468
- @raw_columns - @right_margin, @raw_rows - @bottom_margin)
469
- # Graph area box
470
- @d.rectangle(@graph_left, @graph_top, @graph_right, @graph_bottom)
471
- }
472
-
473
413
  draw_legend
474
414
  draw_line_markers
475
415
  draw_axis_labels
@@ -479,10 +419,9 @@ module Gruff
479
419
  # Perform data manipulation before calculating chart measurements
480
420
  def setup_data # :nodoc:
481
421
  if @y_axis_increment && !@hide_line_markers
482
- @maximum_value = [@y_axis_increment, @maximum_value, (@maximum_value.to_f / @y_axis_increment).round * @y_axis_increment].max
483
- @minimum_value = [@minimum_value, (@minimum_value.to_f / @y_axis_increment).round * @y_axis_increment].min
422
+ self.maximum_value = [@y_axis_increment, maximum_value, (maximum_value.to_f / @y_axis_increment).round * @y_axis_increment].max
423
+ self.minimum_value = [minimum_value, (minimum_value.to_f / @y_axis_increment).round * @y_axis_increment].min
484
424
  end
485
- make_stacked if @stacked
486
425
  end
487
426
 
488
427
  # Calculates size of drawable area and generates normalized data.
@@ -492,6 +431,7 @@ module Gruff
492
431
  # * title
493
432
  def setup_drawing
494
433
  calculate_spread
434
+ calculate_increment
495
435
  sort_data if @sort # Sort data with avg largest values set first (for display)
496
436
  set_colors
497
437
  normalize
@@ -499,32 +439,29 @@ module Gruff
499
439
  sort_norm_data if @sorted_drawing # Sort norm_data with avg largest values set first (for display)
500
440
  end
501
441
 
502
- # Make copy of data with values scaled between 0-100
503
- def normalize(force = false)
504
- if @norm_data.nil? || force
505
- @norm_data = []
506
- return unless @has_data
507
-
508
- @data.each do |data_row|
509
- norm_data_points = []
510
- data_row[DATA_VALUES_INDEX].each do |data_point|
511
- if data_point.nil?
512
- norm_data_points << nil
513
- else
514
- norm_data_points << ((data_point.to_f - @minimum_value.to_f) / @spread)
515
- end
516
- end
517
- if @show_labels_for_bar_values
518
- @norm_data << [data_row[DATA_LABEL_INDEX], norm_data_points, data_row[DATA_COLOR_INDEX], data_row[DATA_VALUES_INDEX]]
519
- else
520
- @norm_data << [data_row[DATA_LABEL_INDEX], norm_data_points, data_row[DATA_COLOR_INDEX]]
521
- end
442
+ attr_reader :store
443
+
444
+ def data_given?
445
+ @data_given ||= begin
446
+ if store.empty?
447
+ false
448
+ else
449
+ minimum_value <= store.min || maximum_value >= store.max
522
450
  end
523
451
  end
524
452
  end
525
453
 
454
+ def column_count
455
+ store.columns
456
+ end
457
+
458
+ # Make copy of data with values scaled between 0-100
459
+ def normalize
460
+ store.normalize(minimum: minimum_value, spread: @spread)
461
+ end
462
+
526
463
  def calculate_spread # :nodoc:
527
- @spread = @maximum_value.to_f - @minimum_value.to_f
464
+ @spread = maximum_value.to_f - minimum_value.to_f
528
465
  @spread = @spread > 0 ? @spread : 1
529
466
  end
530
467
 
@@ -532,12 +469,9 @@ module Gruff
532
469
  # Calculates size of drawable area, general font dimensions, etc.
533
470
 
534
471
  def setup_graph_measurements
535
- @marker_caps_height = @hide_line_markers ? 0 :
536
- calculate_caps_height(@marker_font_size)
537
- @title_caps_height = (@hide_title || @title.nil?) ? 0 :
538
- calculate_caps_height(@title_font_size) * @title.lines.to_a.size
539
- @legend_caps_height = @hide_legend ? 0 :
540
- calculate_caps_height(@legend_font_size)
472
+ @marker_caps_height = @hide_line_markers ? 0 : calculate_caps_height(@marker_font_size)
473
+ @title_caps_height = (@hide_title || @title.nil?) ? 0 : calculate_caps_height(@title_font_size) * @title.lines.to_a.size
474
+ @legend_caps_height = @hide_legend ? 0 : calculate_caps_height(@legend_font_size)
541
475
 
542
476
  if @hide_line_markers
543
477
  @graph_left = @left_margin
@@ -546,31 +480,26 @@ module Gruff
546
480
  else
547
481
  if @has_left_labels
548
482
  longest_left_label_width = calculate_width(@marker_font_size,
549
- labels.values.inject('') { |value, memo| (value.to_s.length > memo.to_s.length) ? value : memo }) * 1.25
483
+ labels.values.reduce('') { |value, memo| (value.to_s.length > memo.to_s.length) ? value : memo }) * 1.25
550
484
  else
551
485
  longest_left_label_width = calculate_width(@marker_font_size,
552
- label(@maximum_value.to_f, @increment))
486
+ label(maximum_value.to_f, @increment))
553
487
  end
554
488
 
555
489
  # Shift graph if left line numbers are hidden
556
- line_number_width = @hide_line_numbers && !@has_left_labels ?
557
- 0.0 :
558
- (longest_left_label_width + LABEL_MARGIN * 2)
490
+ line_number_width = @hide_line_numbers && !@has_left_labels ? 0.0 : (longest_left_label_width + LABEL_MARGIN * 2)
559
491
 
560
- @graph_left = @left_margin +
561
- line_number_width +
562
- (@y_axis_label.nil? ? 0.0 : @marker_caps_height + LABEL_MARGIN * 2)
492
+ @graph_left = @left_margin + line_number_width + (@y_axis_label.nil? ? 0.0 : @marker_caps_height + LABEL_MARGIN * 2)
563
493
 
564
494
  # Make space for half the width of the rightmost column label.
565
495
  # Might be greater than the number of columns if between-style bar markers are used.
566
- last_label = @labels.keys.sort.last.to_i
567
- extra_room_for_long_label = (last_label >= (@column_count - 1) && @center_labels_over_point) ?
568
- calculate_width(@marker_font_size, @labels[last_label]) / 2.0 :
569
- 0
496
+ last_label = @labels.keys.max.to_i
497
+ extra_room_for_long_label = begin
498
+ (last_label >= (column_count - 1) && @center_labels_over_point) ? calculate_width(@marker_font_size, @labels[last_label]) / 2.0 : 0
499
+ end
570
500
  @graph_right_margin = @right_margin + extra_room_for_long_label
571
501
 
572
- @graph_bottom_margin = @bottom_margin +
573
- @marker_caps_height + LABEL_MARGIN
502
+ @graph_bottom_margin = @bottom_margin + @marker_caps_height + LABEL_MARGIN
574
503
  end
575
504
 
576
505
  @graph_right = @raw_columns - @graph_right_margin
@@ -578,12 +507,17 @@ module Gruff
578
507
 
579
508
  # When @hide title, leave a title_margin space for aesthetics.
580
509
  # Same with @hide_legend
581
- @graph_top = @legend_at_bottom ? @top_margin : (@top_margin +
582
- (@hide_title ? title_margin : @title_caps_height + title_margin) +
583
- (@hide_legend ? legend_margin : @legend_caps_height + legend_margin))
510
+ @graph_top = begin
511
+ if @legend_at_bottom
512
+ @top_margin
513
+ else
514
+ @top_margin +
515
+ (@hide_title ? title_margin : @title_caps_height + title_margin) +
516
+ (@hide_legend ? legend_margin : @legend_caps_height + legend_margin)
517
+ end
518
+ end
584
519
 
585
- x_axis_label_height = @x_axis_label.nil? ? 0.0 :
586
- @marker_caps_height + LABEL_MARGIN
520
+ x_axis_label_height = @x_axis_label.nil? ? 0.0 : @marker_caps_height + LABEL_MARGIN
587
521
  # FIXME: Consider chart types other than bar
588
522
  @graph_bottom = @raw_rows - @graph_bottom_margin - x_axis_label_height - @label_stagger_height
589
523
  @graph_height = @graph_bottom - @graph_top
@@ -591,34 +525,21 @@ module Gruff
591
525
 
592
526
  # Draw the optional labels for the x axis and y axis.
593
527
  def draw_axis_labels
594
- unless @x_axis_label.nil?
528
+ if @x_axis_label
595
529
  # X Axis
596
530
  # Centered vertically and horizontally by setting the
597
531
  # height to 1.0 and the width to the width of the graph.
598
532
  x_axis_label_y_coordinate = @graph_bottom + LABEL_MARGIN * 2 + @marker_caps_height
599
533
 
600
- # TODO Center between graph area
601
- @d.fill = @font_color
602
- @d.font = @font if @font
603
- @d.stroke('transparent')
604
- @d.pointsize = scale_fontsize(@marker_font_size)
605
- @d.gravity = NorthGravity
606
- @d = @d.annotate_scaled(@base_image,
607
- @raw_columns, 1.0,
608
- 0.0, x_axis_label_y_coordinate,
609
- @x_axis_label, @scale)
610
- debug { @d.line 0.0, x_axis_label_y_coordinate, @raw_columns, x_axis_label_y_coordinate }
534
+ # TODO: Center between graph area
535
+ text_renderer = Gruff::Renderer::Text.new(@x_axis_label, font: @font, size: @marker_font_size, color: @font_color)
536
+ text_renderer.render(@raw_columns, 1.0, 0.0, x_axis_label_y_coordinate)
611
537
  end
612
538
 
613
- unless @y_axis_label.nil?
539
+ if @y_axis_label
614
540
  # Y Axis, rotated vertically
615
- @d.rotation = -90.0
616
- @d.gravity = CenterGravity
617
- @d = @d.annotate_scaled(@base_image,
618
- 1.0, @raw_rows,
619
- @left_margin + @marker_caps_height / 2.0, 0.0,
620
- @y_axis_label, @scale)
621
- @d.rotation = 90.0
541
+ text_renderer = Gruff::Renderer::Text.new(@y_axis_label, font: @font, size: @marker_font_size, color: @font_color, rotation: -90)
542
+ text_renderer.render(1.0, @raw_rows, @left_margin + @marker_caps_height / 2.0, 0.0, Magick::CenterGravity)
622
543
  end
623
544
  end
624
545
 
@@ -626,127 +547,53 @@ module Gruff
626
547
  def draw_line_markers
627
548
  return if @hide_line_markers
628
549
 
629
- @d = @d.stroke_antialias false
630
-
631
- if @y_axis_increment.nil?
632
- # Try to use a number of horizontal lines that will come out even.
633
- #
634
- # TODO Do the same for larger numbers...100, 75, 50, 25
635
- if @marker_count.nil?
636
- (3..7).each do |lines|
637
- if @spread % lines == 0.0
638
- @marker_count = lines
639
- break
640
- end
641
- end
642
- @marker_count ||= 4
643
- end
644
- @increment = (@spread > 0 && @marker_count > 0) ? significant(@spread / @marker_count) : 1
645
- else
646
- # TODO Make this work for negative values
647
- @marker_count = (@spread / @y_axis_increment).to_i
648
- @increment = @y_axis_increment
649
- end
650
- @increment_scaled = @graph_height.to_f / (@spread / @increment)
550
+ increment_scaled = @graph_height.to_f / (@spread / @increment)
651
551
 
652
552
  # Draw horizontal line markers and annotate with numbers
653
553
  (0..@marker_count).each do |index|
654
- y = @graph_top + @graph_height - index.to_f * @increment_scaled
554
+ y = @graph_top + @graph_height - index.to_f * increment_scaled
655
555
 
656
- @d = @d.fill(@marker_color)
657
-
658
- # FIXME(uwe): Workaround for Issue #66
659
- # https://github.com/topfunky/gruff/issues/66
660
- # https://github.com/rmagick/rmagick/issues/82
661
- # Remove if the issue gets fixed.
662
- y += 0.001 unless defined?(JRUBY_VERSION)
663
- # EMXIF
664
-
665
- @d = @d.line(@graph_left, y, @graph_right, y)
556
+ Gruff::Renderer::Line.new(color: @marker_color).render(@graph_left, y, @graph_right, y)
666
557
  #If the user specified a marker shadow color, draw a shadow just below it
667
- unless @marker_shadow_color.nil?
668
- @d = @d.fill(@marker_shadow_color)
669
- @d = @d.line(@graph_left, y + 1, @graph_right, y + 1)
558
+ if @marker_shadow_color
559
+ Gruff::Renderer::Line.new(color: @marker_shadow_color).render(@graph_left, y + 1, @graph_right, y + 1)
670
560
  end
671
561
 
672
- marker_label = BigDecimal(index.to_s) * BigDecimal(@increment.to_s) +
673
- BigDecimal(@minimum_value.to_s)
562
+ marker_label = BigDecimal(index.to_s) * BigDecimal(@increment.to_s) + BigDecimal(minimum_value.to_s)
674
563
 
675
564
  unless @hide_line_numbers
676
- @d.fill = @font_color
677
- @d.font = @font if @font
678
- @d.stroke('transparent')
679
- @d.pointsize = scale_fontsize(@marker_font_size)
680
- @d.gravity = EastGravity
681
-
682
- # Vertically center with 1.0 for the height
683
- @d = @d.annotate_scaled(@base_image,
684
- @graph_left - LABEL_MARGIN, 1.0,
685
- 0.0, y,
686
- label(marker_label, @increment), @scale)
565
+ label = label(marker_label, @increment)
566
+ text_renderer = Gruff::Renderer::Text.new(label, font: @font, size: @marker_font_size, color: @font_color)
567
+ text_renderer.render(@graph_left - LABEL_MARGIN, 1.0, 0.0, y, Magick::EastGravity)
687
568
  end
688
569
  end
689
-
690
- # # Submitted by a contibutor...the utility escapes me
691
- # i = 0
692
- # @additional_line_values.each do |value|
693
- # @increment_scaled = @graph_height.to_f / (@maximum_value.to_f / value)
694
- #
695
- # y = @graph_top + @graph_height - @increment_scaled
696
- #
697
- # @d = @d.stroke(@additional_line_colors[i])
698
- # @d = @d.line(@graph_left, y, @graph_right, y)
699
- #
700
- #
701
- # @d.fill = @additional_line_colors[i]
702
- # @d.font = @font if @font
703
- # @d.stroke('transparent')
704
- # @d.pointsize = scale_fontsize(@marker_font_size)
705
- # @d.gravity = EastGravity
706
- # @d = @d.annotate_scaled( @base_image,
707
- # 100, 20,
708
- # -10, y - (@marker_font_size/2.0),
709
- # "", @scale)
710
- # i += 1
711
- # end
712
-
713
- @d = @d.stroke_antialias true
714
570
  end
715
571
 
716
- ##
717
572
  # Return the sum of values in an array.
718
573
  #
719
574
  # Duplicated to not conflict with active_support in Rails.
720
-
721
575
  def sum(arr)
722
- arr.inject(0) { |i, m| m + i }
576
+ arr.reduce(0) { |i, m| m + i }
723
577
  end
724
578
 
725
- ##
726
579
  # Return a calculation of center
727
-
728
580
  def center(size)
729
581
  (@raw_columns - size) / 2
730
582
  end
731
583
 
732
- ##
733
584
  # Draws a legend with the names of the datasets matched
734
585
  # to the colors used to draw them.
735
-
736
586
  def draw_legend
737
587
  return if @hide_legend
738
588
 
739
- @legend_labels = @data.collect { |item| item[DATA_LABEL_INDEX] }
589
+ legend_labels = store.data.map(&:label)
740
590
 
741
591
  legend_square_width = @legend_box_size # small square with color of this item
742
592
 
743
593
  # May fix legend drawing problem at small sizes
744
- @d.font = @font if @font
745
- @d.pointsize = @legend_font_size
746
-
747
594
  label_widths = [[]] # Used to calculate line wrap
748
- @legend_labels.each do |label|
749
- metrics = @d.get_type_metrics(@base_image, label.to_s)
595
+ legend_labels.each do |label|
596
+ metrics = Renderer::Text.metrics(label, @legend_font_size)
750
597
  label_width = metrics.width + legend_square_width * 2.7
751
598
  label_widths.last.push label_width
752
599
 
@@ -756,44 +603,36 @@ module Gruff
756
603
  end
757
604
 
758
605
  current_x_offset = center(sum(label_widths.first))
759
- current_y_offset = @legend_at_bottom ? @graph_height + title_margin : (@hide_title ?
760
- @top_margin + title_margin :
761
- @top_margin + title_margin + @title_caps_height)
606
+ current_y_offset = begin
607
+ if @legend_at_bottom
608
+ @graph_height + title_margin
609
+ else
610
+ @hide_title ? @top_margin + title_margin : @top_margin + title_margin + @title_caps_height
611
+ end
612
+ end
762
613
 
763
- @legend_labels.each_with_index do |legend_label, index|
614
+ legend_labels.each_with_index do |legend_label, index|
764
615
  # Draw label
765
- @d.fill = @font_color
766
- @d.font = @font if @font
767
- @d.pointsize = scale_fontsize(@legend_font_size)
768
- @d.stroke('transparent')
769
- @d.font_weight = NormalWeight
770
- @d.gravity = WestGravity
771
- @d = @d.annotate_scaled(@base_image,
772
- @raw_columns, 1.0,
773
- current_x_offset + (legend_square_width * 1.7), current_y_offset,
774
- legend_label.to_s, @scale)
616
+ text_renderer = Gruff::Renderer::Text.new(legend_label, font: @font, size: @legend_font_size, color: @font_color)
617
+ text_renderer.render(@raw_columns, 1.0, current_x_offset + (legend_square_width * 1.7), current_y_offset, Magick::WestGravity)
775
618
 
776
619
  # Now draw box with color of this dataset
777
- @d = @d.stroke('transparent')
778
- @d = @d.fill @data[index][DATA_COLOR_INDEX]
779
- @d = @d.rectangle(current_x_offset,
780
- current_y_offset - legend_square_width / 2.0,
781
- current_x_offset + legend_square_width,
782
- current_y_offset + legend_square_width / 2.0)
783
-
784
- @d.pointsize = @legend_font_size
785
- metrics = @d.get_type_metrics(@base_image, legend_label.to_s)
620
+ rect_renderer = Gruff::Renderer::Rectangle.new(color: store.data[index].color)
621
+ rect_renderer.render(current_x_offset,
622
+ current_y_offset - legend_square_width / 2.0,
623
+ current_x_offset + legend_square_width,
624
+ current_y_offset + legend_square_width / 2.0)
625
+
626
+ metrics = Renderer::Text.metrics(legend_label, legend_font_size)
786
627
  current_string_offset = metrics.width + (legend_square_width * 2.7)
787
628
 
788
629
  # Handle wrapping
789
630
  label_widths.first.shift
790
631
  if label_widths.first.empty?
791
- debug { @d.line 0.0, current_y_offset, @raw_columns, current_y_offset }
792
-
793
632
  label_widths.shift
794
633
  current_x_offset = center(sum(label_widths.first)) unless label_widths.empty?
795
634
  line_height = [@legend_caps_height, legend_square_width].max + legend_margin
796
- if label_widths.length > 0
635
+ unless label_widths.empty?
797
636
  # Wrap to next line and shrink available graph dimensions
798
637
  current_y_offset += line_height
799
638
  @graph_top += line_height
@@ -803,32 +642,29 @@ module Gruff
803
642
  current_x_offset += current_string_offset
804
643
  end
805
644
  end
806
- @color_index = 0
807
645
  end
808
646
 
809
647
  # Draws a title on the graph.
810
648
  def draw_title
811
649
  return if @hide_title || @title.nil?
812
650
 
813
- @d.fill = @font_color
814
- @d.font = @title_font || @font if @title_font || @font
815
- @d.stroke('transparent')
816
- @d.pointsize = scale_fontsize(@title_font_size)
817
- @d.font_weight = @bold_title ? BoldWeight : NormalWeight
818
- @d.gravity = NorthGravity
819
- @d = @d.annotate_scaled(@base_image,
820
- @raw_columns, 1.0,
821
- 0, @top_margin,
822
- @title, @scale)
651
+ font = (@title_font || @font) if @title_font || @font
652
+ font_weight = @bold_title ? Magick::BoldWeight : Magick::NormalWeight
653
+ font_size = @title_font_size
654
+
655
+ metrics = Renderer::Text.metrics(@title, font_size, font_weight)
656
+ if metrics.width > @raw_columns
657
+ font_size = font_size * (@raw_columns / metrics.width) * 0.95
658
+ end
659
+ text_renderer = Gruff::Renderer::Text.new(@title, font: font, size: font_size, color: @font_color, weight: font_weight)
660
+ text_renderer.render(@raw_columns, 1.0, 0, @top_margin)
823
661
  end
824
662
 
825
663
  # Draws column labels below graph, centered over x_offset
826
664
  #--
827
665
  # TODO Allow WestGravity as an option
828
- def draw_label(x_offset, index)
829
- return if @hide_line_markers
830
-
831
- if !@labels[index].nil? && @labels_seen[index].nil?
666
+ def draw_label(x_offset, index, gravity = Magick::NorthGravity)
667
+ draw_unique_label(index) do
832
668
  y_offset = @graph_bottom + LABEL_MARGIN
833
669
 
834
670
  # TESTME
@@ -849,23 +685,21 @@ module Gruff
849
685
  else # @label_truncation_style is :absolute (default)
850
686
  label_text = label_text[0..(@label_max_size - 1)]
851
687
  end
852
-
853
688
  end
854
689
 
855
690
  if x_offset >= @graph_left && x_offset <= @graph_right
856
- @d.fill = @font_color
857
- @d.font = @font if @font
858
- @d.stroke('transparent')
859
- @d.font_weight = NormalWeight
860
- @d.pointsize = scale_fontsize(@marker_font_size)
861
- @d.gravity = NorthGravity
862
- @d = @d.annotate_scaled(@base_image,
863
- 1.0, 1.0,
864
- x_offset, y_offset,
865
- label_text, @scale)
691
+ text_renderer = Gruff::Renderer::Text.new(label_text, font: @font, size: @marker_font_size, color: @font_color)
692
+ text_renderer.render(1.0, 1.0, x_offset, y_offset, gravity)
866
693
  end
694
+ end
695
+ end
696
+
697
+ def draw_unique_label(index)
698
+ return if @hide_line_markers
699
+
700
+ if !@labels[index].nil? && @labels_seen[index].nil?
701
+ yield
867
702
  @labels_seen[index] = 1
868
- debug { @d.line 0.0, y_offset, @raw_columns, y_offset }
869
703
  end
870
704
  end
871
705
 
@@ -873,101 +707,20 @@ module Gruff
873
707
  def draw_value_label(x_offset, y_offset, data_point, bar_value = false)
874
708
  return if @hide_line_markers && !bar_value
875
709
 
876
- #y_offset = @graph_bottom + LABEL_MARGIN
877
-
878
- @d.fill = @font_color
879
- @d.font = @font if @font
880
- @d.stroke('transparent')
881
- @d.font_weight = NormalWeight
882
- @d.pointsize = scale_fontsize(@marker_font_size)
883
- @d.gravity = NorthGravity
884
- @d = @d.annotate_scaled(@base_image,
885
- 1.0, 1.0,
886
- x_offset, y_offset,
887
- data_point.to_s, @scale)
888
-
889
- debug { @d.line 0.0, y_offset, @raw_columns, y_offset }
710
+ text_renderer = Gruff::Renderer::Text.new(data_point, font: @font, size: @marker_font_size, color: @font_color)
711
+ text_renderer.render(1.0, 1.0, x_offset, y_offset)
890
712
  end
891
713
 
892
714
  # Shows an error message because you have no data.
893
715
  def draw_no_data
894
- @d.fill = @font_color
895
- @d.font = @font if @font
896
- @d.stroke('transparent')
897
- @d.font_weight = NormalWeight
898
- @d.pointsize = scale_fontsize(80)
899
- @d.gravity = CenterGravity
900
- @d = @d.annotate_scaled(@base_image,
901
- @raw_columns, @raw_rows / 2.0,
902
- 0, 10,
903
- @no_data_message, @scale)
904
- end
905
-
906
- # Finds the best background to render based on the provided theme options.
907
- #
908
- # Creates a @base_image to draw on.
909
- def render_background
910
- case @theme_options[:background_colors]
911
- when Array
912
- @base_image = render_gradiated_background(@theme_options[:background_colors][0], @theme_options[:background_colors][1], @theme_options[:background_direction])
913
- when String
914
- @base_image = render_solid_background(@theme_options[:background_colors])
915
- else
916
- @base_image = render_image_background(*@theme_options[:background_image])
917
- end
918
- end
919
-
920
- # Make a new image at the current size with a solid +color+.
921
- def render_solid_background(color)
922
- Image.new(@columns, @rows) {
923
- self.background_color = color
924
- }
925
- end
926
-
927
- # Use with a theme definition method to draw a gradiated background.
928
- def render_gradiated_background(top_color, bottom_color, direct = :top_bottom)
929
- case direct
930
- when :bottom_top
931
- gradient_fill = GradientFill.new(0, 0, 100, 0, bottom_color, top_color)
932
- when :left_right
933
- gradient_fill = GradientFill.new(0, 0, 0, 100, top_color, bottom_color)
934
- when :right_left
935
- gradient_fill = GradientFill.new(0, 0, 0, 100, bottom_color, top_color)
936
- when :topleft_bottomright
937
- gradient_fill = GradientFill.new(0, 100, 100, 0, top_color, bottom_color)
938
- when :topright_bottomleft
939
- gradient_fill = GradientFill.new(0, 0, 100, 100, bottom_color, top_color)
940
- else
941
- gradient_fill = GradientFill.new(0, 0, 100, 0, top_color, bottom_color)
942
- end
943
- Image.new(@columns, @rows, gradient_fill)
944
- end
945
-
946
- # Use with a theme to use an image (800x600 original) background.
947
- def render_image_background(image_path)
948
- image = Image.read(image_path)
949
- if @scale != 1.0
950
- image[0].resize!(@scale) # TODO Resize with new scale (crop if necessary for wide graph)
951
- end
952
- image[0]
953
- end
954
-
955
- # Use with a theme to make a transparent background
956
- def render_transparent_background
957
- Image.new(@columns, @rows) do
958
- self.background_color = 'transparent'
959
- end
716
+ text_renderer = Gruff::Renderer::Text.new(@no_data_message, font: @font, size: 80, color: @font_color)
717
+ text_renderer.render(@raw_columns, @raw_rows / 2.0, 0, 10, Magick::CenterGravity)
960
718
  end
961
719
 
962
720
  # Resets everything to defaults (except data).
963
721
  def reset_themes
964
- @color_index = 0
965
722
  @labels_seen = {}
966
723
  @theme_options = {}
967
-
968
- @d = Draw.new
969
- # Scale down from 800x600 used to calculate drawing.
970
- @d = @d.scale(@scale, @scale)
971
724
  end
972
725
 
973
726
  def scale(value) # :nodoc:
@@ -983,15 +736,6 @@ module Gruff
983
736
  (value > max_value) ? max_value : value
984
737
  end
985
738
 
986
- # Overridden by subclasses such as stacked bar.
987
- def larger_than_max?(data_point) # :nodoc:
988
- data_point > @maximum_value
989
- end
990
-
991
- def less_than_min?(data_point) # :nodoc:
992
- data_point < @minimum_value
993
- end
994
-
995
739
  def significant(i) # :nodoc:
996
740
  return 1.0 if i == 0 # Keep from going into infinite loop
997
741
 
@@ -1010,6 +754,8 @@ module Gruff
1010
754
  res = inc.floor * factor
1011
755
  if res.to_i.to_f == res
1012
756
  res.to_i
757
+ elsif res.to_f == res
758
+ res.to_f
1013
759
  else
1014
760
  res
1015
761
  end
@@ -1017,71 +763,22 @@ module Gruff
1017
763
 
1018
764
  # Sort with largest overall summed value at front of array.
1019
765
  def sort_data
1020
- @data = @data.sort_by { |a| -a[DATA_VALUES_INDEX].inject(0) { |sum, num| sum + num.to_f } }
766
+ store.sort_data!
1021
767
  end
1022
768
 
1023
- # Set the color for each data set unless it was gived in the data(...) call.
769
+ # Set the color for each data set unless it was given in the data(...) call.
1024
770
  def set_colors
1025
- @data.each { |a| a[DATA_COLOR_INDEX] ||= increment_color }
771
+ store.set_colors!(@colors)
1026
772
  end
1027
773
 
1028
774
  # Sort with largest overall summed value at front of array so it shows up
1029
775
  # correctly in the drawn graph.
1030
776
  def sort_norm_data
1031
- @norm_data =
1032
- @norm_data.sort_by { |a| -a[DATA_VALUES_INDEX].inject(0) { |sum, num| sum + num.to_f } }
1033
- end
1034
-
1035
- # Used by StackedBar and child classes.
1036
- #
1037
- # May need to be moved to the StackedBar class.
1038
- def get_maximum_by_stack
1039
- # Get sum of each stack
1040
- max_hash = {}
1041
- @data.each do |data_set|
1042
- data_set[DATA_VALUES_INDEX].each_with_index do |data_point, i|
1043
- max_hash[i] = 0.0 unless max_hash[i]
1044
- max_hash[i] += data_point.to_f
1045
- end
1046
- end
1047
-
1048
- # @maximum_value = 0
1049
- max_hash.each_key do |key|
1050
- @maximum_value = max_hash[key] if max_hash[key] > @maximum_value
1051
- end
1052
- @minimum_value = 0
1053
- end
1054
-
1055
- def make_stacked # :nodoc:
1056
- stacked_values = Array.new(@column_count, 0)
1057
- @data.each do |value_set|
1058
- value_set[DATA_VALUES_INDEX].each_with_index do |value, index|
1059
- stacked_values[index] += value
1060
- end
1061
- value_set[DATA_VALUES_INDEX] = stacked_values.dup
1062
- end
777
+ store.sort_norm_data!
1063
778
  end
1064
779
 
1065
780
  private
1066
781
 
1067
- # Takes a block and draws it if DEBUG is true.
1068
- #
1069
- # Example:
1070
- # debug { @d.rectangle x1, y1, x2, y2 }
1071
- def debug
1072
- if DEBUG
1073
- @d = @d.fill 'transparent'
1074
- @d = @d.stroke 'turquoise'
1075
- @d = yield
1076
- end
1077
- end
1078
-
1079
- # Returns the next color in your color list.
1080
- def increment_color
1081
- @color_index = (@color_index + 1) % @colors.length
1082
- @colors[@color_index - 1]
1083
- end
1084
-
1085
782
  # Return a formatted string representing a number value that should be
1086
783
  # printed as a label.
1087
784
  def label(value, increment)
@@ -1120,9 +817,8 @@ module Gruff
1120
817
  # Not scaled since it deals with dimensions that the regular scaling will
1121
818
  # handle.
1122
819
  def calculate_caps_height(font_size)
1123
- @d.pointsize = font_size
1124
- @d.font = @font if @font
1125
- @d.get_type_metrics(@base_image, 'X').height
820
+ metrics = Renderer::Text.metrics('X', font_size)
821
+ metrics.height
1126
822
  end
1127
823
 
1128
824
  # Returns the width of a string at this pointsize.
@@ -1132,57 +828,38 @@ module Gruff
1132
828
  def calculate_width(font_size, text)
1133
829
  return 0 if text.nil?
1134
830
 
1135
- @d.pointsize = font_size
1136
- @d.font = @font if @font
1137
- @d.get_type_metrics(@base_image, text.to_s).width
831
+ metrics = Renderer::Text.metrics(text, font_size)
832
+ metrics.width
833
+ end
834
+
835
+ def calculate_increment
836
+ if @y_axis_increment.nil?
837
+ # Try to use a number of horizontal lines that will come out even.
838
+ #
839
+ # TODO Do the same for larger numbers...100, 75, 50, 25
840
+ if @marker_count.nil?
841
+ (3..7).each do |lines|
842
+ if @spread % lines == 0.0
843
+ @marker_count = lines
844
+ break
845
+ end
846
+ end
847
+ @marker_count ||= 4
848
+ end
849
+ @increment = (@spread > 0 && @marker_count > 0) ? significant(@spread / @marker_count) : 1
850
+ else
851
+ # TODO: Make this work for negative values
852
+ @marker_count = (@spread / @y_axis_increment).to_i
853
+ @increment = @y_axis_increment
854
+ end
1138
855
  end
1139
856
 
1140
857
  # Used for degree => radian conversions
1141
858
  def deg2rad(angle)
1142
859
  angle * (Math::PI / 180.0)
1143
860
  end
1144
-
1145
- end # Gruff::Base
1146
-
1147
- class IncorrectNumberOfDatasetsException < StandardError
1148
- end
1149
-
1150
- end # Gruff
1151
-
1152
- module Magick
1153
-
1154
- class Draw
1155
-
1156
- # Additional method to scale annotation text since Draw.scale doesn't.
1157
- def annotate_scaled(img, width, height, x, y, text, scale)
1158
- scaled_width = (width * scale) >= 1 ? (width * scale) : 1
1159
- scaled_height = (height * scale) >= 1 ? (height * scale) : 1
1160
-
1161
- self.annotate(img,
1162
- scaled_width, scaled_height,
1163
- x * scale, y * scale,
1164
- text.gsub('%', '%%'))
1165
- end
1166
-
1167
- if defined? JRUBY_VERSION
1168
- # FIXME(uwe): We should NOT need to implement this method.
1169
- # Remove this method as soon as RMagick4J Issue #16 is fixed.
1170
- # https://github.com/Serabe/RMagick4J/issues/16
1171
- def fill=(fill)
1172
- fill = { white: '#FFFFFF' }[fill.to_sym] || fill
1173
- @draw.fill = Magick4J.ColorDatabase.query_default(fill)
1174
- self
1175
- end
1176
- # EMXIF
1177
- end
1178
-
1179
861
  end
1180
862
 
1181
- end # Magick
1182
-
1183
- class String
1184
- #Taken from http://codesnippets.joyent.com/posts/show/330
1185
- def commify(delimiter = ',')
1186
- self.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")
863
+ class IncorrectNumberOfDatasetsException < StandardError
1187
864
  end
1188
865
  end