Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Provide attr_accessors for title_margin and legend_margin.

  • Loading branch information...
commit deedb4f0f0cbb92e33618fa35b1bced887ecce91 1 parent 04cb419
@jcoglan jcoglan authored
Showing with 21 additions and 9 deletions.
  1. +19 −9 lib/gruff/base.rb
  2. +2 −0  test/test_bar.rb
View
28 lib/gruff/base.rb
@@ -36,7 +36,9 @@ class Base
DATA_COLOR_INDEX = 2
# 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
@@ -51,6 +53,12 @@ class Base
# Blank space to the left of the graph
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
# index for the column this label represents.
@@ -170,8 +178,6 @@ class Base
# 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.)
def initialize(target_width=DEFAULT_TARGET_WIDTH)
- @top_margin = @bottom_margin = @left_margin = @right_margin = 20.0
-
if not Numeric === target_width
geometric_width, geometric_height = target_width.split('x')
@columns = geometric_width.to_f
@@ -215,6 +221,10 @@ def initialize_ivars
@marker_font_size = 21.0
@legend_font_size = 20.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
@@ -602,11 +612,11 @@ def setup_graph_measurements
@graph_right = @raw_columns - @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
@graph_top = @top_margin +
- (@hide_title ? TITLE_MARGIN : @title_caps_height + TITLE_MARGIN * 2) +
- (@hide_legend ? LEGEND_MARGIN : @legend_caps_height + LEGEND_MARGIN * 2)
+ (@hide_title ? title_margin : @title_caps_height + title_margin ) +
+ (@hide_legend ? legend_margin : @legend_caps_height + legend_margin)
x_axis_label_height = @x_axis_label.nil? ? 0.0 :
@marker_caps_height + LABEL_MARGIN
@@ -773,8 +783,8 @@ def draw_legend
current_x_offset = center(sum(label_widths.first))
current_y_offset = @hide_title ?
- @top_margin + LEGEND_MARGIN :
- @top_margin + TITLE_MARGIN + @title_caps_height + LEGEND_MARGIN
+ @top_margin + title_margin :
+ @top_margin + title_margin + @title_caps_height
@legend_labels.each_with_index do |legend_label, index|
@@ -809,7 +819,7 @@ def draw_legend
label_widths.shift
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
# Wrap to next line and shrink available graph dimensions
current_y_offset += line_height
View
2  test/test_bar.rb
@@ -20,6 +20,7 @@ def setup
def test_bar_graph
g = setup_basic_graph
g.title = "Bar Graph Test"
+ g.title_margin = 100
g.write("test/output/bar_keynote.png")
g = setup_basic_graph
@@ -36,6 +37,7 @@ def test_bar_graph
def test_bar_graph_set_colors
g = Gruff::Bar.new
g.title = "Bar Graph With Manual Colors"
+ g.legend_margin = 50
g.labels = {
0 => '5/6',
1 => '5/15',
Please sign in to comment.
Something went wrong with that request. Please try again.