gruff 0.12.2 → 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -19,15 +19,13 @@
19
19
  # g.write('sidebar.png')
20
20
  #
21
21
  class Gruff::SideBar < Gruff::Base
22
- using String::GruffCommify
23
-
24
22
  # Spacing factor applied between bars.
25
23
  attr_writer :bar_spacing
26
24
 
27
25
  # Spacing factor applied between a group of bars belonging to the same label.
28
26
  attr_writer :group_spacing
29
27
 
30
- # Set the number output format for labels using sprintf.
28
+ # Set the number output format string or lambda.
31
29
  # Default is +"%.2f"+.
32
30
  attr_writer :label_formatting
33
31
 
@@ -57,6 +55,12 @@ class Gruff::SideBar < Gruff::Base
57
55
  draw_bars
58
56
  end
59
57
 
58
+ # With Side Bars use the data label for the marker value to the left of the bar.
59
+ # @deprecated
60
+ def use_data_label=(_value)
61
+ warn '#use_data_label is deprecated. It is no longer effective.'
62
+ end
63
+
60
64
  protected
61
65
 
62
66
  def hide_labels?
@@ -73,15 +77,22 @@ protected
73
77
 
74
78
  private
75
79
 
80
+ # Value to avoid completely overwriting the coordinate axis
81
+ AXIS_MARGIN = 0.5
82
+
76
83
  def draw_bars
77
84
  # Setup spacing.
78
85
  #
79
86
  bars_width = (@graph_height - calculate_spacing) / column_count.to_f
80
87
  bar_width = bars_width / store.length
81
- height = Array.new(column_count, 0)
82
- length = Array.new(column_count, @graph_left)
83
88
  padding = (bar_width * (1 - @bar_spacing)) / 2
84
89
 
90
+ # Setup the BarConversion Object
91
+ conversion = Gruff::BarConversion.new(
92
+ top: @graph_right, bottom: @graph_left,
93
+ minimum_value: minimum_value, maximum_value: maximum_value, spread: @spread
94
+ )
95
+
85
96
  # if we're a side stacked bar then we don't need to draw ourself at all
86
97
  # because sometimes (due to different heights/min/max) you can actually
87
98
  # see both graphs and it looks like crap
@@ -91,35 +102,24 @@ private
91
102
  data_row.points.each_with_index do |data_point, point_index|
92
103
  group_spacing = @group_spacing * @scale * point_index
93
104
 
94
- # Using the original calculations from the stacked bar chart
95
- # to get the difference between
96
- # part of the bart chart we wish to stack.
97
- temp1 = @graph_left + (@graph_width - data_point * @graph_width - height[point_index])
98
- temp2 = @graph_left + @graph_width - height[point_index]
99
- difference = temp2 - temp1
100
-
101
- left_x = length[point_index] - 1
102
105
  left_y = @graph_top + (bars_width * point_index) + (bar_width * row_index) + padding + group_spacing
103
- right_x = left_x + difference
104
106
  right_y = left_y + bar_width * @bar_spacing
105
107
 
106
- height[point_index] += (data_point * @graph_width)
108
+ left_x, right_x = conversion.get_top_bottom_scaled(data_point).sort
107
109
 
108
110
  rect_renderer = Gruff::Renderer::Rectangle.new(color: data_row.color)
109
- rect_renderer.render(left_x, left_y, right_x, right_y)
111
+ rect_renderer.render(left_x + AXIS_MARGIN, left_y, right_x + AXIS_MARGIN, right_y)
110
112
 
111
113
  # Calculate center based on bar_width and current row
114
+ label_center = left_y + bars_width / 2
112
115
 
113
- if @use_data_label
114
- label_center = left_y + bar_width / 2
115
- draw_label(label_center, row_index, store.norm_data[row_index].label)
116
- else
117
- label_center = left_y + bars_width / 2
118
- draw_label(label_center, point_index)
119
- end
116
+ # Subtract half a bar width to center left if requested
117
+ draw_label(label_center, point_index)
120
118
  if @show_labels_for_bar_values
121
- val = (@label_formatting || '%.2f') % store.data[row_index].points[point_index]
122
- draw_value_label(right_x + 40, right_y - bar_width / 2, val.commify, true)
119
+ bar_value_label = Gruff::BarValueLabel::SideBar.new([left_x, left_y, right_x, right_y], store.data[row_index].points[point_index])
120
+ bar_value_label.prepare_rendering(@label_formatting, bar_width) do |x, y, text|
121
+ draw_value_label(x, y, text, true)
122
+ end
123
123
  end
124
124
  end
125
125
  end
@@ -142,11 +142,11 @@ private
142
142
  line_renderer = Gruff::Renderer::Line.new(color: @marker_color, shadow_color: @marker_shadow_color)
143
143
  line_renderer.render(x, @graph_bottom, x, @graph_top)
144
144
 
145
- diff = index - number_of_lines
146
- marker_label = diff.abs * increment + minimum_value
147
-
148
145
  unless @hide_line_numbers
149
- text_renderer = Gruff::Renderer::Text.new(marker_label, font: @font, size: @marker_font_size, color: @font_color)
146
+ diff = index - number_of_lines
147
+ marker_label = BigDecimal(diff.abs.to_s) * BigDecimal(increment.to_s) + BigDecimal(minimum_value.to_s)
148
+ label = x_axis_label(marker_label, @increment)
149
+ text_renderer = Gruff::Renderer::Text.new(label, font: @font, size: @marker_font_size, color: @font_color)
150
150
  text_renderer.add_to_render_queue(0, 0, x, @graph_bottom + LABEL_MARGIN, Magick::CenterGravity)
151
151
  end
152
152
  end
@@ -155,12 +155,9 @@ private
155
155
  ##
156
156
  # Draw on the Y axis instead of the X
157
157
 
158
- def draw_label(y_offset, index, label = nil)
158
+ def draw_label(y_offset, index)
159
159
  draw_unique_label(index) do
160
- lbl = @use_data_label ? label : @labels[index]
161
-
162
- text_renderer = Gruff::Renderer::Text.new(lbl, font: @font, size: @marker_font_size, color: @font_color)
163
- text_renderer.add_to_render_queue(@graph_left - LABEL_MARGIN, 1.0, 0.0, y_offset, Magick::EastGravity)
160
+ draw_label_at(@graph_left - LABEL_MARGIN, 1.0, 0.0, y_offset, @labels[index], Magick::EastGravity)
164
161
  end
165
162
  end
166
163
 
@@ -21,7 +21,6 @@
21
21
  #
22
22
  class Gruff::SideStackedBar < Gruff::SideBar
23
23
  include StackedMixin
24
- include BarValueLabelMixin
25
24
 
26
25
  # Spacing factor applied between bars.
27
26
  attr_writer :bar_spacing
@@ -29,7 +28,7 @@ class Gruff::SideStackedBar < Gruff::SideBar
29
28
  # Number of pixels between bar segments.
30
29
  attr_writer :segment_spacing
31
30
 
32
- # Set the number output format for labels using sprintf.
31
+ # Set the number output format string or lambda.
33
32
  # Default is +"%.2f"+.
34
33
  attr_writer :label_formatting
35
34
 
@@ -80,7 +79,7 @@ private
80
79
  height = Array.new(column_count, 0)
81
80
  length = Array.new(column_count, @graph_left)
82
81
  padding = (bar_width * (1 - @bar_spacing)) / 2
83
- bar_value_label = BarValueLabel.new(column_count, bar_width)
82
+ stack_bar_value_label = Gruff::BarValueLabel::StackedBar.new
84
83
 
85
84
  store.norm_data.each_with_index do |data_row, row_index|
86
85
  data_row.points.each_with_index do |data_point, point_index|
@@ -99,8 +98,8 @@ private
99
98
  length[point_index] += difference
100
99
  height[point_index] += (data_point * @graph_width - 2)
101
100
 
102
- bar_value_label.coordinates[point_index] = [left_x, left_y, right_x, right_y]
103
- bar_value_label.values[point_index] += store.data[row_index].points[point_index]
101
+ bar_value_label = Gruff::BarValueLabel::SideBar.new([left_x, left_y, right_x, right_y], store.data[row_index].points[point_index])
102
+ stack_bar_value_label.add(bar_value_label, point_index)
104
103
 
105
104
  # if a data point is 0 it can result in weird really thing lines
106
105
  # that shouldn't even be there being drawn on top of the existing
@@ -118,7 +117,7 @@ private
118
117
  end
119
118
 
120
119
  if @show_labels_for_bar_values
121
- bar_value_label.prepare_sidebar_rendering(@label_formatting) do |x, y, text|
120
+ stack_bar_value_label.prepare_rendering(@label_formatting, bar_width) do |x, y, text|
122
121
  draw_value_label(x, y, text, true)
123
122
  end
124
123
  end
@@ -12,7 +12,6 @@
12
12
  #
13
13
  class Gruff::StackedBar < Gruff::Base
14
14
  include StackedMixin
15
- include BarValueLabelMixin
16
15
 
17
16
  # Spacing factor applied between bars.
18
17
  attr_writer :bar_spacing
@@ -20,7 +19,7 @@ class Gruff::StackedBar < Gruff::Base
20
19
  # Number of pixels between bar segments.
21
20
  attr_writer :segment_spacing
22
21
 
23
- # Set the number output format for labels using sprintf.
22
+ # Set the number output format string or lambda.
24
23
  # Default is +"%.2f"+.
25
24
  attr_writer :label_formatting
26
25
 
@@ -54,7 +53,7 @@ class Gruff::StackedBar < Gruff::Base
54
53
  padding = (bar_width * (1 - @bar_spacing)) / 2
55
54
 
56
55
  height = Array.new(column_count, 0)
57
- bar_value_label = BarValueLabel.new(column_count, bar_width)
56
+ stack_bar_value_label = Gruff::BarValueLabel::StackedBar.new
58
57
 
59
58
  store.norm_data.each_with_index do |data_row, row_index|
60
59
  data_row.points.each_with_index do |data_point, point_index|
@@ -78,13 +77,13 @@ class Gruff::StackedBar < Gruff::Base
78
77
  label_center = left_x + bar_width * @bar_spacing / 2.0
79
78
  draw_label(label_center, point_index)
80
79
 
81
- bar_value_label.coordinates[point_index] = [left_x, left_y, right_x, right_y]
82
- bar_value_label.values[point_index] += store.data[row_index].points[point_index]
80
+ bar_value_label = Gruff::BarValueLabel::Bar.new([left_x, left_y, right_x, right_y], store.data[row_index].points[point_index])
81
+ stack_bar_value_label.add(bar_value_label, point_index)
83
82
  end
84
83
  end
85
84
 
86
85
  if @show_labels_for_bar_values
87
- bar_value_label.prepare_rendering(@label_formatting) do |x, y, text|
86
+ stack_bar_value_label.prepare_rendering(@label_formatting, bar_width) do |x, y, text|
88
87
  draw_value_label(x, y, text, true)
89
88
  end
90
89
  end
data/lib/gruff/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Gruff
4
- VERSION = '0.12.2'
4
+ VERSION = '0.13.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gruff
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.2
4
+ version: 0.13.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Geoffrey Grosenbach
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-04-24 00:00:00.000000000 Z
12
+ date: 2021-05-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rmagick
@@ -31,28 +31,28 @@ dependencies:
31
31
  requirements:
32
32
  - - "~>"
33
33
  - !ruby/object:Gem::Version
34
- version: 1.11.0
34
+ version: 1.12.1
35
35
  type: :development
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
39
  - - "~>"
40
40
  - !ruby/object:Gem::Version
41
- version: 1.11.0
41
+ version: 1.12.1
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: rubocop-performance
44
44
  requirement: !ruby/object:Gem::Requirement
45
45
  requirements:
46
46
  - - "~>"
47
47
  - !ruby/object:Gem::Version
48
- version: 1.10.1
48
+ version: 1.10.2
49
49
  type: :development
50
50
  prerelease: false
51
51
  version_requirements: !ruby/object:Gem::Requirement
52
52
  requirements:
53
53
  - - "~>"
54
54
  - !ruby/object:Gem::Version
55
- version: 1.10.1
55
+ version: 1.10.2
56
56
  - !ruby/object:Gem::Dependency
57
57
  name: histogram
58
58
  requirement: !ruby/object:Gem::Requirement
@@ -109,6 +109,20 @@ dependencies:
109
109
  - - ">="
110
110
  - !ruby/object:Gem::Version
111
111
  version: '0'
112
+ - !ruby/object:Gem::Dependency
113
+ name: simplecov
114
+ requirement: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
112
126
  - !ruby/object:Gem::Dependency
113
127
  name: yard
114
128
  requirement: !ruby/object:Gem::Requirement
@@ -141,6 +155,9 @@ files:
141
155
  - Gemfile
142
156
  - MIT-LICENSE
143
157
  - README.md
158
+ - assets/fonts/LICENSE.txt
159
+ - assets/fonts/Roboto-Bold.ttf
160
+ - assets/fonts/Roboto-Regular.ttf
144
161
  - assets/plastik/blue.png
145
162
  - assets/plastik/green.png
146
163
  - assets/plastik/red.png
@@ -155,7 +172,7 @@ files:
155
172
  - lib/gruff/bullet.rb
156
173
  - lib/gruff/dot.rb
157
174
  - lib/gruff/helper/bar_conversion.rb
158
- - lib/gruff/helper/bar_value_label_mixin.rb
175
+ - lib/gruff/helper/bar_value_label.rb
159
176
  - lib/gruff/helper/stacked_mixin.rb
160
177
  - lib/gruff/histogram.rb
161
178
  - lib/gruff/line.rb
@@ -1,33 +0,0 @@
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