Index: gruff-0.2.8/lib/gruff/pie.rb
===================================================================
--- gruff-0.2.8/lib/gruff/pie.rb
+++ gruff-0.2.8/lib/gruff/pie.rb
@@ -34,13 +34,16 @@
       if data_row[DATA_VALUES_INDEX][0] > 0
         @d = @d.stroke data_row[DATA_COLOR_INDEX]
         @d = @d.fill 'transparent'
-        @d.stroke_width(radius) # stroke width should be equal to radius. we'll draw centered on (radius / 2)
+        # Stroke width should be equal to radius. 
+        # We'll draw centered on (radius / 2)
+        @d.stroke_width(radius)
 
         current_degrees = (data_row[DATA_VALUES_INDEX][0] / total_sum) * 360.0 
 
-        # ellipse will draw the the stroke centered on the first two parameters offset by the second two.
-        # therefore, in order to draw a circle of the proper diameter we must center the stroke at
-        # half the radius for both x and y
+        # Ellipse will draw the the stroke centered on the first two 
+        # parameters offset by the second two.
+        # Therefore, in order to draw a circle of the proper diameter we 
+        # must center the stroke at half the radius for both x and y
         @d = @d.ellipse(center_x, center_y, 
                   radius / 2.0, radius / 2.0,
                   prev_degrees, prev_degrees + current_degrees + 0.5) # <= +0.5 'fudge factor' gets rid of the ugly gaps
Index: gruff-0.2.8/lib/gruff/base.rb
===================================================================
--- gruff-0.2.8/lib/gruff/base.rb
+++ gruff-0.2.8/lib/gruff/base.rb
@@ -70,6 +70,9 @@
     # Get or set the list of colors that will be used to draw the bars or lines.
     attr_accessor :colors
 
+    # Extra params for the legend
+    attr_accessor :minimum_value, :maximum_value
+
     # The large title of the graph displayed at the top
     attr_accessor :title
 
@@ -401,6 +404,13 @@
     # Subclasses should start by calling super() for this method.
     def draw
       make_stacked if @stacked
+
+      # Handle case where a graph contains a single data value.
+      # This case causes RMagick to hang (see bug # 3589 and # 4027)
+      if !@maximum_value.nil? and @minimum_value == @maximum_value
+        @minimum_value = @maximum_value - 1
+      end
+
       setup_drawing
       
       debug {
@@ -431,6 +441,7 @@
       sort_norm_data() if @sort # Sort norm_data with avg largest values set first (for display)
       
       draw_legend()
+      setup_graph_height()
       draw_line_markers()
       draw_axis_labels()
       draw_title
@@ -463,6 +474,10 @@
       @spread = @spread > 0 ? @spread : 1
     end
 
+    def setup_graph_height
+      @graph_height = @graph_bottom - @graph_top
+    end
+
     ##
     # Calculates size of drawable area, general font dimensions, etc.
     
@@ -517,7 +532,7 @@
       @graph_bottom = @raw_rows - @graph_bottom_margin -
                       (@x_axis_label.nil? ? 0.0 : @marker_caps_height + LABEL_MARGIN)
       
-      @graph_height = @graph_bottom - @graph_top
+      setup_graph_height
     end
 
     # Draw the optional labels for the x axis and y axis.
@@ -651,9 +666,20 @@
 
       metrics = @d.get_type_metrics(@base_image, @legend_labels.join(''))
       legend_text_width = metrics.width
+      legend_text_height = metrics.height
+
       legend_width = legend_text_width + 
                     (@legend_labels.length * legend_square_width * 2.7)
+
+      # If the legend will fit on
+      # a single line recalculate the left position to center the legend
       legend_left = (@raw_columns - legend_width) / 2
+      legend_left = 20
+      if  @raw_columns - legend_width > 0
+        legend_left = (@raw_columns - legend_width) / 2
+      end
+      legend_text_height_with_padding = legend_text_height + 10
+
       legend_increment = legend_width / @legend_labels.length.to_f
 
       current_x_offset = legend_left
@@ -670,7 +696,30 @@
         # Draw label
         @d.fill = @font_color
         @d.font = @font if @font
+
+        # Calculate the metrics for the current legend item based on 
+        # the actual font size
+        @d.pointsize = @legend_font_size
+        legend_item_width = @d.get_type_metrics(@base_image,legend_label.to_s).width + (legend_square_width * 2.7)
+        if current_x_offset + legend_item_width > @graph_right
+          # Reset the x offset to the leftmost legend column
+          current_x_offset = legend_left
+          # Move the y offset down by one text row
+          current_y_offset = current_y_offset + legend_text_height_with_padding 
+        end
+
+        # Scale pointsize now that calculations are complete
         @d.pointsize = scale_fontsize(@legend_font_size)
+
+        # Detect if the current row of legend text will overrun the graph. 
+        # If it will,
+        # Shift graph_top down by two legend text rows. 
+        # This spacing should leave sufficent
+        # room for a label in the case of pie graphs.
+        if current_y_offset + legend_text_height_with_padding + 40 >= @graph_top
+          @graph_top = current_y_offset + legend_text_height_with_padding + 40
+        end
+
         @d.stroke('transparent')
         @d.font_weight = NormalWeight
         @d.gravity = WestGravity
