gruff 0.16.0 → 0.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'helper/stacked_mixin'
4
+
3
5
  #
4
6
  # Here's how to set up a Gruff::StackedBar.
5
7
  #
@@ -42,6 +44,15 @@ private
42
44
  @minimum_value = 0.0
43
45
  end
44
46
 
47
+ def setup_drawing
48
+ # Labels will be centered over the left of the bar if
49
+ # there are more labels than columns. This is basically the same
50
+ # as where it would be for a line graph.
51
+ @center_labels_over_point = (@labels.keys.length > column_count)
52
+
53
+ super
54
+ end
55
+
45
56
  def setup_data
46
57
  calculate_maximum_by_stack
47
58
  super
@@ -52,8 +63,6 @@ private
52
63
  return if @hide_line_markers
53
64
 
54
65
  if @show_labels_for_bar_values
55
- proc_text_metrics = ->(text) { text_metrics(@marker_font, text) }
56
-
57
66
  if maximum_value >= 0
58
67
  _, metrics = Gruff::BarValueLabel.metrics(maximum_value, @label_formatting, proc_text_metrics)
59
68
  @graph_top += metrics.height
@@ -71,44 +80,39 @@ private
71
80
  bar_width = @graph_width / column_count
72
81
  padding = (bar_width * (1 - @bar_spacing)) / 2
73
82
 
74
- height = Array.new(column_count, 0)
75
- length = Array.new(column_count, @graph_bottom)
76
- stack_bar_value_labels = Gruff::BarValueLabel::StackedBar.new
77
-
78
- store.norm_data.each_with_index do |data_row, row_index|
79
- data_row.points.each_with_index do |data_point, point_index|
80
- temp1 = @graph_top + (@graph_height - (data_point * @graph_height) - height[point_index])
81
- temp2 = @graph_top + @graph_height - height[point_index]
82
- difference = temp2 - temp1
83
- difference = 0 if difference < 0
84
-
85
- # Use incremented x and scaled y
86
- left_x = @graph_left + (bar_width * point_index) + padding
87
- left_y = length[point_index] - difference
88
- right_x = left_x + (bar_width * @bar_spacing)
89
- right_y = length[point_index]
90
- right_y -= @segment_spacing if row_index != store.columns - 1
91
-
92
- # update the total height of the current stacked bar
93
- length[point_index] -= difference
94
- height[point_index] += (data_point * @graph_height)
95
-
96
- rect_renderer = Gruff::Renderer::Rectangle.new(renderer, color: data_row.color)
97
- rect_renderer.render(left_x, left_y, right_x, right_y)
98
-
99
- # Calculate center based on bar_width and current row
100
- label_center = left_x + (bar_width * @bar_spacing / 2.0)
101
- draw_label(label_center, point_index)
102
-
103
- bar_value_label = Gruff::BarValueLabel::Bar.new([left_x, left_y, right_x, right_y], store.data[row_index].points[point_index])
104
- stack_bar_value_labels.add(bar_value_label, point_index)
83
+ # Setup the BarConversion Object
84
+ conversion = Gruff::BarConversion.new(
85
+ top: @graph_top, bottom: @graph_bottom,
86
+ minimum_value: minimum_value, maximum_value: maximum_value, spread: @spread
87
+ )
88
+
89
+ normalized_stacked_bars.each_with_index do |stacked_bars, stacked_index|
90
+ total = 0
91
+ left_x = @graph_left + (bar_width * stacked_index) + padding
92
+ right_x = left_x + (bar_width * @bar_spacing)
93
+
94
+ top_y = 0
95
+ stacked_bars.each do |bar|
96
+ next if bar.point == 0
97
+
98
+ bottom_y, = conversion.get_top_bottom_scaled(total)
99
+ bottom_y -= @segment_spacing
100
+ top_y, = conversion.get_top_bottom_scaled(total + bar.point)
101
+
102
+ rect_renderer = Gruff::Renderer::Rectangle.new(renderer, color: bar.color)
103
+ rect_renderer.render(left_x, bottom_y, right_x, top_y)
104
+
105
+ total += bar.point
105
106
  end
106
- end
107
107
 
108
- if @show_labels_for_bar_values
109
- proc_text_metrics = ->(text) { text_metrics(@marker_font, text) }
110
- stack_bar_value_labels.prepare_rendering(@label_formatting, proc_text_metrics) do |x, y, text, _text_width, text_height|
111
- draw_value_label(bar_width * @bar_spacing, text_height, x, y, text)
108
+ label_center = left_x + (bar_width * @bar_spacing / 2.0)
109
+ draw_label(label_center, stacked_index)
110
+
111
+ if @show_labels_for_bar_values
112
+ bar_value_label = Gruff::BarValueLabel::Bar.new([left_x, top_y, right_x, @graph_bottom], stacked_bars.sum(&:value))
113
+ bar_value_label.prepare_rendering(@label_formatting, proc_text_metrics) do |x, y, text, _text_width, text_height|
114
+ draw_value_label(bar_width * @bar_spacing, text_height, x, y, text)
115
+ end
112
116
  end
113
117
  end
114
118
  end
@@ -124,4 +128,8 @@ private
124
128
  def hide_bottom_label_area?
125
129
  hide_labels? && @x_axis_label.nil? && @legend_at_bottom == false
126
130
  end
131
+
132
+ def proc_text_metrics
133
+ ->(text) { text_metrics(@marker_font, text) }
134
+ end
127
135
  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.16.0'
4
+ VERSION = '0.17.0'
5
5
  end
data/lib/gruff.rb CHANGED
@@ -21,9 +21,6 @@ module Gruff
21
21
 
22
22
  autoload :BarConversion, Gruff.libpath('helper/bar_conversion')
23
23
  autoload :BarValueLabel, Gruff.libpath('helper/bar_value_label')
24
- class Base
25
- autoload :StackedMixin, Gruff.libpath('helper/stacked_mixin')
26
- end
27
24
 
28
25
  autoload :AccumulatorBar, Gruff.libpath('accumulator_bar')
29
26
  autoload :Area, Gruff.libpath('area')
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gruff
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.16.0
4
+ version: 0.17.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Geoffrey Grosenbach
8
8
  - Uwe Kubosch
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2022-05-02 00:00:00.000000000 Z
12
+ date: 2022-06-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rmagick
@@ -31,14 +31,14 @@ dependencies:
31
31
  requirements:
32
32
  - - "~>"
33
33
  - !ruby/object:Gem::Version
34
- version: 1.27.0
34
+ version: 1.28.2
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.27.0
41
+ version: 1.28.2
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: rubocop-performance
44
44
  requirement: !ruby/object:Gem::Requirement
@@ -129,14 +129,14 @@ dependencies:
129
129
  requirements:
130
130
  - - "~>"
131
131
  - !ruby/object:Gem::Version
132
- version: 0.9.25
132
+ version: 0.9.28
133
133
  type: :development
134
134
  prerelease: false
135
135
  version_requirements: !ruby/object:Gem::Requirement
136
136
  requirements:
137
137
  - - "~>"
138
138
  - !ruby/object:Gem::Version
139
- version: 0.9.25
139
+ version: 0.9.28
140
140
  description: Beautiful graphs for one or multiple datasets. Can be used on websites
141
141
  or in documents.
142
142
  email: boss@topfunky.com
@@ -171,6 +171,7 @@ files:
171
171
  - lib/gruff/dot.rb
172
172
  - lib/gruff/font.rb
173
173
  - lib/gruff/helper/bar_conversion.rb
174
+ - lib/gruff/helper/bar_mixin.rb
174
175
  - lib/gruff/helper/bar_value_label.rb
175
176
  - lib/gruff/helper/stacked_mixin.rb
176
177
  - lib/gruff/histogram.rb
@@ -213,7 +214,7 @@ licenses:
213
214
  - MIT
214
215
  metadata:
215
216
  rubygems_mfa_required: 'true'
216
- post_install_message:
217
+ post_install_message:
217
218
  rdoc_options: []
218
219
  require_paths:
219
220
  - lib
@@ -229,7 +230,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
229
230
  version: '0'
230
231
  requirements: []
231
232
  rubygems_version: 3.3.7
232
- signing_key:
233
+ signing_key:
233
234
  specification_version: 4
234
235
  summary: Beautiful graphs for one or multiple datasets.
235
236
  test_files: []