Skip to content

Commit

Permalink
Provide attr_accessors for title_margin and legend_margin.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoglan committed Aug 15, 2009
1 parent 04cb419 commit deedb4f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
28 changes: 19 additions & 9 deletions lib/gruff/base.rb
Expand Up @@ -36,7 +36,9 @@ class Base
DATA_COLOR_INDEX = 2 DATA_COLOR_INDEX = 2


# Space around text elements. Mostly used for vertical spacing # Space around text elements. Mostly used for vertical spacing
LEGEND_MARGIN = TITLE_MARGIN = LABEL_MARGIN = 10.0 LEGEND_MARGIN = TITLE_MARGIN = 20.0
LABEL_MARGIN = 10.0
DEFAULT_MARGIN = 20.0


DEFAULT_TARGET_WIDTH = 800 DEFAULT_TARGET_WIDTH = 800


Expand All @@ -51,6 +53,12 @@ class Base


# Blank space to the left of the graph # Blank space to the left of the graph
attr_accessor :left_margin attr_accessor :left_margin

# Blank space below the title
attr_accessor :title_margin

# Blank space below the legend
attr_accessor :legend_margin


# A hash of names for the individual columns, where the key is the array # A hash of names for the individual columns, where the key is the array
# index for the column this label represents. # index for the column this label represents.
Expand Down Expand Up @@ -170,8 +178,6 @@ class Base
# Looks for Bitstream Vera as the default font. Expects an environment var # Looks for Bitstream Vera as the default font. Expects an environment var
# of MAGICK_FONT_PATH to be set. (Uses RMagick's default font otherwise.) # of MAGICK_FONT_PATH to be set. (Uses RMagick's default font otherwise.)
def initialize(target_width=DEFAULT_TARGET_WIDTH) def initialize(target_width=DEFAULT_TARGET_WIDTH)
@top_margin = @bottom_margin = @left_margin = @right_margin = 20.0

if not Numeric === target_width if not Numeric === target_width
geometric_width, geometric_height = target_width.split('x') geometric_width, geometric_height = target_width.split('x')
@columns = geometric_width.to_f @columns = geometric_width.to_f
Expand Down Expand Up @@ -215,6 +221,10 @@ def initialize_ivars
@marker_font_size = 21.0 @marker_font_size = 21.0
@legend_font_size = 20.0 @legend_font_size = 20.0
@title_font_size = 36.0 @title_font_size = 36.0

@top_margin = @bottom_margin = @left_margin = @right_margin = DEFAULT_MARGIN
@legend_margin = LEGEND_MARGIN
@title_margin = TITLE_MARGIN


@legend_box_size = 20.0 @legend_box_size = 20.0


Expand Down Expand Up @@ -602,11 +612,11 @@ def setup_graph_measurements
@graph_right = @raw_columns - @graph_right_margin @graph_right = @raw_columns - @graph_right_margin
@graph_width = @raw_columns - @graph_left - @graph_right_margin @graph_width = @raw_columns - @graph_left - @graph_right_margin


# When @hide title, leave a TITLE_MARGIN space for aesthetics. # When @hide title, leave a title_margin space for aesthetics.
# Same with @hide_legend # Same with @hide_legend
@graph_top = @top_margin + @graph_top = @top_margin +
(@hide_title ? TITLE_MARGIN : @title_caps_height + TITLE_MARGIN * 2) + (@hide_title ? title_margin : @title_caps_height + title_margin ) +
(@hide_legend ? LEGEND_MARGIN : @legend_caps_height + LEGEND_MARGIN * 2) (@hide_legend ? legend_margin : @legend_caps_height + legend_margin)


x_axis_label_height = @x_axis_label.nil? ? 0.0 : x_axis_label_height = @x_axis_label.nil? ? 0.0 :
@marker_caps_height + LABEL_MARGIN @marker_caps_height + LABEL_MARGIN
Expand Down Expand Up @@ -773,8 +783,8 @@ def draw_legend


current_x_offset = center(sum(label_widths.first)) current_x_offset = center(sum(label_widths.first))
current_y_offset = @hide_title ? current_y_offset = @hide_title ?
@top_margin + LEGEND_MARGIN : @top_margin + title_margin :
@top_margin + TITLE_MARGIN + @title_caps_height + LEGEND_MARGIN @top_margin + title_margin + @title_caps_height


@legend_labels.each_with_index do |legend_label, index| @legend_labels.each_with_index do |legend_label, index|


Expand Down Expand Up @@ -809,7 +819,7 @@ def draw_legend


label_widths.shift label_widths.shift
current_x_offset = center(sum(label_widths.first)) unless label_widths.empty? current_x_offset = center(sum(label_widths.first)) unless label_widths.empty?
line_height = [@legend_caps_height, legend_square_width].max + LEGEND_MARGIN line_height = [@legend_caps_height, legend_square_width].max + legend_margin
if label_widths.length > 0 if label_widths.length > 0
# Wrap to next line and shrink available graph dimensions # Wrap to next line and shrink available graph dimensions
current_y_offset += line_height current_y_offset += line_height
Expand Down
2 changes: 2 additions & 0 deletions test/test_bar.rb
Expand Up @@ -20,6 +20,7 @@ def setup
def test_bar_graph def test_bar_graph
g = setup_basic_graph g = setup_basic_graph
g.title = "Bar Graph Test" g.title = "Bar Graph Test"
g.title_margin = 100
g.write("test/output/bar_keynote.png") g.write("test/output/bar_keynote.png")


g = setup_basic_graph g = setup_basic_graph
Expand All @@ -36,6 +37,7 @@ def test_bar_graph
def test_bar_graph_set_colors def test_bar_graph_set_colors
g = Gruff::Bar.new g = Gruff::Bar.new
g.title = "Bar Graph With Manual Colors" g.title = "Bar Graph With Manual Colors"
g.legend_margin = 50
g.labels = { g.labels = {
0 => '5/6', 0 => '5/6',
1 => '5/15', 1 => '5/15',
Expand Down

0 comments on commit deedb4f

Please sign in to comment.