gruff 0.16.0-java → 0.17.0-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -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,7 +1,7 @@
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: 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: 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
  requirement: !ruby/object:Gem::Requirement
@@ -86,7 +86,7 @@ dependencies:
86
86
  requirements:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: 0.9.25
89
+ version: 0.9.28
90
90
  name: yard
91
91
  prerelease: false
92
92
  type: :development
@@ -94,7 +94,7 @@ dependencies:
94
94
  requirements:
95
95
  - - "~>"
96
96
  - !ruby/object:Gem::Version
97
- version: 0.9.25
97
+ version: 0.9.28
98
98
  description: Beautiful graphs for one or multiple datasets. Can be used on websites
99
99
  or in documents.
100
100
  email: boss@topfunky.com
@@ -129,6 +129,7 @@ files:
129
129
  - lib/gruff/dot.rb
130
130
  - lib/gruff/font.rb
131
131
  - lib/gruff/helper/bar_conversion.rb
132
+ - lib/gruff/helper/bar_mixin.rb
132
133
  - lib/gruff/helper/bar_value_label.rb
133
134
  - lib/gruff/helper/stacked_mixin.rb
134
135
  - lib/gruff/histogram.rb