Skip to content

Commit

Permalink
add hide_labels to stacked side bar graphs
Browse files Browse the repository at this point in the history
  • Loading branch information
elrayle committed Sep 13, 2020
1 parent f743cc0 commit 6977449
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 46 deletions.
8 changes: 8 additions & 0 deletions lib/gruff/bar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ def hide_labels?
@hide_labels
end

def hide_left_label_area?
@hide_line_markers
end

def hide_bottom_label_area?
hide_labels?
end

def draw_bars
# Setup spacing.
#
Expand Down
14 changes: 11 additions & 3 deletions lib/gruff/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,14 @@ def hide_labels?
@hide_line_markers
end

def hide_left_label_area?
@hide_line_markers
end

def hide_bottom_label_area?
@hide_line_markers
end

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

Expand Down Expand Up @@ -750,7 +758,7 @@ def sort_norm_data
private

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

def setup_title_caps_height
Expand All @@ -773,7 +781,7 @@ def extra_room_for_long_label
end

def setup_left_margin
return @left_margin if @hide_line_markers
return @left_margin if hide_left_label_area?

if @has_left_labels
longest_left_label_width = calculate_width(@marker_font_size,
Expand All @@ -799,7 +807,7 @@ def setup_top_margin
end

def setup_bottom_margin
graph_bottom_margin = hide_labels? ? @bottom_margin : @bottom_margin + @marker_caps_height + LABEL_MARGIN
graph_bottom_margin = hide_bottom_label_area? ? @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
18 changes: 18 additions & 0 deletions lib/gruff/side_stacked_bar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,16 @@ class Gruff::SideStackedBar < Gruff::SideBar
# Default is +false+.
attr_writer :show_labels_for_bar_values

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

def initialize_ivars
super
@bar_spacing = 0.9
@segment_spacing = 2.0
@label_formatting = nil
@show_labels_for_bar_values = false
@hide_labels = false
end
private :initialize_ivars

Expand All @@ -55,6 +59,20 @@ def draw
super
end

protected

def hide_labels?
@hide_labels
end

def hide_left_label_area?
hide_labels?
end

def hide_bottom_label_area?
@hide_line_markers
end

private

def draw_bars
Expand Down
Binary file added test/expected/side_stacked_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.
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.
Binary file added test/expected_java/side_stacked_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.
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.
92 changes: 49 additions & 43 deletions test/test_sidestacked_bar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,85 +12,91 @@ def setup
#[:Philip, [90, 34, 23, 12, 78, 89, 98, 88]],
#["Arthur", [5, 10, 13, 11, 6, 16, 22, 32]],
]
@sample_labels = {
@labels = {
0 => '5/6',
1 => '5/15',
2 => '5/24'
2 => '5/24',
3 => '5/30'
}
@long_labels = {
0 => 'September',
1 => 'Oct',
2 => 'Nov',
3 => 'Dec'
}
end

def test_bar_graph
g = Gruff::SideStackedBar.new
g = setup_basic_graph
g.title = 'Visual Stacked Bar Graph Test'
g.labels = {
0 => '5/6',
1 => '5/15',
2 => '5/24',
3 => '5/30'
}
@datasets.each do |data|
g.data(data[0], data[1])
end
g.write('test/output/side_stacked_bar_keynote.png')
assert_same_image('test/expected/side_stacked_bar_keynote.png', 'test/output/side_stacked_bar_keynote.png')
end

def test_bar_graph_small
g = Gruff::SideStackedBar.new(400)
g = setup_basic_graph(400)
g.title = 'Visual Stacked Bar Graph Test'
g.labels = {
0 => '5/6',
1 => '5/15',
2 => '5/24',
3 => '5/30'
}
@datasets.each do |data|
g.data(data[0], data[1])
end
g.write('test/output/side_stacked_bar_keynote_small.png')
assert_same_image('test/expected/side_stacked_bar_keynote_small.png', 'test/output/side_stacked_bar_keynote_small.png')
end

def test_wide
@labels = {
0 => '5/6',
1 => '5/15',
2 => '5/24'
}
g = setup_basic_graph('800x400')
g.title = 'Wide SSBar'
g.write('test/output/side_stacked_bar_wide.png')
assert_same_image('test/expected/side_stacked_bar_wide.png', 'test/output/side_stacked_bar_wide.png')
end

def test_should_space_long_left_labels_appropriately
g = Gruff::SideStackedBar.new
@labels = @long_labels
g = setup_basic_graph
g.title = 'Stacked Bar Long Label'
g.labels = {
0 => 'September',
1 => 'Oct',
2 => 'Nov',
3 => 'Dec'
}
@datasets.each do |data|
g.data(data[0], data[1])
end
g.write('test/output/side_stacked_bar_long_label.png')
assert_same_image('test/expected/side_stacked_bar_long_label.png', 'test/output/side_stacked_bar_long_label.png')
end

def test_bar_labels
g = Gruff::SideStackedBar.new
@labels = @long_labels
g = setup_basic_graph
g.title = 'Stacked Bar Long Label'
g.labels = {
0 => 'September',
1 => 'Oct',
2 => 'Nov',
3 => 'Dec'
}
@datasets.each do |data|
g.data(data[0], data[1])
end
g.show_labels_for_bar_values = true
g.write('test/output/side_stacked_bar_labels.png')
assert_same_image('test/expected/side_stacked_bar_labels.png', 'test/output/side_stacked_bar_labels.png')
end

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

def test_no_line_markers_or_labels
@labels = @long_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/side_stacked_bar_no_line_markers_or_labels.png')
assert_same_image('test/expected/side_stacked_bar_no_line_markers_or_labels.png', 'test/output/side_stacked_bar_no_line_markers_or_labels.png')
end

def test_no_line_markers
@labels = @long_labels
g = setup_basic_graph(400)
g.title = 'No Line Markers'
g.hide_line_markers = true
g.write('test/output/side_stacked_bar_no_line_markers.png')
assert_same_image('test/expected/side_stacked_bar_no_line_markers.png', 'test/output/side_stacked_bar_no_line_markers.png')
end

def test_draw_twice
g = setup_basic_graph
g.show_labels_for_bar_values = true
Expand All @@ -105,7 +111,7 @@ def test_draw_twice
def setup_basic_graph(size = 800)
g = Gruff::SideStackedBar.new(size)
g.title = 'My Graph Title'
g.labels = @sample_labels
g.labels = @labels
@datasets.each do |data|
g.data(data[0], data[1])
end
Expand Down

0 comments on commit 6977449

Please sign in to comment.