gruff 0.12.2-java → 0.13.0-java

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 48aea7b38e287c7020e9c5accef465de355ea7604e88234cc3eeacb97a76247d
4
- data.tar.gz: 52f15904875b6ebfa35b98da41a919ad400fdb7faf50ea6c61f1b43dc3011380
3
+ metadata.gz: 46eb3259410a2c7b797d4482f37db956ee9f7b69ab00770d9980522666e58f38
4
+ data.tar.gz: 3ddd8e3ecf26ecf8315173e1c6afc18b37cb6d0d9d487b3ff24a3a4623184e05
5
5
  SHA512:
6
- metadata.gz: 76395c1016e0af0df78eb4960381c955be484b3448f19d46f08aa12613a3e0a6b2ef8b3449e4a881cded7600fb3178ecd6e23eb89fc016a83fae36912361963b
7
- data.tar.gz: f5c52f1b2270cbe9907d4ef0eea38582f5babcf9b81dcac4548ce6a17f6fc79d0067a9f11aadcb1c9c6deca4b9e9edf161b9d439db9af3e6c01d1422e229f978
6
+ metadata.gz: 4792f0f6dc06df61238e6312988bb0ac41994cca4b30feffbda7253fb51d26c7cbbecb8152760a7eef1881e089b83a1976a333e9f1f80e8a1f5377f0155dfe3c
7
+ data.tar.gz: 24f43e7dd7f304fe2a3e44afe56d1f235ba205495de69f7b25fba74280b730619f60af3d51ee854b89ad00a73e9509a3052c514dd4adce56e00bff16c4233a1e
data/.gitignore CHANGED
@@ -1,6 +1,7 @@
1
1
  *~
2
2
  /.idea/
3
3
  /pkg/
4
+ /coverage/
4
5
  /vendor/
5
6
  /test/output/
6
7
  /test/output_java/
data/.rubocop.yml CHANGED
@@ -107,6 +107,9 @@ Style/NumericPredicate:
107
107
  Style/OptionHash:
108
108
  Enabled: false
109
109
 
110
+ Style/RedundantBegin:
111
+ Enabled: false
112
+
110
113
  Style/SingleLineBlockParams:
111
114
  Enabled: false
112
115
 
data/.rubocop_todo.yml CHANGED
@@ -1,6 +1,6 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2021-03-07 06:12:58 UTC using RuboCop version 1.11.0.
3
+ # on 2021-05-07 13:38:24 UTC using RuboCop version 1.12.1.
4
4
  # The point is for the user to remove these configuration records
5
5
  # one by one as the offenses are removed from the code base.
6
6
  # Note that changes in the inspected code, or installation of new
@@ -49,10 +49,10 @@ Lint/Void:
49
49
  Exclude:
50
50
  - 'lib/gruff/patch/rmagick.rb'
51
51
 
52
- # Offense count: 54
52
+ # Offense count: 56
53
53
  # Configuration parameters: IgnoredMethods, CountRepeatedAttributes.
54
54
  Metrics/AbcSize:
55
- Max: 82
55
+ Max: 75
56
56
 
57
57
  # Offense count: 1
58
58
  # Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
@@ -60,17 +60,17 @@ Metrics/AbcSize:
60
60
  Metrics/BlockLength:
61
61
  Max: 29
62
62
 
63
- # Offense count: 13
63
+ # Offense count: 14
64
64
  # Configuration parameters: CountComments, CountAsOne.
65
65
  Metrics/ClassLength:
66
- Max: 637
66
+ Max: 669
67
67
 
68
- # Offense count: 9
68
+ # Offense count: 8
69
69
  # Configuration parameters: IgnoredMethods.
70
70
  Metrics/CyclomaticComplexity:
71
71
  Max: 21
72
72
 
73
- # Offense count: 121
73
+ # Offense count: 126
74
74
  # Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
75
75
  Metrics/MethodLength:
76
76
  Max: 81
@@ -80,7 +80,7 @@ Metrics/MethodLength:
80
80
  Metrics/ParameterLists:
81
81
  Max: 7
82
82
 
83
- # Offense count: 8
83
+ # Offense count: 7
84
84
  # Configuration parameters: IgnoredMethods.
85
85
  Metrics/PerceivedComplexity:
86
86
  Max: 22
@@ -151,7 +151,7 @@ Style/DocumentDynamicEvalDefinition:
151
151
  Exclude:
152
152
  - 'test/gruff_test_case.rb'
153
153
 
154
- # Offense count: 132
154
+ # Offense count: 137
155
155
  # Configuration parameters: RequireForNonPublicMethods.
156
156
  Style/DocumentationMethod:
157
157
  Enabled: false
@@ -180,11 +180,3 @@ Style/StringConcatenation:
180
180
  Exclude:
181
181
  - 'lib/gruff/photo_bar.rb'
182
182
  - 'test/gruff_test_case.rb'
183
-
184
- # Offense count: 2
185
- # Configuration parameters: EnforcedStyle.
186
- # SupportedStyles: forbid_mixed_logical_operators, forbid_logical_operators
187
- Style/UnlessLogicalOperators:
188
- Exclude:
189
- - 'lib/gruff/bar.rb'
190
- - 'lib/gruff/scatter.rb'
data/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # Change Log
2
2
 
3
+ ## 0.13.0
4
+ - Allow to customize label value with lambda (#510)
5
+ - Rename enable_vertical_line_markers attribute to show_vertical_markers (#509)
6
+ - Fix error that cause ArgumentError with x_axis_increment in Gruff::Scatter (#506)
7
+ - Fix draw_label that it should follow label_truncation_style setting (#505)
8
+ - Deprecate use_data_label attribute from SideBar (#504)
9
+ - Add negative values graph support in Gruff::SideBar (#499)
10
+ - Add margin of axis in order to avoid overwriting the coordinate axes (#497)
11
+ - Fix get_type_metrics error (#496)
12
+ - Embed Roboto as default font (#495)
13
+ - Fix legend rendering position when it enable legend_at_bottom (#492)
14
+
3
15
  ## 0.12.2
4
16
  - Avoid SEGV caused with '%S' in Draw#get_type_metrics by old ImageMagick (6.9.9 or below) (#490)
5
17
 
data/README.md CHANGED
@@ -28,6 +28,9 @@ Or install it yourself as:
28
28
  $ gem install gruff
29
29
  ```
30
30
 
31
+ ## Font
32
+ Gruff uses [Roboto](https://fonts.google.com/specimen/Roboto) font as default font which is licensed under the Apache License, Version 2.0.
33
+
31
34
  ## Usage
32
35
 
33
36
  ```ruby
@@ -133,7 +136,7 @@ http://www.rubydoc.info/github/topfunky/gruff/frames
133
136
  We aim to support all Ruby implementations supporting Ruby language level 1.9.3
134
137
  or later. Currently we are running CI for MRI, JRuby, and Rubinius.
135
138
 
136
- ## Development Setup
139
+ ## Development
137
140
  1. Build docker image
138
141
  ```sh
139
142
  $ ./docker/build.sh
@@ -150,6 +153,12 @@ $ bundle install
150
153
  $ bundle exec rake
151
154
  ```
152
155
 
156
+ If you have made changes that involve updating the expected image, you need to update the image with the following command after running tests.
157
+
158
+ ```sh
159
+ $ bundle exec rake test:image:update
160
+ ```
161
+
153
162
  ## Contributing
154
163
 
155
164
  ### Source
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
Binary file
Binary file
data/gruff.gemspec CHANGED
@@ -29,8 +29,8 @@ Gem::Specification.new do |s|
29
29
  s.add_dependency 'rmagick4j'
30
30
  else
31
31
  s.add_dependency 'rmagick'
32
- s.add_development_dependency 'rubocop', '~> 1.11.0'
33
- s.add_development_dependency 'rubocop-performance', '~> 1.10.1'
32
+ s.add_development_dependency 'rubocop', '~> 1.12.1'
33
+ s.add_development_dependency 'rubocop-performance', '~> 1.10.2'
34
34
  end
35
35
  s.add_dependency 'histogram'
36
36
  s.required_ruby_version = '>= 2.4.0'
@@ -38,5 +38,6 @@ Gem::Specification.new do |s|
38
38
  s.add_development_dependency 'rake'
39
39
  s.add_development_dependency 'parallel'
40
40
  s.add_development_dependency 'minitest-reporters'
41
+ s.add_development_dependency 'simplecov'
41
42
  s.add_development_dependency 'yard', '~> 0.9.25'
42
43
  end
data/lib/gruff.rb CHANGED
@@ -13,7 +13,7 @@ require 'gruff/version'
13
13
 
14
14
  helper/bar_conversion.rb
15
15
  helper/stacked_mixin
16
- helper/bar_value_label_mixin
16
+ helper/bar_value_label
17
17
 
18
18
  themes
19
19
  area
data/lib/gruff/bar.rb CHANGED
@@ -16,15 +16,13 @@
16
16
  # g.write('bar.png')
17
17
  #
18
18
  class Gruff::Bar < Gruff::Base
19
- using String::GruffCommify
20
-
21
19
  # Spacing factor applied between bars.
22
20
  attr_writer :bar_spacing
23
21
 
24
22
  # Spacing factor applied between a group of bars belonging to the same label.
25
23
  attr_writer :group_spacing
26
24
 
27
- # Set the number output format for labels using sprintf.
25
+ # Set the number output format string or lambda.
28
26
  # Default is +"%.2f"+.
29
27
  attr_writer :label_formatting
30
28
 
@@ -83,6 +81,9 @@ protected
83
81
  hide_labels?
84
82
  end
85
83
 
84
+ # Value to avoid completely overwriting the coordinate axis
85
+ AXIS_MARGIN = 0.5
86
+
86
87
  def draw_bars
87
88
  # Setup spacing.
88
89
  #
@@ -93,24 +94,10 @@ protected
93
94
  padding = (bar_width * (1 - @bar_spacing)) / 2
94
95
 
95
96
  # Setup the BarConversion Object
96
- conversion = Gruff::BarConversion.new
97
- conversion.graph_height = @graph_height
98
- conversion.graph_top = @graph_top
99
-
100
- # Set up the right mode [1,2,3] see BarConversion for further explanation
101
- if minimum_value >= 0
102
- # all bars go from zero to positive
103
- conversion.mode = 1
104
- elsif maximum_value <= 0
105
- # all bars go from 0 to negative
106
- conversion.mode = 2
107
- else
108
- # bars either go from zero to negative or to positive
109
- conversion.mode = 3
110
- conversion.spread = @spread
111
- conversion.minimum_value = minimum_value
112
- conversion.zero = -minimum_value / @spread
113
- end
97
+ conversion = Gruff::BarConversion.new(
98
+ top: @graph_top, bottom: @graph_bottom,
99
+ minimum_value: minimum_value, maximum_value: maximum_value, spread: @spread
100
+ )
114
101
 
115
102
  # iterate over all normalised data
116
103
  store.norm_data.each_with_index do |data_row, row_index|
@@ -122,11 +109,11 @@ protected
122
109
  left_x = @graph_left + (bar_width * (row_index + point_index + ((store.length - 1) * point_index))) + padding + group_spacing
123
110
  right_x = left_x + bar_width * @bar_spacing
124
111
  # y
125
- left_y, right_y = conversion.get_left_y_right_y_scaled(data_point)
112
+ left_y, right_y = conversion.get_top_bottom_scaled(data_point)
126
113
 
127
114
  # create new bar
128
115
  rect_renderer = Gruff::Renderer::Rectangle.new(color: data_row.color)
129
- rect_renderer.render(left_x, left_y, right_x, right_y)
116
+ rect_renderer.render(left_x, left_y - AXIS_MARGIN, right_x, right_y - AXIS_MARGIN)
130
117
 
131
118
  # Calculate center based on bar_width and current row
132
119
  label_center = @graph_left + group_spacing + (store.length * bar_width * point_index) + (store.length * bar_width / 2.0)
@@ -134,10 +121,10 @@ protected
134
121
  # Subtract half a bar width to center left if requested
135
122
  draw_label(label_center, point_index)
136
123
  if @show_labels_for_bar_values
137
- raw_value = store.data[row_index].points[point_index]
138
- val = (@label_formatting || '%.2f') % raw_value
139
- y = raw_value >= 0 ? left_y - 30 : left_y + 12
140
- draw_value_label(left_x + (right_x - left_x) / 2, y, val.commify, true)
124
+ bar_value_label = Gruff::BarValueLabel::Bar.new([left_x, left_y, right_x, right_y], store.data[row_index].points[point_index])
125
+ bar_value_label.prepare_rendering(@label_formatting, bar_width) do |x, y, text|
126
+ draw_value_label(x, y, text, true)
127
+ end
141
128
  end
142
129
  end
143
130
  end
data/lib/gruff/base.rb CHANGED
@@ -156,9 +156,11 @@ module Gruff
156
156
  # Will be scaled down if graph is smaller than 800px wide.
157
157
  attr_writer :legend_box_size
158
158
 
159
- # With Side Bars use the data label for the marker value to the left of the bar.
160
- # Default is +false+.
161
- attr_writer :use_data_label
159
+ # Allow passing lambdas to format labels for x axis.
160
+ attr_writer :x_axis_label_format
161
+
162
+ # Allow passing lambdas to format labels for y axis.
163
+ attr_writer :y_axis_label_format
162
164
 
163
165
  # If one numerical argument is given, the graph is drawn at 4/3 ratio
164
166
  # according to the given width (+800+ results in 800x600, +400+ gives 400x300,
@@ -214,8 +216,8 @@ module Gruff
214
216
  @sort = false
215
217
  @sorted_drawing = false
216
218
  @title = nil
217
- @title_font = nil
218
219
 
220
+ @title_font = nil
219
221
  @font = nil
220
222
  @bold_title = true
221
223
 
@@ -238,10 +240,12 @@ module Gruff
238
240
  @label_max_size = 0
239
241
  @label_truncation_style = :absolute
240
242
 
241
- @use_data_label = false
242
243
  @x_axis_increment = nil
243
244
  @x_axis_label = @y_axis_label = nil
244
245
  @y_axis_increment = nil
246
+
247
+ @x_axis_label_format = nil
248
+ @y_axis_label_format = nil
245
249
  end
246
250
  protected :initialize_ivars
247
251
 
@@ -603,7 +607,7 @@ module Gruff
603
607
 
604
608
  unless @hide_line_numbers
605
609
  marker_label = BigDecimal(index.to_s) * BigDecimal(@increment.to_s) + BigDecimal(minimum_value.to_s)
606
- label = label(marker_label, @increment)
610
+ label = y_axis_label(marker_label, @increment)
607
611
  text_renderer = Gruff::Renderer::Text.new(label, font: @font, size: @marker_font_size, color: @font_color)
608
612
  text_renderer.add_to_render_queue(@graph_left - LABEL_MARGIN, 1.0, 0.0, y, Magick::EastGravity)
609
613
  end
@@ -627,7 +631,7 @@ module Gruff
627
631
  current_x_offset = center(label_widths.first.sum)
628
632
  current_y_offset = begin
629
633
  if @legend_at_bottom
630
- @graph_height + @title_margin
634
+ @graph_bottom + @legend_margin + @legend_caps_height + LABEL_MARGIN
631
635
  else
632
636
  hide_title? ? @top_margin + @title_margin : @top_margin + @title_margin + @title_caps_height
633
637
  end
@@ -659,8 +663,6 @@ module Gruff
659
663
  unless label_widths.empty?
660
664
  # Wrap to next line and shrink available graph dimensions
661
665
  current_y_offset += line_height
662
- @graph_top += line_height
663
- @graph_height = @graph_bottom - @graph_top
664
666
  end
665
667
  end
666
668
  end
@@ -674,7 +676,7 @@ module Gruff
674
676
  font_weight = @bold_title ? Magick::BoldWeight : Magick::NormalWeight
675
677
  font_size = @title_font_size
676
678
 
677
- metrics = Renderer::Text.metrics(@title, font_size, font_weight)
679
+ metrics = Renderer::Text.metrics(@title, font, font_size, font_weight)
678
680
  if metrics.width > @raw_columns
679
681
  font_size = font_size * (@raw_columns / metrics.width) * 0.95
680
682
  end
@@ -694,11 +696,8 @@ module Gruff
694
696
  # TODO: See if index.odd? is the best stragegy
695
697
  y_offset += @label_stagger_height if index.odd?
696
698
 
697
- label_text = truncate_label_text(@labels[index].to_s)
698
-
699
699
  if x_offset >= @graph_left && x_offset <= @graph_right
700
- text_renderer = Gruff::Renderer::Text.new(label_text, font: @font, size: @marker_font_size, color: @font_color)
701
- text_renderer.add_to_render_queue(1.0, 1.0, x_offset, y_offset, gravity)
700
+ draw_label_at(1.0, 1.0, x_offset, y_offset, @labels[index], gravity)
702
701
  end
703
702
  end
704
703
  end
@@ -713,6 +712,12 @@ module Gruff
713
712
  end
714
713
  end
715
714
 
715
+ def draw_label_at(width, height, x, y, text, gravity = Magick::NorthGravity)
716
+ label_text = truncate_label_text(text)
717
+ text_renderer = Gruff::Renderer::Text.new(label_text, font: @font, size: @marker_font_size, color: @font_color)
718
+ text_renderer.add_to_render_queue(width, height, x, y, gravity)
719
+ end
720
+
716
721
  # Draws the data value over the data point in bar graphs
717
722
  def draw_value_label(x_offset, y_offset, data_point, bar_value = false)
718
723
  return if @hide_line_markers && !bar_value
@@ -818,10 +823,10 @@ module Gruff
818
823
  if @has_left_labels
819
824
  @labels.values.reduce('') { |value, memo| (value.to_s.length > memo.to_s.length) ? value : memo }
820
825
  else
821
- label(maximum_value.to_f, @increment)
826
+ y_axis_label(maximum_value.to_f, @increment)
822
827
  end
823
828
  end
824
- longest_left_label_width = calculate_width(@marker_font_size, text)
829
+ longest_left_label_width = calculate_width(@marker_font_size, truncate_label_text(text))
825
830
  longest_left_label_width *= 1.25 if @has_left_labels
826
831
 
827
832
  # Shift graph if left line numbers are hidden
@@ -831,17 +836,16 @@ module Gruff
831
836
  end
832
837
 
833
838
  def setup_top_margin
834
- return @top_margin if @legend_at_bottom
835
-
836
839
  # When @hide title, leave a title_margin space for aesthetics.
837
840
  # Same with @hide_legend
838
841
  @top_margin +
839
842
  (hide_title? ? @title_margin : @title_caps_height + @title_margin) +
840
- (@hide_legend ? @legend_margin : @legend_caps_height + @legend_margin)
843
+ ((@hide_legend || @legend_at_bottom) ? @legend_margin : calculate_legend_height + @legend_margin)
841
844
  end
842
845
 
843
846
  def setup_bottom_margin
844
847
  graph_bottom_margin = hide_bottom_label_area? ? @bottom_margin : @bottom_margin + @marker_caps_height + LABEL_MARGIN
848
+ graph_bottom_margin += (calculate_legend_height + @legend_margin) if @legend_at_bottom
845
849
 
846
850
  x_axis_label_height = @x_axis_label.nil? ? 0.0 : @marker_caps_height + LABEL_MARGIN
847
851
  # FIXME: Consider chart types other than bar
@@ -849,6 +853,7 @@ module Gruff
849
853
  end
850
854
 
851
855
  def truncate_label_text(text)
856
+ text = text.to_s
852
857
  return text if text.size <= @label_max_size
853
858
 
854
859
  if @label_truncation_style == :trailing_dots
@@ -892,6 +897,22 @@ module Gruff
892
897
  parts.join('.')
893
898
  end
894
899
 
900
+ def x_axis_label(value, increment)
901
+ if @x_axis_label_format
902
+ @x_axis_label_format.call(value)
903
+ else
904
+ label(value, increment)
905
+ end
906
+ end
907
+
908
+ def y_axis_label(value, increment)
909
+ if @y_axis_label_format
910
+ @y_axis_label_format.call(value)
911
+ else
912
+ label(value, increment)
913
+ end
914
+ end
915
+
895
916
  def calculate_legend_label_widths_for_each_line(legend_labels, legend_square_width)
896
917
  # May fix legend drawing problem at small sizes
897
918
  label_widths = [[]] # Used to calculate line wrap
@@ -908,13 +929,38 @@ module Gruff
908
929
  label_widths
909
930
  end
910
931
 
932
+ def calculate_legend_height
933
+ return 0.0 if @hide_legend
934
+
935
+ legend_labels = store.data.map(&:label)
936
+ legend_square_width = @legend_box_size
937
+ label_widths = calculate_legend_label_widths_for_each_line(legend_labels, legend_square_width)
938
+ legend_height = 0.0
939
+
940
+ legend_labels.each_with_index do |legend_label, _index|
941
+ next if legend_label.empty?
942
+
943
+ label_widths.first.shift
944
+ if label_widths.first.empty?
945
+ label_widths.shift
946
+ line_height = [@legend_caps_height, legend_square_width].max + @legend_margin
947
+ unless label_widths.empty?
948
+ # Wrap to next line and shrink available graph dimensions
949
+ legend_height += line_height
950
+ end
951
+ end
952
+ end
953
+
954
+ legend_height + @legend_caps_height
955
+ end
956
+
911
957
  # Returns the height of the capital letter 'X' for the current font and
912
958
  # size.
913
959
  #
914
960
  # Not scaled since it deals with dimensions that the regular scaling will
915
961
  # handle.
916
962
  def calculate_caps_height(font_size)
917
- metrics = Renderer::Text.metrics('X', font_size)
963
+ metrics = Renderer::Text.metrics('X', @font, font_size)
918
964
  metrics.height
919
965
  end
920
966
 
@@ -926,7 +972,7 @@ module Gruff
926
972
  text = text.to_s
927
973
  return 0 if text.empty?
928
974
 
929
- metrics = Renderer::Text.metrics(text, font_size)
975
+ metrics = Renderer::Text.metrics(text, @font, font_size)
930
976
  metrics.width
931
977
  end
932
978
 
data/lib/gruff/dot.rb CHANGED
@@ -51,14 +51,14 @@ protected
51
51
  return if @hide_line_markers
52
52
 
53
53
  (0..marker_count).each do |index|
54
- marker_label = minimum_value + index * @increment
54
+ marker_label = BigDecimal(index.to_s) * BigDecimal(@increment.to_s) + BigDecimal(minimum_value.to_s)
55
55
  x = @graph_left + (marker_label - minimum_value) * @graph_width / @spread
56
56
 
57
57
  line_renderer = Gruff::Renderer::Line.new(color: @marker_color, shadow_color: @marker_shadow_color)
58
58
  line_renderer.render(x, @graph_bottom, x, @graph_bottom + 5)
59
59
 
60
60
  unless @hide_line_numbers
61
- label = label(marker_label, @increment)
61
+ label = y_axis_label(marker_label, @increment)
62
62
  text_renderer = Gruff::Renderer::Text.new(label, font: @font, size: @marker_font_size, color: @font_color)
63
63
  text_renderer.add_to_render_queue(0, 0, x, @graph_bottom + (LABEL_MARGIN * 1.5), Magick::CenterGravity)
64
64
  end
@@ -70,8 +70,7 @@ protected
70
70
 
71
71
  def draw_label(y_offset, index)
72
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)
73
+ draw_label_at(@graph_left - LABEL_MARGIN, 1.0, 0.0, y_offset, @labels[index], Magick::EastGravity)
75
74
  end
76
75
  end
77
76
  end
@@ -14,29 +14,44 @@
14
14
  # @private
15
15
  class Gruff::BarConversion
16
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
17
 
23
- def get_left_y_right_y_scaled(data_point)
18
+ def initialize(top:, bottom:, minimum_value:, maximum_value:, spread:)
19
+ @graph_top = top
20
+ @graph_height = bottom - top
21
+ @spread = spread
22
+ @minimum_value = minimum_value
23
+ @maximum_value = maximum_value
24
+
25
+ if minimum_value >= 0
26
+ # all bars go from zero to positive
27
+ @mode = 1
28
+ elsif maximum_value <= 0
29
+ # all bars go from 0 to negative
30
+ @mode = 2
31
+ else
32
+ # bars either go from zero to negative or to positive
33
+ @mode = 3
34
+ @zero = -minimum_value / @spread
35
+ end
36
+ end
37
+
38
+ def get_top_bottom_scaled(data_point)
24
39
  result = []
25
40
 
26
41
  case @mode
27
42
  when 1
28
43
  # 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
44
+ result[0] = @graph_top + @graph_height * (1 - data_point)
45
+ result[1] = @graph_top + @graph_height
31
46
  when 2
32
47
  # only negative values
33
- result[0] = @graph_top + 1
34
- result[1] = @graph_top + @graph_height * (1 - data_point) - 1
48
+ result[0] = @graph_top
49
+ result[1] = @graph_top + @graph_height * (1 - data_point)
35
50
  when 3
36
51
  # positive and negative values
37
52
  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
53
+ result[0] = @graph_top + @graph_height * (1 - (val - @zero))
54
+ result[1] = @graph_top + @graph_height * (1 - @zero)
40
55
  else
41
56
  result[0] = 0.0
42
57
  result[1] = 0.0
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @private
4
+ module Gruff::BarValueLabel
5
+ using String::GruffCommify
6
+
7
+ # @private
8
+ class Base
9
+ attr_reader :coordinate, :value
10
+
11
+ def initialize(coordinate, value)
12
+ @coordinate = coordinate
13
+ @value = value
14
+ end
15
+ end
16
+
17
+ # @private
18
+ class Bar < Base
19
+ def prepare_rendering(format, _bar_width = 0)
20
+ left_x, left_y, right_x, _right_y = @coordinate
21
+ if format.is_a?(Proc)
22
+ val = format.call(@value)
23
+ else
24
+ val = sprintf(format || '%.2f', @value).commify
25
+ end
26
+
27
+ y = @value >= 0 ? left_y - 30 : left_y + 12
28
+ yield left_x + (right_x - left_x) / 2, y, val
29
+ end
30
+ end
31
+
32
+ # @private
33
+ class SideBar < Base
34
+ def prepare_rendering(format, bar_width = 0)
35
+ left_x, _left_y, right_x, right_y = @coordinate
36
+ if format.is_a?(Proc)
37
+ val = format.call(@value)
38
+ else
39
+ val = sprintf(format || '%.2f', @value).commify
40
+ end
41
+
42
+ x = @value >= 0 ? right_x + 40 : left_x - 40
43
+ yield x, right_y - bar_width / 2, val
44
+ end
45
+ end
46
+
47
+ # @private
48
+ class StackedBar
49
+ def initialize
50
+ @bars = []
51
+ end
52
+
53
+ def add(bar, index)
54
+ bars = @bars[index] || []
55
+ bars << bar
56
+ @bars[index] = bars
57
+ end
58
+
59
+ def prepare_rendering(format, bar_width = 0, &block)
60
+ @bars.each do |bars|
61
+ value = bars.sum(&:value)
62
+ bar = bars.last
63
+ bar_value_label = bar.class.new(bar.coordinate, value)
64
+ bar_value_label.prepare_rendering(format, bar_width, &block)
65
+ end
66
+ end
67
+ end
68
+ end
@@ -34,7 +34,7 @@ module Gruff
34
34
  draw.rotation = @rotation if @rotation
35
35
  draw.fill = @font_color
36
36
  draw.stroke = 'transparent'
37
- draw.font = @font if @font
37
+ draw.font = @font || Renderer::Text.default_font(@font_weight)
38
38
  draw.font_weight = @font_weight
39
39
  draw.pointsize = @font_size * scale
40
40
  draw.gravity = gravity
@@ -45,10 +45,11 @@ module Gruff
45
45
  draw.rotation = -@rotation if @rotation
46
46
  end
47
47
 
48
- def self.metrics(text, size, font_weight = Magick::NormalWeight)
48
+ def self.metrics(text, font, size, font_weight = Magick::NormalWeight)
49
49
  draw = Renderer.instance.draw
50
50
  image = Renderer.instance.image
51
51
 
52
+ draw.font = font || Renderer::Text.default_font(font_weight)
52
53
  draw.font_weight = font_weight
53
54
  draw.pointsize = size
54
55
 
@@ -60,5 +61,12 @@ module Gruff
60
61
 
61
62
  draw.get_type_metrics(image, text)
62
63
  end
64
+
65
+ FONT_BOLD = File.expand_path(File.join(__FILE__, '../../../../assets/fonts/Roboto-Bold.ttf'))
66
+ FONT_REGULAR = File.expand_path(File.join(__FILE__, '../../../../assets/fonts/Roboto-Regular.ttf'))
67
+
68
+ def self.default_font(font_weight)
69
+ (font_weight == Magick::BoldWeight) ? FONT_BOLD : FONT_REGULAR
70
+ end
63
71
  end
64
72
  end
data/lib/gruff/scatter.rb CHANGED
@@ -28,17 +28,13 @@ class Gruff::Scatter < Gruff::Base
28
28
  # This is useful when working with a small range of high values (for example, a date range of months, while seconds as units).
29
29
  attr_writer :disable_significant_rounding_x_axis
30
30
 
31
- # Allow enabling vertical lines. When you have a lot of data, they can work great.
32
- attr_writer :enable_vertical_line_markers
31
+ # Allow for vertical marker lines.
32
+ attr_writer :show_vertical_markers
33
33
 
34
34
  # Allow using vertical labels in the X axis (and setting the label margin).
35
35
  attr_writer :x_label_margin
36
36
  attr_writer :use_vertical_x_labels
37
37
 
38
- # Allow passing lambdas to format labels.
39
- attr_writer :y_axis_label_format
40
- attr_writer :x_axis_label_format
41
-
42
38
  def initialize_store
43
39
  @store = Gruff::Store.new(Gruff::Store::XYData)
44
40
  end
@@ -51,17 +47,22 @@ class Gruff::Scatter < Gruff::Base
51
47
  @baseline_x_value = @baseline_y_value = nil
52
48
  @circle_radius = nil
53
49
  @disable_significant_rounding_x_axis = false
54
- @enable_vertical_line_markers = false
50
+ @show_vertical_markers = false
55
51
  @marker_x_count = nil
56
52
  @maximum_x_value = @minimum_x_value = nil
57
53
  @stroke_width = nil
58
54
  @use_vertical_x_labels = false
59
- @x_axis_label_format = nil
60
55
  @x_label_margin = nil
61
- @y_axis_label_format = nil
62
56
  end
63
57
  private :initialize_ivars
64
58
 
59
+ # Allow enabling vertical lines. When you have a lot of data, they can work great.
60
+ # @deprecated Please use +show_vertical_markers+ attribute instead.
61
+ def enable_vertical_line_markers=(value)
62
+ warn '#enable_vertical_line_markers= is deprecated. Please use `show_vertical_markers` attribute instead'
63
+ @show_vertical_markers = value
64
+ end
65
+
65
66
  def draw
66
67
  super
67
68
  return unless data_given?
@@ -191,7 +192,7 @@ private
191
192
  calculate_spread
192
193
  normalize
193
194
 
194
- self.marker_count = (@x_spread / @x_axis_increment).to_i
195
+ @marker_x_count = (@x_spread / @x_axis_increment).to_i
195
196
  @x_increment = @x_axis_increment
196
197
  end
197
198
  increment_x_scaled = @graph_width.to_f / (@x_spread / @x_increment)
@@ -199,7 +200,7 @@ private
199
200
  # Draw vertical line markers and annotate with numbers
200
201
  (0..@marker_x_count).each do |index|
201
202
  # TODO: Fix the vertical lines, and enable them by default. Not pretty when they don't match up with top y-axis line
202
- if @enable_vertical_line_markers
203
+ if @show_vertical_markers
203
204
  x = @graph_left + @graph_width - index.to_f * increment_x_scaled
204
205
 
205
206
  line_renderer = Gruff::Renderer::Line.new(color: @marker_color, shadow_color: @marker_shadow_color)
@@ -207,11 +208,11 @@ private
207
208
  end
208
209
 
209
210
  unless @hide_line_numbers
210
- marker_label = index * @x_increment + @minimum_x_value.to_f
211
+ marker_label = BigDecimal(index.to_s) * BigDecimal(@x_increment.to_s) + BigDecimal(@minimum_x_value.to_s)
211
212
  y_offset = @graph_bottom + (@x_label_margin || LABEL_MARGIN)
212
213
  x_offset = get_x_coord(index.to_f, increment_x_scaled, @graph_left)
213
214
 
214
- label = vertical_label(marker_label, @x_increment)
215
+ label = x_axis_label(marker_label, @x_increment)
215
216
  rotation = -90.0 if @use_vertical_x_labels
216
217
  text_renderer = Gruff::Renderer::Text.new(label, font: @font, size: @marker_font_size, color: @font_color, rotation: rotation)
217
218
  text_renderer.add_to_render_queue(1.0, 1.0, x_offset, y_offset)
@@ -219,22 +220,6 @@ private
219
220
  end
220
221
  end
221
222
 
222
- def label(value, increment)
223
- if @y_axis_label_format
224
- @y_axis_label_format.call(value)
225
- else
226
- super
227
- end
228
- end
229
-
230
- def vertical_label(value, increment)
231
- if @x_axis_label_format
232
- @x_axis_label_format.call(value)
233
- else
234
- label(value, increment)
235
- end
236
- end
237
-
238
223
  def get_x_coord(x_data_point, width, offset) #:nodoc:
239
224
  x_data_point * width + offset
240
225
  end
@@ -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: java
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
  requirement: !ruby/object:Gem::Requirement
@@ -81,6 +81,20 @@ dependencies:
81
81
  - - ">="
82
82
  - !ruby/object:Gem::Version
83
83
  version: '0'
84
+ - !ruby/object:Gem::Dependency
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ name: simplecov
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
84
98
  - !ruby/object:Gem::Dependency
85
99
  requirement: !ruby/object:Gem::Requirement
86
100
  requirements:
@@ -113,6 +127,9 @@ files:
113
127
  - Gemfile
114
128
  - MIT-LICENSE
115
129
  - README.md
130
+ - assets/fonts/LICENSE.txt
131
+ - assets/fonts/Roboto-Bold.ttf
132
+ - assets/fonts/Roboto-Regular.ttf
116
133
  - assets/plastik/blue.png
117
134
  - assets/plastik/green.png
118
135
  - assets/plastik/red.png
@@ -127,7 +144,7 @@ files:
127
144
  - lib/gruff/bullet.rb
128
145
  - lib/gruff/dot.rb
129
146
  - lib/gruff/helper/bar_conversion.rb
130
- - lib/gruff/helper/bar_value_label_mixin.rb
147
+ - lib/gruff/helper/bar_value_label.rb
131
148
  - lib/gruff/helper/stacked_mixin.rb
132
149
  - lib/gruff/histogram.rb
133
150
  - 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