Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support hide_labels separate from hide_line_markers for bar graphs only #446

Merged
merged 2 commits into from
Sep 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/gruff/bar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,16 @@ class Gruff::Bar < Gruff::Base
# Default is +false+.
attr_writer :show_labels_for_bar_values

# Prevent drawing of column labels below a bar graph. Default is +false+.
attr_writer :hide_labels

def initialize_ivars
super
@spacing_factor = 0.9
@group_spacing = 10
@label_formatting = nil
@show_labels_for_bar_values = false
@hide_labels = false
end
private :initialize_ivars

Expand Down Expand Up @@ -68,6 +72,10 @@ def spacing_factor=(space_percent)

protected

def hide_labels?
@hide_labels
end

def draw_bars
# Setup spacing.
#
Expand Down
12 changes: 8 additions & 4 deletions lib/gruff/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def initialize_store
end
protected :initialize_store

# Initialize instance variable of attribures
# Initialize instance variable of attributes
#
# Subclasses can override this, call super, then set values separately.
#
Expand Down Expand Up @@ -500,6 +500,10 @@ def hide_title?
@hide_title || @title.nil? || @title.empty?
end

def hide_labels?
@hide_line_markers
end

##
# Calculates size of drawable area, general font dimensions, etc.

Expand Down Expand Up @@ -661,7 +665,7 @@ def draw_label(x_offset, index, gravity = Magick::NorthGravity)
end

def draw_unique_label(index)
return if @hide_line_markers
return if hide_labels?

@labels_seen ||= {}
if !@labels[index].nil? && @labels_seen[index].nil?
Expand Down Expand Up @@ -746,7 +750,7 @@ def sort_norm_data
private

def setup_marker_caps_height
@hide_line_markers ? 0 : calculate_caps_height(@marker_font_size)
hide_labels? ? 0 : calculate_caps_height(@marker_font_size)
end

def setup_title_caps_height
Expand Down Expand Up @@ -795,7 +799,7 @@ def setup_top_margin
end

def setup_bottom_margin
graph_bottom_margin = @hide_line_markers ? @bottom_margin : @bottom_margin + @marker_caps_height + LABEL_MARGIN
graph_bottom_margin = hide_labels? ? @bottom_margin : @bottom_margin + @marker_caps_height + LABEL_MARGIN

x_axis_label_height = @x_axis_label.nil? ? 0.0 : @marker_caps_height + LABEL_MARGIN
# FIXME: Consider chart types other than bar
Expand Down
Binary file added test/expected/bar_no_labels.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/expected/bar_no_line_markers.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/expected/bar_no_line_markers_or_labels.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/expected_java/bar_no_labels.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/expected_java/bar_no_line_markers.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions test/test_bar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,23 @@ def test_bar_graph_small
# g.write "test/output/bar_nil_font.png"
# end

def test_no_labels
g = setup_basic_graph(400)
g.title = 'No Labels'
g.hide_labels = true
g.write('test/output/bar_no_labels.png')
assert_same_image('test/expected/bar_no_labels.png', 'test/output/bar_no_labels.png')
end

def test_no_line_markers_or_labels
g = setup_basic_graph(400)
g.title = 'No Line Markers or Labels'
g.hide_labels = true
g.hide_line_markers = true
g.write('test/output/bar_no_line_markers_or_labels.png')
assert_same_image('test/expected/bar_no_line_markers_or_labels.png', 'test/output/bar_no_line_markers_or_labels.png')
end

def test_no_line_markers
g = setup_basic_graph(400)
g.title = 'No Line Markers'
Expand Down