gruff 0.13.0-java → 0.14.0-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +66 -0
- data/.rubocop.yml +6 -10
- data/.rubocop_todo.yml +19 -85
- data/CHANGELOG.md +10 -0
- data/README.md +6 -7
- data/gruff.gemspec +2 -4
- data/lib/gruff.rb +1 -0
- data/lib/gruff/accumulator_bar.rb +3 -1
- data/lib/gruff/area.rb +5 -8
- data/lib/gruff/bar.rb +19 -23
- data/lib/gruff/base.rb +134 -96
- data/lib/gruff/bezier.rb +4 -6
- data/lib/gruff/bullet.rb +11 -10
- data/lib/gruff/dot.rb +10 -10
- data/lib/gruff/font.rb +39 -0
- data/lib/gruff/helper/stacked_mixin.rb +1 -2
- data/lib/gruff/histogram.rb +9 -7
- data/lib/gruff/line.rb +55 -46
- data/lib/gruff/mini/bar.rb +9 -6
- data/lib/gruff/mini/legend.rb +12 -8
- data/lib/gruff/mini/pie.rb +9 -6
- data/lib/gruff/mini/side_bar.rb +9 -6
- data/lib/gruff/net.rb +9 -15
- data/lib/gruff/patch/string.rb +1 -1
- data/lib/gruff/pie.rb +23 -65
- data/lib/gruff/renderer/bezier.rb +8 -9
- data/lib/gruff/renderer/circle.rb +8 -9
- data/lib/gruff/renderer/dash_line.rb +9 -10
- data/lib/gruff/renderer/dot.rb +13 -14
- data/lib/gruff/renderer/ellipse.rb +8 -9
- data/lib/gruff/renderer/line.rb +8 -9
- data/lib/gruff/renderer/polygon.rb +9 -10
- data/lib/gruff/renderer/polyline.rb +8 -9
- data/lib/gruff/renderer/rectangle.rb +7 -8
- data/lib/gruff/renderer/renderer.rb +21 -36
- data/lib/gruff/renderer/text.rb +21 -37
- data/lib/gruff/scatter.rb +41 -46
- data/lib/gruff/scene.rb +15 -13
- data/lib/gruff/side_bar.rb +14 -30
- data/lib/gruff/side_stacked_bar.rb +8 -11
- data/lib/gruff/spider.rb +11 -16
- data/lib/gruff/stacked_area.rb +10 -11
- data/lib/gruff/stacked_bar.rb +9 -9
- data/lib/gruff/store/store.rb +6 -10
- data/lib/gruff/themes.rb +6 -6
- data/lib/gruff/version.rb +1 -1
- data/rails_generators/gruff/templates/controller.rb +1 -1
- metadata +4 -21
- data/.travis.yml +0 -23
- data/assets/plastik/blue.png +0 -0
- data/assets/plastik/green.png +0 -0
- data/assets/plastik/red.png +0 -0
- data/lib/gruff/photo_bar.rb +0 -93
data/lib/gruff/bar.rb
CHANGED
@@ -33,7 +33,21 @@ class Gruff::Bar < Gruff::Base
|
|
33
33
|
# Prevent drawing of column labels below a bar graph. Default is +false+.
|
34
34
|
attr_writer :hide_labels
|
35
35
|
|
36
|
-
|
36
|
+
# Can be used to adjust the spaces between the bars.
|
37
|
+
# Accepts values between 0.00 and 1.00 where 0.00 means no spacing at all
|
38
|
+
# and 1 means that each bars' width is nearly 0 (so each bar is a simple
|
39
|
+
# line with no x dimension).
|
40
|
+
#
|
41
|
+
# Default value is +0.9+.
|
42
|
+
def spacing_factor=(space_percent)
|
43
|
+
raise ArgumentError, 'spacing_factor must be between 0.00 and 1.00' unless (space_percent >= 0) && (space_percent <= 1)
|
44
|
+
|
45
|
+
@spacing_factor = (1 - space_percent)
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def initialize_attributes
|
37
51
|
super
|
38
52
|
@spacing_factor = 0.9
|
39
53
|
@group_spacing = 10
|
@@ -41,34 +55,16 @@ class Gruff::Bar < Gruff::Base
|
|
41
55
|
@show_labels_for_bar_values = false
|
42
56
|
@hide_labels = false
|
43
57
|
end
|
44
|
-
private :initialize_ivars
|
45
58
|
|
46
|
-
def
|
59
|
+
def setup_drawing
|
47
60
|
# Labels will be centered over the left of the bar if
|
48
61
|
# there are more labels than columns. This is basically the same
|
49
62
|
# as where it would be for a line graph.
|
50
63
|
@center_labels_over_point = (@labels.keys.length > column_count)
|
51
64
|
|
52
65
|
super
|
53
|
-
return unless data_given?
|
54
|
-
|
55
|
-
draw_bars
|
56
66
|
end
|
57
67
|
|
58
|
-
# Can be used to adjust the spaces between the bars.
|
59
|
-
# Accepts values between 0.00 and 1.00 where 0.00 means no spacing at all
|
60
|
-
# and 1 means that each bars' width is nearly 0 (so each bar is a simple
|
61
|
-
# line with no x dimension).
|
62
|
-
#
|
63
|
-
# Default value is +0.9+.
|
64
|
-
def spacing_factor=(space_percent)
|
65
|
-
raise ArgumentError, 'spacing_factor must be between 0.00 and 1.00' unless (space_percent >= 0) && (space_percent <= 1)
|
66
|
-
|
67
|
-
@spacing_factor = (1 - space_percent)
|
68
|
-
end
|
69
|
-
|
70
|
-
protected
|
71
|
-
|
72
68
|
def hide_labels?
|
73
69
|
@hide_labels
|
74
70
|
end
|
@@ -84,7 +80,7 @@ protected
|
|
84
80
|
# Value to avoid completely overwriting the coordinate axis
|
85
81
|
AXIS_MARGIN = 0.5
|
86
82
|
|
87
|
-
def
|
83
|
+
def draw_graph
|
88
84
|
# Setup spacing.
|
89
85
|
#
|
90
86
|
# Columns sit side-by-side.
|
@@ -112,7 +108,7 @@ protected
|
|
112
108
|
left_y, right_y = conversion.get_top_bottom_scaled(data_point)
|
113
109
|
|
114
110
|
# create new bar
|
115
|
-
rect_renderer = Gruff::Renderer::Rectangle.new(color: data_row.color)
|
111
|
+
rect_renderer = Gruff::Renderer::Rectangle.new(renderer, color: data_row.color)
|
116
112
|
rect_renderer.render(left_x, left_y - AXIS_MARGIN, right_x, right_y - AXIS_MARGIN)
|
117
113
|
|
118
114
|
# Calculate center based on bar_width and current row
|
@@ -123,7 +119,7 @@ protected
|
|
123
119
|
if @show_labels_for_bar_values
|
124
120
|
bar_value_label = Gruff::BarValueLabel::Bar.new([left_x, left_y, right_x, right_y], store.data[row_index].points[point_index])
|
125
121
|
bar_value_label.prepare_rendering(@label_formatting, bar_width) do |x, y, text|
|
126
|
-
draw_value_label(x, y, text
|
122
|
+
draw_value_label(x, y, text)
|
127
123
|
end
|
128
124
|
end
|
129
125
|
end
|
data/lib/gruff/base.rb
CHANGED
@@ -93,15 +93,6 @@ module Gruff
|
|
93
93
|
# Set the large title of the graph displayed at the top.
|
94
94
|
attr_writer :title
|
95
95
|
|
96
|
-
# Same as {#font=} but for the title.
|
97
|
-
attr_writer :title_font
|
98
|
-
|
99
|
-
# Specifies whether to draw the title bolded or not. Default is +true+.
|
100
|
-
attr_writer :bold_title
|
101
|
-
|
102
|
-
# Specifies the text color.
|
103
|
-
attr_writer :font_color
|
104
|
-
|
105
96
|
# Prevent drawing of line markers. Default is +false+.
|
106
97
|
attr_writer :hide_line_markers
|
107
98
|
|
@@ -118,21 +109,9 @@ module Gruff
|
|
118
109
|
# to +"No Data."+.
|
119
110
|
attr_writer :no_data_message
|
120
111
|
|
121
|
-
# Set the font size of the large title at the top of the graph. Default is +36+.
|
122
|
-
attr_writer :title_font_size
|
123
|
-
|
124
|
-
# Optionally set the size of the font. Based on an 800x600px graph.
|
125
|
-
# Default is +20+.
|
126
|
-
#
|
127
|
-
# Will be scaled down if the graph is smaller than 800px wide.
|
128
|
-
attr_writer :legend_font_size
|
129
|
-
|
130
112
|
# Display the legend under the graph. Default is +false+.
|
131
113
|
attr_writer :legend_at_bottom
|
132
114
|
|
133
|
-
# The font size of the labels around the graph. Default is +21+.
|
134
|
-
attr_writer :marker_font_size
|
135
|
-
|
136
115
|
# Set the color of the auxiliary lines.
|
137
116
|
attr_writer :marker_color
|
138
117
|
|
@@ -181,7 +160,7 @@ module Gruff
|
|
181
160
|
@rows.freeze
|
182
161
|
|
183
162
|
initialize_graph_scale
|
184
|
-
|
163
|
+
initialize_attributes
|
185
164
|
initialize_store
|
186
165
|
|
187
166
|
self.theme = Themes::KEYNOTE
|
@@ -209,7 +188,7 @@ module Gruff
|
|
209
188
|
#
|
210
189
|
# This makes it possible to set defaults in a subclass but still allow
|
211
190
|
# developers to change this values in their program.
|
212
|
-
def
|
191
|
+
def initialize_attributes
|
213
192
|
@marker_count = nil
|
214
193
|
@maximum_value = @minimum_value = nil
|
215
194
|
@labels = {}
|
@@ -217,13 +196,9 @@ module Gruff
|
|
217
196
|
@sorted_drawing = false
|
218
197
|
@title = nil
|
219
198
|
|
220
|
-
@title_font =
|
221
|
-
@
|
222
|
-
@
|
223
|
-
|
224
|
-
@marker_font_size = 21.0
|
225
|
-
@legend_font_size = 20.0
|
226
|
-
@title_font_size = 36.0
|
199
|
+
@title_font = Gruff::Font.new(size: 36.0, bold: true)
|
200
|
+
@marker_font = Gruff::Font.new(size: 21.0)
|
201
|
+
@legend_font = Gruff::Font.new(size: 20.0)
|
227
202
|
|
228
203
|
@top_margin = @bottom_margin = @left_margin = @right_margin = DEFAULT_MARGIN
|
229
204
|
@legend_margin = LEGEND_MARGIN
|
@@ -247,7 +222,7 @@ module Gruff
|
|
247
222
|
@x_axis_label_format = nil
|
248
223
|
@y_axis_label_format = nil
|
249
224
|
end
|
250
|
-
protected :
|
225
|
+
protected :initialize_attributes
|
251
226
|
|
252
227
|
# Sets the top, bottom, left and right margins to +margin+.
|
253
228
|
#
|
@@ -262,8 +237,62 @@ module Gruff
|
|
262
237
|
# @param font_path [String] The path to font.
|
263
238
|
#
|
264
239
|
def font=(font_path)
|
265
|
-
@
|
266
|
-
|
240
|
+
@title_font.path = font_path unless @title_font.path
|
241
|
+
@marker_font.path = font_path
|
242
|
+
@legend_font.path = font_path
|
243
|
+
end
|
244
|
+
|
245
|
+
# Same as {#font=} but for the title.
|
246
|
+
#
|
247
|
+
# @param font_path [String] The path to font.
|
248
|
+
#
|
249
|
+
def title_font=(font_path)
|
250
|
+
@title_font.path = font_path
|
251
|
+
end
|
252
|
+
|
253
|
+
# Set the font size of the large title at the top of the graph. Default is +36+.
|
254
|
+
#
|
255
|
+
# @param value [Numeric] title font size
|
256
|
+
#
|
257
|
+
def title_font_size=(value)
|
258
|
+
@title_font.size = value
|
259
|
+
end
|
260
|
+
|
261
|
+
# The font size of the labels around the graph. Default is +21+.
|
262
|
+
#
|
263
|
+
# @param value [Numeric] marker font size
|
264
|
+
#
|
265
|
+
def marker_font_size=(value)
|
266
|
+
@marker_font.size = value
|
267
|
+
end
|
268
|
+
|
269
|
+
# Optionally set the size of the font. Based on an 800x600px graph.
|
270
|
+
# Default is +20+.
|
271
|
+
#
|
272
|
+
# Will be scaled down if the graph is smaller than 800px wide.
|
273
|
+
#
|
274
|
+
# @param value [Numeric] legend font size
|
275
|
+
#
|
276
|
+
def legend_font_size=(value)
|
277
|
+
@legend_font.size = value
|
278
|
+
end
|
279
|
+
|
280
|
+
# Specifies whether to draw the title bolded or not. Default is +true+.
|
281
|
+
#
|
282
|
+
# @param value [Boolean] specifies whether to draw the title bolded or not.
|
283
|
+
#
|
284
|
+
def bold_title=(value)
|
285
|
+
@title_font.bold = value
|
286
|
+
end
|
287
|
+
|
288
|
+
# Specifies the text color.
|
289
|
+
#
|
290
|
+
# @param value [String] color
|
291
|
+
#
|
292
|
+
def font_color=(value)
|
293
|
+
@title_font.color = value
|
294
|
+
@marker_font.color = value
|
295
|
+
@legend_font.color = value
|
267
296
|
end
|
268
297
|
|
269
298
|
# Add a color to the list of available colors for lines.
|
@@ -332,12 +361,13 @@ module Gruff
|
|
332
361
|
}
|
333
362
|
@theme_options = defaults.merge options
|
334
363
|
|
364
|
+
self.marker_color = @theme_options[:marker_color]
|
365
|
+
self.font_color = @theme_options[:font_color] || @marker_color
|
366
|
+
|
335
367
|
@colors = @theme_options[:colors]
|
336
|
-
@marker_color = @theme_options[:marker_color]
|
337
368
|
@marker_shadow_color = @theme_options[:marker_shadow_color]
|
338
|
-
@font_color = @theme_options[:font_color] || @marker_color
|
339
369
|
|
340
|
-
Gruff::Renderer.
|
370
|
+
@renderer = Gruff::Renderer.new(@columns, @rows, @scale, @theme_options)
|
341
371
|
end
|
342
372
|
|
343
373
|
# Apply Apple's keynote theme.
|
@@ -439,8 +469,8 @@ module Gruff
|
|
439
469
|
def to_image
|
440
470
|
@to_image ||= begin
|
441
471
|
draw
|
442
|
-
|
443
|
-
|
472
|
+
renderer.finish
|
473
|
+
renderer.image
|
444
474
|
end
|
445
475
|
end
|
446
476
|
|
@@ -456,27 +486,29 @@ module Gruff
|
|
456
486
|
end
|
457
487
|
end
|
458
488
|
|
459
|
-
|
460
|
-
|
461
|
-
# Overridden by subclasses to do the actual plotting of the graph.
|
462
|
-
#
|
463
|
-
# Subclasses should start by calling super() for this method.
|
489
|
+
# Draw a graph.
|
464
490
|
def draw
|
491
|
+
setup_data
|
492
|
+
|
465
493
|
# Maybe should be done in one of the following functions for more granularity.
|
466
494
|
unless data_given?
|
467
495
|
draw_no_data
|
468
496
|
return
|
469
497
|
end
|
470
498
|
|
471
|
-
setup_data
|
472
499
|
setup_drawing
|
473
500
|
|
474
501
|
draw_legend
|
475
502
|
draw_line_markers
|
476
503
|
draw_axis_labels
|
477
504
|
draw_title
|
505
|
+
draw_graph
|
478
506
|
end
|
479
507
|
|
508
|
+
protected
|
509
|
+
|
510
|
+
attr_reader :renderer
|
511
|
+
|
480
512
|
# Perform data manipulation before calculating chart measurements
|
481
513
|
def setup_data # :nodoc:
|
482
514
|
if @y_axis_increment && !@hide_line_markers
|
@@ -581,13 +613,13 @@ module Gruff
|
|
581
613
|
x_axis_label_y_coordinate = @graph_bottom + LABEL_MARGIN + @marker_caps_height
|
582
614
|
|
583
615
|
# TODO: Center between graph area
|
584
|
-
text_renderer = Gruff::Renderer::Text.new(@x_axis_label, font: @
|
616
|
+
text_renderer = Gruff::Renderer::Text.new(renderer, @x_axis_label, font: @marker_font)
|
585
617
|
text_renderer.add_to_render_queue(@raw_columns, 1.0, 0.0, x_axis_label_y_coordinate)
|
586
618
|
end
|
587
619
|
|
588
620
|
if @y_axis_label
|
589
621
|
# Y Axis, rotated vertically
|
590
|
-
text_renderer = Gruff::Renderer::Text.new(@y_axis_label, font: @
|
622
|
+
text_renderer = Gruff::Renderer::Text.new(renderer, @y_axis_label, font: @marker_font, rotation: -90)
|
591
623
|
text_renderer.add_to_render_queue(1.0, @raw_rows, @left_margin + @marker_caps_height / 2.0, 0.0, Magick::CenterGravity)
|
592
624
|
end
|
593
625
|
end
|
@@ -602,13 +634,13 @@ module Gruff
|
|
602
634
|
(0..marker_count).each do |index|
|
603
635
|
y = @graph_top + @graph_height - index.to_f * increment_scaled
|
604
636
|
|
605
|
-
line_renderer = Gruff::Renderer::Line.new(color: @marker_color, shadow_color: @marker_shadow_color)
|
637
|
+
line_renderer = Gruff::Renderer::Line.new(renderer, color: @marker_color, shadow_color: @marker_shadow_color)
|
606
638
|
line_renderer.render(@graph_left, y, @graph_right, y)
|
607
639
|
|
608
640
|
unless @hide_line_numbers
|
609
641
|
marker_label = BigDecimal(index.to_s) * BigDecimal(@increment.to_s) + BigDecimal(minimum_value.to_s)
|
610
642
|
label = y_axis_label(marker_label, @increment)
|
611
|
-
text_renderer = Gruff::Renderer::Text.new(label, font: @
|
643
|
+
text_renderer = Gruff::Renderer::Text.new(renderer, label, font: @marker_font)
|
612
644
|
text_renderer.add_to_render_queue(@graph_left - LABEL_MARGIN, 1.0, 0.0, y, Magick::EastGravity)
|
613
645
|
end
|
614
646
|
end
|
@@ -641,17 +673,17 @@ module Gruff
|
|
641
673
|
next if legend_label.empty?
|
642
674
|
|
643
675
|
# Draw label
|
644
|
-
text_renderer = Gruff::Renderer::Text.new(legend_label, font: @
|
676
|
+
text_renderer = Gruff::Renderer::Text.new(renderer, legend_label, font: @legend_font)
|
645
677
|
text_renderer.add_to_render_queue(@raw_columns, 1.0, current_x_offset + (legend_square_width * 1.7), current_y_offset, Magick::WestGravity)
|
646
678
|
|
647
679
|
# Now draw box with color of this dataset
|
648
|
-
rect_renderer = Gruff::Renderer::Rectangle.new(color: store.data[index].color)
|
680
|
+
rect_renderer = Gruff::Renderer::Rectangle.new(renderer, color: store.data[index].color)
|
649
681
|
rect_renderer.render(current_x_offset,
|
650
682
|
current_y_offset - legend_square_width / 2.0,
|
651
683
|
current_x_offset + legend_square_width,
|
652
684
|
current_y_offset + legend_square_width / 2.0)
|
653
685
|
|
654
|
-
width = calculate_width(@
|
686
|
+
width = calculate_width(@legend_font, legend_label)
|
655
687
|
current_x_offset += width + (legend_square_width * 2.7)
|
656
688
|
label_widths.first.shift
|
657
689
|
|
@@ -672,15 +704,12 @@ module Gruff
|
|
672
704
|
def draw_title
|
673
705
|
return if hide_title?
|
674
706
|
|
675
|
-
|
676
|
-
font_weight = @bold_title ? Magick::BoldWeight : Magick::NormalWeight
|
677
|
-
font_size = @title_font_size
|
678
|
-
|
679
|
-
metrics = Renderer::Text.metrics(@title, font, font_size, font_weight)
|
707
|
+
metrics = Gruff::Renderer::Text.new(renderer, @title, font: @title_font).metrics
|
680
708
|
if metrics.width > @raw_columns
|
681
|
-
|
709
|
+
@title_font.size = @title_font.size * (@raw_columns / metrics.width) * 0.95
|
682
710
|
end
|
683
|
-
|
711
|
+
|
712
|
+
text_renderer = Gruff::Renderer::Text.new(renderer, @title, font: @title_font)
|
684
713
|
text_renderer.add_to_render_queue(@raw_columns, 1.0, 0, @top_margin)
|
685
714
|
end
|
686
715
|
|
@@ -714,24 +743,31 @@ module Gruff
|
|
714
743
|
|
715
744
|
def draw_label_at(width, height, x, y, text, gravity = Magick::NorthGravity)
|
716
745
|
label_text = truncate_label_text(text)
|
717
|
-
text_renderer = Gruff::Renderer::Text.new(label_text, font: @
|
746
|
+
text_renderer = Gruff::Renderer::Text.new(renderer, label_text, font: @marker_font)
|
718
747
|
text_renderer.add_to_render_queue(width, height, x, y, gravity)
|
719
748
|
end
|
720
749
|
|
721
750
|
# Draws the data value over the data point in bar graphs
|
722
|
-
def draw_value_label(x_offset, y_offset, data_point
|
723
|
-
return if @hide_line_markers
|
751
|
+
def draw_value_label(x_offset, y_offset, data_point)
|
752
|
+
return if @hide_line_markers
|
724
753
|
|
725
|
-
text_renderer = Gruff::Renderer::Text.new(data_point, font: @
|
754
|
+
text_renderer = Gruff::Renderer::Text.new(renderer, data_point, font: @marker_font)
|
726
755
|
text_renderer.add_to_render_queue(1.0, 1.0, x_offset, y_offset)
|
727
756
|
end
|
728
757
|
|
729
758
|
# Shows an error message because you have no data.
|
730
759
|
def draw_no_data
|
731
|
-
|
760
|
+
font = @title_font.dup
|
761
|
+
font.size = 80
|
762
|
+
font.bold = false
|
763
|
+
text_renderer = Gruff::Renderer::Text.new(renderer, @no_data_message, font: font)
|
732
764
|
text_renderer.render(@raw_columns, @raw_rows, 0, 0, Magick::CenterGravity)
|
733
765
|
end
|
734
766
|
|
767
|
+
def draw_graph
|
768
|
+
raise 'Should implement this method at inherited class.'
|
769
|
+
end
|
770
|
+
|
735
771
|
# Resets everything to defaults (except data).
|
736
772
|
def reset_themes
|
737
773
|
@theme_options = {}
|
@@ -794,15 +830,15 @@ module Gruff
|
|
794
830
|
private
|
795
831
|
|
796
832
|
def setup_marker_caps_height
|
797
|
-
hide_bottom_label_area? ? 0 : calculate_caps_height(@
|
833
|
+
hide_bottom_label_area? ? 0 : calculate_caps_height(@marker_font)
|
798
834
|
end
|
799
835
|
|
800
836
|
def setup_title_caps_height
|
801
|
-
hide_title? ? 0 : calculate_caps_height(@
|
837
|
+
hide_title? ? 0 : calculate_caps_height(@title_font) * @title.lines.to_a.size
|
802
838
|
end
|
803
839
|
|
804
840
|
def setup_legend_caps_height
|
805
|
-
@hide_legend ? 0 : calculate_caps_height(@
|
841
|
+
@hide_legend ? 0 : calculate_caps_height(@legend_font)
|
806
842
|
end
|
807
843
|
|
808
844
|
def graph_right_margin
|
@@ -813,7 +849,7 @@ module Gruff
|
|
813
849
|
# Make space for half the width of the rightmost column label.
|
814
850
|
# Might be greater than the number of columns if between-style bar markers are used.
|
815
851
|
last_label = @labels.keys.max.to_i
|
816
|
-
(last_label >= (column_count - 1) && @center_labels_over_point) ? calculate_width(@
|
852
|
+
(last_label >= (column_count - 1) && @center_labels_over_point) ? calculate_width(@marker_font, @labels[last_label]) / 2.0 : 0
|
817
853
|
end
|
818
854
|
|
819
855
|
def setup_left_margin
|
@@ -826,7 +862,7 @@ module Gruff
|
|
826
862
|
y_axis_label(maximum_value.to_f, @increment)
|
827
863
|
end
|
828
864
|
end
|
829
|
-
longest_left_label_width = calculate_width(@
|
865
|
+
longest_left_label_width = calculate_width(@marker_font, truncate_label_text(text))
|
830
866
|
longest_left_label_width *= 1.25 if @has_left_labels
|
831
867
|
|
832
868
|
# Shift graph if left line numbers are hidden
|
@@ -868,29 +904,31 @@ module Gruff
|
|
868
904
|
# Return a formatted string representing a number value that should be
|
869
905
|
# printed as a label.
|
870
906
|
def label(value, increment)
|
871
|
-
label =
|
872
|
-
|
873
|
-
|
874
|
-
|
875
|
-
|
876
|
-
|
877
|
-
|
878
|
-
|
879
|
-
|
880
|
-
|
881
|
-
|
882
|
-
|
883
|
-
|
884
|
-
|
885
|
-
|
886
|
-
|
887
|
-
|
888
|
-
|
889
|
-
|
890
|
-
|
891
|
-
|
892
|
-
|
893
|
-
|
907
|
+
label = begin
|
908
|
+
if increment
|
909
|
+
if increment >= 10 || (increment * 1) == (increment * 1).to_i.to_f
|
910
|
+
sprintf('%0i', value)
|
911
|
+
elsif increment >= 1.0 || (increment * 10) == (increment * 10).to_i.to_f
|
912
|
+
sprintf('%0.1f', value)
|
913
|
+
elsif increment >= 0.1 || (increment * 100) == (increment * 100).to_i.to_f
|
914
|
+
sprintf('%0.2f', value)
|
915
|
+
elsif increment >= 0.01 || (increment * 1000) == (increment * 1000).to_i.to_f
|
916
|
+
sprintf('%0.3f', value)
|
917
|
+
elsif increment >= 0.001 || (increment * 10000) == (increment * 10000).to_i.to_f
|
918
|
+
sprintf('%0.4f', value)
|
919
|
+
else
|
920
|
+
value.to_s
|
921
|
+
end
|
922
|
+
elsif (@spread.to_f % (marker_count.to_f == 0 ? 1 : marker_count.to_f) == 0) || !@y_axis_increment.nil?
|
923
|
+
value.to_i.to_s
|
924
|
+
elsif @spread > 10.0
|
925
|
+
sprintf('%0i', value)
|
926
|
+
elsif @spread >= 3.0
|
927
|
+
sprintf('%0.2f', value)
|
928
|
+
else
|
929
|
+
value.to_s
|
930
|
+
end
|
931
|
+
end
|
894
932
|
|
895
933
|
parts = label.split('.')
|
896
934
|
parts[0] = parts[0].commify
|
@@ -917,7 +955,7 @@ module Gruff
|
|
917
955
|
# May fix legend drawing problem at small sizes
|
918
956
|
label_widths = [[]] # Used to calculate line wrap
|
919
957
|
legend_labels.each do |label|
|
920
|
-
width = calculate_width(@
|
958
|
+
width = calculate_width(@legend_font, label)
|
921
959
|
label_width = width + legend_square_width * 2.7
|
922
960
|
label_widths.last.push label_width
|
923
961
|
|
@@ -959,8 +997,8 @@ module Gruff
|
|
959
997
|
#
|
960
998
|
# Not scaled since it deals with dimensions that the regular scaling will
|
961
999
|
# handle.
|
962
|
-
def calculate_caps_height(
|
963
|
-
metrics = Renderer::Text.
|
1000
|
+
def calculate_caps_height(font)
|
1001
|
+
metrics = Gruff::Renderer::Text.new(renderer, 'X', font: font).metrics
|
964
1002
|
metrics.height
|
965
1003
|
end
|
966
1004
|
|
@@ -968,11 +1006,11 @@ module Gruff
|
|
968
1006
|
#
|
969
1007
|
# Not scaled since it deals with dimensions that the regular
|
970
1008
|
# scaling will handle.
|
971
|
-
def calculate_width(
|
1009
|
+
def calculate_width(font, text)
|
972
1010
|
text = text.to_s
|
973
1011
|
return 0 if text.empty?
|
974
1012
|
|
975
|
-
metrics = Renderer::Text.
|
1013
|
+
metrics = Gruff::Renderer::Text.new(renderer, text, font: font).metrics
|
976
1014
|
metrics.width
|
977
1015
|
end
|
978
1016
|
|