Skip to content

Commit

Permalink
chore: Remove unnecessary method calling
Browse files Browse the repository at this point in the history
  • Loading branch information
Watson1978 committed May 1, 2022
1 parent e279e15 commit 0bb9e16
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 31 deletions.
2 changes: 1 addition & 1 deletion lib/gruff/area.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def initialize_attributes
end

def draw_graph
x_increment = @graph_width / (column_count - 1).to_f
x_increment = @graph_width / (column_count - 1)

store.norm_data.each do |data_row|
poly_points = []
Expand Down
2 changes: 1 addition & 1 deletion lib/gruff/bar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def draw_graph
# Columns sit side-by-side.
@bar_spacing ||= @spacing_factor # space between the bars

bar_width = (@graph_width - calculate_spacing) / (column_count * store.length).to_f
bar_width = (@graph_width - calculate_spacing) / (column_count * store.length)
padding = (bar_width * (1 - @bar_spacing)) / 2

# Setup the BarConversion Object
Expand Down
20 changes: 10 additions & 10 deletions lib/gruff/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ def data(name, data_points = [], color = nil)
#
# Set it after you have given all your data to the graph object.
def minimum_value
@minimum_value || store.min
(@minimum_value || store.min).to_f
end
attr_writer :minimum_value

Expand All @@ -432,7 +432,7 @@ def minimum_value
# If you use this, you must set it after you have given all your data to
# the graph object.
def maximum_value
@maximum_value || store.max
(@maximum_value || store.max).to_f
end
attr_writer :maximum_value

Expand Down Expand Up @@ -505,8 +505,8 @@ def draw
# Perform data manipulation before calculating chart measurements
def setup_data # :nodoc:
if @y_axis_increment && !@hide_line_markers
self.maximum_value = [@y_axis_increment, maximum_value, (maximum_value.to_f / @y_axis_increment).round * @y_axis_increment].max
self.minimum_value = [minimum_value, (minimum_value.to_f / @y_axis_increment).round * @y_axis_increment].min
self.maximum_value = [@y_axis_increment, maximum_value, (maximum_value / @y_axis_increment).round * @y_axis_increment].max
self.minimum_value = [minimum_value, (minimum_value / @y_axis_increment).round * @y_axis_increment].min
end
end

Expand Down Expand Up @@ -545,7 +545,7 @@ def marker_count
@marker_count ||= begin
count = nil
(3..7).each do |lines|
if @spread.to_f % lines == 0.0
if @spread % lines == 0.0
count = lines and break
end
end
Expand All @@ -559,7 +559,7 @@ def normalize
end

def calculate_spread
@spread = maximum_value.to_f - minimum_value.to_f
@spread = maximum_value - minimum_value
@spread = @spread > 0 ? @spread : 1
end

Expand Down Expand Up @@ -616,11 +616,11 @@ def draw_axis_labels
def draw_line_markers
return if @hide_line_markers

increment_scaled = @graph_height.to_f / (@spread / @increment)
increment_scaled = @graph_height / (@spread / @increment)

# Draw horizontal line markers and annotate with numbers
(0..marker_count).each do |index|
y = @graph_top + @graph_height - (index.to_f * increment_scaled)
y = @graph_top + @graph_height - (index * increment_scaled)

line_renderer = Gruff::Renderer::Line.new(renderer, color: @marker_color, shadow_color: @marker_shadow_color)
line_renderer.render(@graph_left, y, @graph_right, y)
Expand Down Expand Up @@ -849,7 +849,7 @@ def setup_left_margin
if @has_left_labels
@labels.values.reduce('') { |value, memo| value.to_s.length > memo.to_s.length ? value : memo }
else
y_axis_label(maximum_value.to_f, @increment)
y_axis_label(maximum_value, @increment)
end
end
longest_left_label_width = calculate_width(@marker_font, truncate_label_text(text))
Expand Down Expand Up @@ -907,7 +907,7 @@ def label(value, increment)
else
value.to_s
end
elsif (@spread.to_f % (marker_count.to_f == 0 ? 1 : marker_count.to_f) == 0) || !@y_axis_increment.nil?
elsif (@spread % (marker_count == 0 ? 1 : marker_count) == 0) || !@y_axis_increment.nil?
value.to_i.to_s
elsif @spread > 10.0
sprintf('%0i', value)
Expand Down
2 changes: 1 addition & 1 deletion lib/gruff/bezier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Gruff::Bezier < Gruff::Base
private

def draw_graph
x_increment = @graph_width / (column_count - 1).to_f
x_increment = @graph_width / (column_count - 1)

store.norm_data.each do |data_row|
poly_points = []
Expand Down
2 changes: 1 addition & 1 deletion lib/gruff/dot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def draw_graph
#
spacing_factor = 1.0

items_width = @graph_height / column_count.to_f
items_width = @graph_height / column_count
item_width = items_width * spacing_factor / store.length
padding = (items_width * (1 - spacing_factor)) / 2

Expand Down
12 changes: 6 additions & 6 deletions lib/gruff/line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,12 @@ def draw_graph

def setup_data
# Update the global min/max values for the x data
@maximum_x_value ||= store.max_x
@minimum_x_value ||= store.min_x
@maximum_x_value = (@maximum_x_value || store.max_x).to_f
@minimum_x_value = (@minimum_x_value || store.min_x).to_f

# Deal with horizontal reference line values that exceed the existing minimum & maximum values.
possible_maximums = [maximum_value.to_f]
possible_minimums = [minimum_value.to_f]
possible_maximums = [maximum_value]
possible_minimums = [minimum_value]

@reference_lines.each_value do |curr_reference_line|
if curr_reference_line.key?(:value)
Expand All @@ -255,14 +255,14 @@ def setup_drawing
def normalize
return unless data_given?

spread_x = @maximum_x_value.to_f - @minimum_x_value.to_f
spread_x = @maximum_x_value - @minimum_x_value
store.normalize(minimum_x: @minimum_x_value, spread_x: spread_x, minimum_y: minimum_value, spread_y: @spread)

@reference_lines.each_value do |curr_reference_line|
# We only care about horizontal markers ... for normalization.
# Vertical markers won't have a :value, they will have an :index

curr_reference_line[:norm_value] = ((curr_reference_line[:value].to_f - minimum_value) / @spread.to_f) if curr_reference_line.key?(:value)
curr_reference_line[:norm_value] = ((curr_reference_line[:value].to_f - minimum_value) / @spread) if curr_reference_line.key?(:value)
end
end

Expand Down
12 changes: 6 additions & 6 deletions lib/gruff/scatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def initialize_attributes

def draw_graph
# Check to see if more than one datapoint was given. NaN can result otherwise.
@x_increment = @x_spread > 1 ? (@graph_width / (@x_spread - 1).to_f) : @graph_width
@x_increment = @x_spread > 1 ? @graph_width / (@x_spread - 1) : @graph_width

store.norm_data.each do |data_row|
data_row.coordinates.each do |x_value, y_value|
Expand All @@ -135,8 +135,8 @@ def draw_graph

def setup_data
# Update the global min/max values for the x data
@maximum_x_value ||= store.max_x
@minimum_x_value ||= store.min_x
@maximum_x_value = (@maximum_x_value || store.max_x).to_f
@minimum_x_value = (@minimum_x_value || store.min_x).to_f

super
end
Expand Down Expand Up @@ -190,13 +190,13 @@ def draw_line_markers
@marker_x_count = (@x_spread / @x_axis_increment).to_i
@x_increment = @x_axis_increment
end
increment_x_scaled = @graph_width.to_f / (@x_spread / @x_increment)
increment_x_scaled = @graph_width / (@x_spread / @x_increment)

# Draw vertical line markers and annotate with numbers
(0..@marker_x_count).each do |index|
# TODO: Fix the vertical lines, and enable them by default. Not pretty when they don't match up with top y-axis line
if @show_vertical_markers
x = @graph_left + @graph_width - (index.to_f * increment_x_scaled)
x = @graph_left + @graph_width - (index * increment_x_scaled)

line_renderer = Gruff::Renderer::Line.new(renderer, color: @marker_color, shadow_color: @marker_shadow_color)
line_renderer.render(x, @graph_top, x, @graph_bottom)
Expand All @@ -205,7 +205,7 @@ def draw_line_markers
unless @hide_line_numbers
marker_label = (BigDecimal(index.to_s) * BigDecimal(@x_increment.to_s)) + BigDecimal(@minimum_x_value.to_s)
y_offset = @graph_bottom + (@x_label_margin || LABEL_MARGIN)
x_offset = get_x_coord(index.to_f, increment_x_scaled, @graph_left)
x_offset = get_x_coord(index, increment_x_scaled, @graph_left)

label = x_axis_label(marker_label, @x_increment)
rotation = -90.0 if @use_vertical_x_labels
Expand Down
4 changes: 2 additions & 2 deletions lib/gruff/side_bar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def setup_graph_measurements
def draw_graph
# Setup spacing.
#
bars_width = (@graph_height - calculate_spacing) / column_count.to_f
bars_width = (@graph_height - calculate_spacing) / column_count
bar_width = bars_width / store.length
padding = (bar_width * (1 - @bar_spacing)) / 2

Expand Down Expand Up @@ -147,7 +147,7 @@ def draw_line_markers
number_of_lines = 1 if number_of_lines == 0

# TODO: Round maximum marker value to a round number like 100, 0.1, 0.5, etc.
increment = significant(@spread.to_f / number_of_lines)
increment = significant(@spread / number_of_lines)
(0..number_of_lines).each do |index|
line_diff = (@graph_right - @graph_left) / number_of_lines
x = @graph_right - (line_diff * index) - 1
Expand Down
2 changes: 1 addition & 1 deletion lib/gruff/side_stacked_bar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def draw_graph
# Setup spacing.
#
# Columns sit stacked.
bar_width = @graph_height / column_count.to_f
bar_width = @graph_height / column_count
height = Array.new(column_count, 0)
length = Array.new(column_count, @graph_left)
padding = (bar_width * (1 - @bar_spacing)) / 2
Expand Down
2 changes: 1 addition & 1 deletion lib/gruff/stacked_area.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def setup_data
end

def draw_graph
x_increment = @graph_width / (column_count - 1).to_f
x_increment = @graph_width / (column_count - 1)

height = Array.new(column_count, 0)

Expand Down
2 changes: 1 addition & 1 deletion lib/gruff/stacked_bar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def draw_graph
# Setup spacing.
#
# Columns sit stacked.
bar_width = @graph_width / column_count.to_f
bar_width = @graph_width / column_count
padding = (bar_width * (1 - @bar_spacing)) / 2

height = Array.new(column_count, 0)
Expand Down

0 comments on commit 0bb9e16

Please sign in to comment.