Permalink
Browse files

Allow mini legends to be rendered on the right hand side of the graph.

  • Loading branch information...
1 parent ea68aa1 commit 8a88706b685c877f0fce7d9491869843ca7a5706 @jcoglan jcoglan committed Mar 13, 2010
Showing with 42 additions and 9 deletions.
  1. +36 −9 lib/gruff/mini/legend.rb
  2. +6 −0 test/test_mini_pie.rb
View
@@ -2,27 +2,48 @@ module Gruff
module Mini
module Legend
- attr_accessor :hide_mini_legend
+ attr_accessor :hide_mini_legend, :legend_position
##
# The canvas needs to be bigger so we can put the legend beneath it.
def expand_canvas_for_vertical_legend
return if @hide_mini_legend
-
+
+ @legend_labels = @data.collect {|item| item[Gruff::Base::DATA_LABEL_INDEX] }
+
+ legend_height = scale_fontsize(
+ @data.length * calculate_line_height +
+ @top_margin + @bottom_margin)
+
@original_rows = @raw_rows
- @rows += @data.length * calculate_caps_height(scale_fontsize(@legend_font_size)) * 1.7
+ @original_columns = @raw_columns
+
+ case @legend_position
+ when :right then
+ @rows = [@rows, legend_height].max
+ @columns += calculate_legend_width + @left_margin
+ else
+ @rows += @data.length * calculate_caps_height(scale_fontsize(@legend_font_size)) * 1.7
+ end
render_background
end
-
+
+ def calculate_line_height
+ calculate_caps_height(@legend_font_size) * 1.7
+ end
+
+ def calculate_legend_width
+ width = @legend_labels.map { |label| calculate_width(@legend_font_size, label) }.max
+ scale_fontsize(width + 40*1.7)
+ end
+
##
# Draw the legend beneath the existing graph.
def draw_vertical_legend
return if @hide_mini_legend
- @legend_labels = @data.collect {|item| item[Gruff::Base::DATA_LABEL_INDEX] }
-
legend_square_width = 40.0 # small square with color of this item
legend_square_margin = 10.0
@legend_left_margin = 100.0
@@ -32,8 +53,14 @@ def draw_vertical_legend
@d.font = @font if @font
@d.pointsize = @legend_font_size
- current_x_offset = @legend_left_margin
- current_y_offset = @original_rows + legend_top_margin
+ case @legend_position
+ when :right then
+ current_x_offset = @original_columns + @left_margin
+ current_y_offset = @top_margin + legend_top_margin
+ else
+ current_x_offset = @legend_left_margin
+ current_y_offset = @original_rows + legend_top_margin
+ end
debug { @d.line 0.0, current_y_offset, @raw_columns, current_y_offset }
@@ -59,7 +86,7 @@ def draw_vertical_legend
current_x_offset + legend_square_width,
current_y_offset + legend_square_width / 2.0)
- current_y_offset += calculate_caps_height(@legend_font_size) * 1.7
+ current_y_offset += calculate_line_height
end
@color_index = 0
end
View
@@ -7,6 +7,12 @@ def test_simple_pie
g = setup_basic_graph(Gruff::Mini::Pie, 200)
write_test_file g, 'mini_pie.png'
end
+
+ def test_pie_with_legend_right
+ g = setup_basic_graph(Gruff::Mini::Pie, 200)
+ g.legend_position = :right
+ write_test_file g, 'mini_pie_right_legend.png'
+ end
# def test_code_sample
# g = Gruff::Mini::Pie.new(200)

0 comments on commit 8a88706

Please sign in to comment.