Skip to content

Commit

Permalink
add hide_labels to stacked side bar graphs (#452)
Browse files Browse the repository at this point in the history
  • Loading branch information
elrayle authored Sep 14, 2020
1 parent 94bda67 commit 85b50dc
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 43 deletions.
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 85b50dc

Please sign in to comment.