gruff 0.7.0-java → 0.12.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (113) hide show
  1. checksums.yaml +5 -5
  2. data/{History.txt → CHANGELOG.md} +81 -25
  3. data/Gemfile +3 -1
  4. data/README.md +51 -23
  5. data/assets/plastik/blue.png +0 -0
  6. data/assets/plastik/green.png +0 -0
  7. data/assets/plastik/red.png +0 -0
  8. data/gruff.gemspec +23 -13
  9. data/init.rb +2 -0
  10. data/lib/gruff.rb +33 -4
  11. data/lib/gruff/accumulator_bar.rb +17 -9
  12. data/lib/gruff/area.rb +31 -21
  13. data/lib/gruff/bar.rb +90 -46
  14. data/lib/gruff/base.rb +476 -710
  15. data/lib/gruff/bezier.rb +29 -18
  16. data/lib/gruff/bullet.rb +58 -71
  17. data/lib/gruff/dot.rb +35 -83
  18. data/lib/gruff/helper/bar_conversion.rb +47 -0
  19. data/lib/gruff/helper/bar_value_label_mixin.rb +33 -0
  20. data/lib/gruff/helper/stacked_mixin.rb +23 -0
  21. data/lib/gruff/histogram.rb +59 -0
  22. data/lib/gruff/line.rb +121 -199
  23. data/lib/gruff/mini/bar.rb +17 -10
  24. data/lib/gruff/mini/legend.rb +26 -38
  25. data/lib/gruff/mini/pie.rb +18 -13
  26. data/lib/gruff/mini/side_bar.rb +25 -12
  27. data/lib/gruff/net.rb +69 -83
  28. data/lib/gruff/patch/rmagick.rb +31 -0
  29. data/lib/gruff/patch/string.rb +13 -0
  30. data/lib/gruff/photo_bar.rb +36 -43
  31. data/lib/gruff/pie.rb +42 -103
  32. data/lib/gruff/renderer/bezier.rb +22 -0
  33. data/lib/gruff/renderer/circle.rb +22 -0
  34. data/lib/gruff/renderer/dash_line.rb +23 -0
  35. data/lib/gruff/renderer/dot.rb +40 -0
  36. data/lib/gruff/renderer/ellipse.rb +22 -0
  37. data/lib/gruff/renderer/line.rb +43 -0
  38. data/lib/gruff/renderer/polygon.rb +24 -0
  39. data/lib/gruff/renderer/polyline.rb +22 -0
  40. data/lib/gruff/renderer/rectangle.rb +20 -0
  41. data/lib/gruff/renderer/renderer.rb +120 -0
  42. data/lib/gruff/renderer/text.rb +57 -0
  43. data/lib/gruff/scatter.rb +128 -201
  44. data/lib/gruff/scene.rb +30 -41
  45. data/lib/gruff/side_bar.rb +100 -68
  46. data/lib/gruff/side_stacked_bar.rb +92 -63
  47. data/lib/gruff/spider.rb +47 -53
  48. data/lib/gruff/stacked_area.rb +37 -34
  49. data/lib/gruff/stacked_bar.rb +99 -54
  50. data/lib/gruff/store/basic_data.rb +36 -0
  51. data/lib/gruff/store/custom_data.rb +36 -0
  52. data/lib/gruff/store/store.rb +81 -0
  53. data/lib/gruff/store/xy_data.rb +58 -0
  54. data/lib/gruff/themes.rb +32 -33
  55. data/lib/gruff/version.rb +3 -1
  56. metadata +74 -102
  57. data/.gitignore +0 -7
  58. data/.travis.yml +0 -19
  59. data/Manifest.txt +0 -81
  60. data/RELEASE.md +0 -30
  61. data/Rakefile +0 -218
  62. data/assets/bubble.png +0 -0
  63. data/assets/city_scene/background/0000.png +0 -0
  64. data/assets/city_scene/background/0600.png +0 -0
  65. data/assets/city_scene/background/2000.png +0 -0
  66. data/assets/city_scene/clouds/cloudy.png +0 -0
  67. data/assets/city_scene/clouds/partly_cloudy.png +0 -0
  68. data/assets/city_scene/clouds/stormy.png +0 -0
  69. data/assets/city_scene/grass/default.png +0 -0
  70. data/assets/city_scene/haze/true.png +0 -0
  71. data/assets/city_scene/number_sample/1.png +0 -0
  72. data/assets/city_scene/number_sample/2.png +0 -0
  73. data/assets/city_scene/number_sample/default.png +0 -0
  74. data/assets/city_scene/sky/0000.png +0 -0
  75. data/assets/city_scene/sky/0200.png +0 -0
  76. data/assets/city_scene/sky/0400.png +0 -0
  77. data/assets/city_scene/sky/0600.png +0 -0
  78. data/assets/city_scene/sky/0800.png +0 -0
  79. data/assets/city_scene/sky/1000.png +0 -0
  80. data/assets/city_scene/sky/1200.png +0 -0
  81. data/assets/city_scene/sky/1400.png +0 -0
  82. data/assets/city_scene/sky/1500.png +0 -0
  83. data/assets/city_scene/sky/1700.png +0 -0
  84. data/assets/city_scene/sky/2000.png +0 -0
  85. data/assets/pc306715.jpg +0 -0
  86. data/lib/gruff/bar_conversion.rb +0 -46
  87. data/lib/gruff/deprecated.rb +0 -39
  88. data/lib/gruff/stacked_mixin.rb +0 -23
  89. data/test/gruff_test_case.rb +0 -152
  90. data/test/image_compare.rb +0 -58
  91. data/test/test_accumulator_bar.rb +0 -51
  92. data/test/test_area.rb +0 -134
  93. data/test/test_bar.rb +0 -505
  94. data/test/test_base.rb +0 -33
  95. data/test/test_bezier.rb +0 -33
  96. data/test/test_bullet.rb +0 -26
  97. data/test/test_dot.rb +0 -263
  98. data/test/test_labels_for_null_data.rb +0 -27
  99. data/test/test_legend.rb +0 -68
  100. data/test/test_line.rb +0 -674
  101. data/test/test_mini_bar.rb +0 -33
  102. data/test/test_mini_pie.rb +0 -25
  103. data/test/test_mini_side_bar.rb +0 -36
  104. data/test/test_net.rb +0 -231
  105. data/test/test_photo.rb +0 -41
  106. data/test/test_pie.rb +0 -194
  107. data/test/test_scatter.rb +0 -270
  108. data/test/test_scene.rb +0 -100
  109. data/test/test_side_bar.rb +0 -56
  110. data/test/test_sidestacked_bar.rb +0 -105
  111. data/test/test_spider.rb +0 -226
  112. data/test/test_stacked_area.rb +0 -52
  113. data/test/test_stacked_bar.rb +0 -68
@@ -1,20 +1,37 @@
1
- require File.dirname(__FILE__) + '/base'
2
-
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Gruff::Bezier is a special line graph that have
5
+ # the bezier curve.
6
+ #
7
+ # Here's how to set up a Gruff::Bezier.
8
+ #
9
+ # dataset = [
10
+ # +0.00, +0.09, +0.19, +0.29, +0.38, +0.47, +0.56, +0.64, +0.71, +0.78,
11
+ # +0.84, +0.89, +0.93, +0.96, +0.98, +0.99, +0.99, +0.99, +0.97, +0.94,
12
+ # +0.90, +0.86, +0.80, +0.74, +0.67, +0.59, +0.51, +0.42, +0.33, +0.23,
13
+ # +0.14, +0.04, -0.06, -0.16, -0.26, -0.36, -0.45, -0.53, -0.62, -0.69,
14
+ # -0.76, -0.82, -0.88, -0.92, -0.96, -0.98, -1.00, -1.00, -1.00, -0.99,
15
+ # -0.96, -0.93, -0.89, -0.84, -0.78, -0.71, -0.64, -0.56, -0.47, -0.38,
16
+ # ]
17
+ # g = Gruff::Bezier.new
18
+ # g.data 'sin', dataset
19
+ # g.write('bezier.png')
20
+ #
3
21
  class Gruff::Bezier < Gruff::Base
4
22
  def draw
5
23
  super
6
24
 
7
- return unless @has_data
25
+ return unless data_given?
8
26
 
9
- @x_increment = @graph_width / (@column_count - 1).to_f
27
+ x_increment = @graph_width / (column_count - 1).to_f
10
28
 
11
- @norm_data.each do |data_row|
12
- poly_points = Array.new
13
- @d = @d.fill data_row[DATA_COLOR_INDEX]
29
+ store.norm_data.each do |data_row|
30
+ poly_points = []
14
31
 
15
32
  data_row[1].each_with_index do |data_point, index|
16
33
  # Use incremented x and scaled y
17
- new_x = @graph_left + (@x_increment * index)
34
+ new_x = @graph_left + (x_increment * index)
18
35
  new_y = @graph_top + (@graph_height - data_point * @graph_height)
19
36
 
20
37
  if index == 0 && RUBY_PLATFORM != 'java'
@@ -27,20 +44,14 @@ class Gruff::Bezier < Gruff::Base
27
44
 
28
45
  draw_label(new_x, index)
29
46
  end
30
-
31
- @d = @d.fill_opacity 0.0
32
- @d = @d.stroke data_row[DATA_COLOR_INDEX]
33
- @d = @d.stroke_width clip_value_if_greater_than(@columns / (@norm_data.first[1].size * 4), 5.0)
47
+
48
+ stroke_width = clip_value_if_greater_than(@columns / (store.norm_data.first[1].size * 4), 5.0)
34
49
 
35
50
  if RUBY_PLATFORM == 'java'
36
- @d = @d.polyline(*poly_points)
51
+ Gruff::Renderer::Polyline.new(color: data_row.color, width: stroke_width).render(poly_points)
37
52
  else
38
- @d = @d.bezier(*poly_points)
53
+ Gruff::Renderer::Bezier.new(color: data_row.color, width: stroke_width).render(poly_points)
39
54
  end
40
55
  end
41
-
42
- @d.draw(@base_image)
43
56
  end
44
-
45
-
46
57
  end
@@ -1,52 +1,48 @@
1
- require File.dirname(__FILE__) + '/base'
2
- require 'gruff/themes'
1
+ # frozen_string_literal: true
3
2
 
3
+ #
4
+ # A bullet graph is a variation of a bar graph.
4
5
  # http://en.wikipedia.org/wiki/Bullet_graph
6
+ #
7
+ # Here's how to set up a Gruff::Bullet.
8
+ #
9
+ # g = Gruff::Bullet.new
10
+ # g.title = 'Monthly Revenue'
11
+ # g.data 75, 100, { target: 80, low: 50, high: 90 }
12
+ # g.write('bullet.png')
13
+ #
5
14
  class Gruff::Bullet < Gruff::Base
15
+ def initialize(target_width = '400x40')
16
+ super
6
17
 
7
- def initialize(target_width="400x40")
8
- if not Numeric === target_width
9
- geometric_width, geometric_height = target_width.split('x')
10
- @columns = geometric_width.to_f
11
- @rows = geometric_height.to_f
18
+ if target_width.is_a?(String)
19
+ @columns, @rows = target_width.split('x').map(&:to_f)
12
20
  else
13
21
  @columns = target_width.to_f
14
22
  @rows = target_width.to_f / 5.0
15
23
  end
24
+ @columns.freeze
25
+ @rows.freeze
16
26
 
17
- initialize_ivars
18
-
19
- reset_themes
20
27
  self.theme = Gruff::Themes::GREYSCALE
28
+ end
29
+
30
+ def initialize_ivars
31
+ super
32
+
21
33
  @title_font_size = 20
22
34
  end
35
+ private :initialize_ivars
23
36
 
24
- def data(value, maximum_value, options={})
37
+ def data(value, maximum_value, options = {})
25
38
  @value = value.to_f
26
- @maximum_value = maximum_value.to_f
39
+ self.maximum_value = maximum_value.to_f
27
40
  @options = options
28
- @options.map { |k, v| @options[k] = v.to_f if v === Numeric }
41
+ @options.map { |k, v| @options[k] = v.to_f if v.is_a?(Numeric) }
29
42
  end
30
43
 
31
- # def setup_drawing
32
- # # Maybe should be done in one of the following functions for more granularity.
33
- # unless @has_data
34
- # draw_no_data()
35
- # return
36
- # end
37
- #
38
- # normalize()
39
- # setup_graph_measurements()
40
- # sort_norm_data() if @sort # Sort norm_data with avg largest values set first (for display)
41
- #
42
- # draw_legend()
43
- # draw_line_markers()
44
- # draw_axis_labels()
45
- # draw_title
46
- # end
47
-
48
44
  def draw
49
- # TODO Left label
45
+ # TODO: Left label
50
46
  # TODO Bottom labels and markers
51
47
  # @graph_bottom
52
48
  # Calculations are off 800x???
@@ -55,57 +51,48 @@ class Gruff::Bullet < Gruff::Base
55
51
 
56
52
  draw_title
57
53
 
58
- @margin = 30.0
59
- @thickness = @raw_rows / 6.0
60
- @right_margin = @margin
61
- @graph_left = (@title && (@title_width * 1.3)) || @margin
62
- @graph_width = @raw_columns - @graph_left - @right_margin
63
- @graph_height = @thickness * 3.0
54
+ title_width = calculate_width(@title_font_size, @title)
55
+ margin = 30.0
56
+ thickness = @raw_rows / 6.0
57
+ right_margin = margin
58
+ graph_left = [title_width * 1.3, margin].max
59
+ graph_width = @raw_columns - graph_left - right_margin
60
+ graph_height = thickness * 3.0
64
61
 
65
62
  # Background
66
- @d = @d.fill @colors[0]
67
- @d = @d.rectangle(@graph_left, 0, @graph_left + @graph_width, @graph_height)
63
+ rect_renderer = Gruff::Renderer::Rectangle.new(color: @colors[0])
64
+ rect_renderer.render(graph_left, 0, graph_left + graph_width, graph_height)
68
65
 
69
66
  [:high, :low].each_with_index do |indicator, index|
70
- next unless @options.has_key?(indicator)
71
- @d = @d.fill @colors[index + 1]
72
- indicator_width_x = @graph_left + @graph_width * (@options[indicator] / @maximum_value)
73
- @d = @d.rectangle(@graph_left, 0, indicator_width_x, @graph_height)
67
+ next unless @options.key?(indicator)
68
+
69
+ indicator_width_x = graph_left + graph_width * (@options[indicator] / maximum_value)
70
+
71
+ rect_renderer = Gruff::Renderer::Rectangle.new(color: @colors[index + 1])
72
+ rect_renderer.render(graph_left, 0, indicator_width_x, graph_height)
74
73
  end
75
74
 
76
- if @options.has_key?(:target)
77
- @d = @d.fill @font_color
78
- target_x = @graph_left + @graph_width * (@options[:target] / @maximum_value)
79
- half_thickness = @thickness / 2.0
80
- @d = @d.rectangle(target_x, half_thickness, target_x + half_thickness, @thickness * 2 + half_thickness)
75
+ if @options.key?(:target)
76
+ target_x = graph_left + graph_width * (@options[:target] / maximum_value)
77
+ half_thickness = thickness / 2.0
78
+
79
+ rect_renderer = Gruff::Renderer::Rectangle.new(color: @font_color)
80
+ rect_renderer.render(target_x, half_thickness, target_x + half_thickness, thickness * 2 + half_thickness)
81
81
  end
82
82
 
83
83
  # Value
84
- @d = @d.fill @font_color
85
- @d = @d.rectangle(@graph_left, @thickness, @graph_left + @graph_width * (@value / @maximum_value), @thickness * 2)
86
-
87
- @d.draw(@base_image)
84
+ rect_renderer = Gruff::Renderer::Rectangle.new(color: @font_color)
85
+ rect_renderer.render(graph_left, thickness, graph_left + graph_width * (@value / maximum_value), thickness * 2)
88
86
  end
89
87
 
88
+ private
89
+
90
90
  def draw_title
91
- return unless @title
92
-
93
- @font_height = calculate_caps_height(scale_fontsize(@title_font_size))
94
- @title_width = calculate_width(@title_font_size, @title)
95
-
96
- @d.fill = @font_color
97
- @d.font = @font if @font
98
- @d.stroke('transparent')
99
- @d.font_weight = NormalWeight
100
- @d.pointsize = scale_fontsize(@title_font_size)
101
- @d.gravity = NorthWestGravity
102
- @d = @d.annotate_scaled(*[
103
- @base_image,
104
- 1.0, 1.0,
105
- @font_height/2, @font_height/2,
106
- @title,
107
- @scale
108
- ])
109
- end
91
+ return if hide_title?
92
+
93
+ font_height = calculate_caps_height(scale_fontsize(@title_font_size))
110
94
 
95
+ text_renderer = Gruff::Renderer::Text.new(@title, font: @font, size: @title_font_size, color: @font_color)
96
+ text_renderer.add_to_render_queue(1.0, 1.0, font_height / 2, font_height / 2, Magick::NorthWestGravity)
97
+ end
111
98
  end
@@ -1,105 +1,67 @@
1
- require File.dirname(__FILE__) + '/base'
1
+ # frozen_string_literal: true
2
2
 
3
- ##
4
- # Graph with dots and labels along a vertical access
3
+ #
4
+ # Graph with dots and labels along a vertical access.
5
5
  # see: 'Creating More Effective Graphs' by Robbins
6
-
6
+ #
7
+ # Here's how to set up a Gruff::Dot.
8
+ #
9
+ # g = Gruff::Dot.new
10
+ # g.title = 'Dot Graph'
11
+ # g.data :Art, [0, 5, 8, 15]
12
+ # g.data :Philosophy, [10, 3, 2, 8]
13
+ # g.data :Science, [2, 15, 8, 11]
14
+ # g.write('dot.png')
15
+ #
7
16
  class Gruff::Dot < Gruff::Base
8
-
9
17
  def draw
10
18
  @has_left_labels = true
11
19
  super
12
20
 
13
- return unless @has_data
21
+ return unless data_given?
14
22
 
15
23
  # Setup spacing.
16
24
  #
17
25
  spacing_factor = 1.0
18
26
 
19
- @items_width = @graph_height / @column_count.to_f
20
- @item_width = @items_width * spacing_factor / @norm_data.size
21
- @d = @d.stroke_opacity 0.0
22
- padding = (@items_width * (1 - spacing_factor)) / 2
27
+ items_width = @graph_height / column_count.to_f
28
+ item_width = items_width * spacing_factor / store.length
29
+ padding = (items_width * (1 - spacing_factor)) / 2
23
30
 
24
- @norm_data.each_with_index do |data_row, row_index|
25
- data_row[DATA_VALUES_INDEX].each_with_index do |data_point, point_index|
31
+ store.norm_data.each_with_index do |data_row, row_index|
32
+ data_row.points.each_with_index do |data_point, point_index|
26
33
  x_pos = @graph_left + (data_point * @graph_width)
27
- y_pos = @graph_top + (@items_width * point_index) + padding + (@items_width.to_f/2.0).round
34
+ y_pos = @graph_top + (items_width * point_index) + padding + (items_width.to_f / 2.0).round
28
35
 
29
36
  if row_index == 0
30
- @d = @d.stroke(@marker_color)
31
- @d = @d.fill(@marker_color)
32
- @d = @d.stroke_width 1.0
33
- @d = @d.stroke_opacity 0.1
34
- @d = @d.fill_opacity 0.1
35
- @d = @d.line(@graph_left, y_pos, @graph_left + @graph_width, y_pos)
36
- @d = @d.fill_opacity 1
37
+ Gruff::Renderer::Line.new(color: @marker_color).render(@graph_left, y_pos, @graph_left + @graph_width, y_pos)
37
38
  end
38
39
 
39
- @d = @d.fill data_row[DATA_COLOR_INDEX]
40
- @d = @d.stroke('transparent')
41
- @d = @d.circle(x_pos, y_pos, x_pos + (@item_width.to_f/3.0).round, y_pos)
40
+ Gruff::Renderer::Circle.new(color: data_row.color).render(x_pos, y_pos, x_pos + (item_width.to_f / 3.0).round, y_pos)
42
41
 
43
42
  draw_label(y_pos, point_index)
44
43
  end
45
-
46
44
  end
47
-
48
- @d.draw(@base_image)
49
45
  end
50
46
 
51
- protected
47
+ protected
52
48
 
53
49
  # Instead of base class version, draws vertical background lines and label
54
50
  def draw_line_markers
55
51
  return if @hide_line_markers
56
52
 
57
- @d = @d.stroke_antialias false
53
+ (0..marker_count).each do |index|
54
+ marker_label = minimum_value + index * @increment
55
+ x = @graph_left + (marker_label - minimum_value) * @graph_width / @spread
58
56
 
59
- # Draw horizontal line markers and annotate with numbers
60
- @d = @d.stroke(@marker_color)
61
- @d = @d.stroke_width 1
62
- if @y_axis_increment
63
- increment = @y_axis_increment
64
- number_of_lines = (@spread / @y_axis_increment).to_i
65
- else
66
- # Try to use a number of horizontal lines that will come out even.
67
- #
68
- # TODO Do the same for larger numbers...100, 75, 50, 25
69
- if @marker_count.nil?
70
- (3..7).each do |lines|
71
- if @spread % lines == 0.0
72
- @marker_count = lines
73
- break
74
- end
75
- end
76
- @marker_count ||= 5
77
- end
78
- # TODO Round maximum marker value to a round number like 100, 0.1, 0.5, etc.
79
- @increment = (@spread > 0 && @marker_count > 0) ? significant(@spread / @marker_count) : 1
80
-
81
- number_of_lines = @marker_count
82
- increment = @increment
83
- end
84
-
85
- (0..number_of_lines).each do |index|
86
- marker_label = @minimum_value + index * increment
87
- x = @graph_left + (marker_label - @minimum_value) * @graph_width / @spread
88
- @d = @d.line(x, @graph_bottom, x, @graph_bottom + 0.5 * LABEL_MARGIN)
57
+ line_renderer = Gruff::Renderer::Line.new(color: @marker_color, shadow_color: @marker_shadow_color)
58
+ line_renderer.render(x, @graph_bottom, x, @graph_bottom + 5)
89
59
 
90
60
  unless @hide_line_numbers
91
- @d.fill = @font_color
92
- @d.font = @font if @font
93
- @d.stroke = 'transparent'
94
- @d.pointsize = scale_fontsize(@marker_font_size)
95
- @d.gravity = CenterGravity
96
- # TODO Center text over line
97
- @d = @d.annotate_scaled(@base_image,
98
- 0, 0, # Width of box to draw text in
99
- x, @graph_bottom + (LABEL_MARGIN * 2.0), # Coordinates of text
100
- label(marker_label, increment), @scale)
101
- end # unless
102
- @d = @d.stroke_antialias true
61
+ label = label(marker_label, @increment)
62
+ text_renderer = Gruff::Renderer::Text.new(label, font: @font, size: @marker_font_size, color: @font_color)
63
+ text_renderer.add_to_render_queue(0, 0, x, @graph_bottom + (LABEL_MARGIN * 1.5), Magick::CenterGravity)
64
+ end
103
65
  end
104
66
  end
105
67
 
@@ -107,19 +69,9 @@ class Gruff::Dot < Gruff::Base
107
69
  # Draw on the Y axis instead of the X
108
70
 
109
71
  def draw_label(y_offset, index)
110
- if !@labels[index].nil? && @labels_seen[index].nil?
111
- @d.fill = @font_color
112
- @d.font = @font if @font
113
- @d.stroke = 'transparent'
114
- @d.font_weight = NormalWeight
115
- @d.pointsize = scale_fontsize(@marker_font_size)
116
- @d.gravity = EastGravity
117
- @d = @d.annotate_scaled(@base_image,
118
- 1, 1,
119
- -@graph_left + LABEL_MARGIN * 2.0, y_offset,
120
- @labels[index], @scale)
121
- @labels_seen[index] = 1
72
+ draw_unique_label(index) do
73
+ text_renderer = Gruff::Renderer::Text.new(@labels[index], font: @font, size: @marker_font_size, color: @font_color)
74
+ text_renderer.add_to_render_queue(@graph_left - LABEL_MARGIN, 1.0, 0.0, y_offset, Magick::EastGravity)
122
75
  end
123
76
  end
124
-
125
77
  end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ ##
4
+ # Original Author: David Stokar
5
+ #
6
+ # This class performs the y coordinates conversion for the bar class.
7
+ #
8
+ # There are three cases:
9
+ #
10
+ # 1. Bars all go from zero in positive direction
11
+ # 2. Bars all go from zero to negative direction
12
+ # 3. Bars either go from zero to positive or from zero to negative
13
+ #
14
+ # @private
15
+ class Gruff::BarConversion
16
+ attr_writer :mode
17
+ attr_writer :zero
18
+ attr_writer :graph_top
19
+ attr_writer :graph_height
20
+ attr_writer :minimum_value
21
+ attr_writer :spread
22
+
23
+ def get_left_y_right_y_scaled(data_point)
24
+ result = []
25
+
26
+ case @mode
27
+ when 1
28
+ # minimum value >= 0 ( only positive values )
29
+ result[0] = @graph_top + @graph_height * (1 - data_point) + 1
30
+ result[1] = @graph_top + @graph_height - 1
31
+ when 2
32
+ # only negative values
33
+ result[0] = @graph_top + 1
34
+ result[1] = @graph_top + @graph_height * (1 - data_point) - 1
35
+ when 3
36
+ # positive and negative values
37
+ val = data_point - @minimum_value / @spread
38
+ result[0] = @graph_top + @graph_height * (1 - (val - @zero)) + 1
39
+ result[1] = @graph_top + @graph_height * (1 - @zero) - 1
40
+ else
41
+ result[0] = 0.0
42
+ result[1] = 0.0
43
+ end
44
+
45
+ result
46
+ end
47
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @private
4
+ module Gruff::Base::BarValueLabelMixin
5
+ using String::GruffCommify
6
+
7
+ # @private
8
+ class BarValueLabel
9
+ attr_accessor :coordinates, :values
10
+
11
+ def initialize(size, bar_width)
12
+ @coordinates = Array.new(size)
13
+ @values = Hash.new(0)
14
+ @bar_width = bar_width
15
+ end
16
+
17
+ def prepare_rendering(format)
18
+ @coordinates.each_with_index do |(left_x, left_y, right_x, _right_y), index|
19
+ value = @values[index]
20
+ val = (format || '%.2f') % value
21
+ y = value >= 0 ? left_y - 30 : left_y + 12
22
+ yield left_x + (right_x - left_x) / 2, y, val.commify
23
+ end
24
+ end
25
+
26
+ def prepare_sidebar_rendering(format)
27
+ @coordinates.each_with_index do |(_left_x, _left_y, right_x, right_y), index|
28
+ val = (format || '%.2f') % @values[index]
29
+ yield right_x + 40, right_y - @bar_width / 2, val.commify
30
+ end
31
+ end
32
+ end
33
+ end