draw_legend (Gruff::Base)

    # File lib/gruff/base.rb, line 721
        def draw_legend
          return if @hide_legend

          @legend_labels = @data.collect {|item| item[DATA_LABEL_INDEX] }

          legend_square_width = @legend_box_size # small square with color of this item

          # May fix legend drawing problem at small sizes
          @d.font = @font if @font
          @d.pointsize = @legend_font_size

          metrics = @d.get_type_metrics(@base_image, @legend_labels.join(''))
          legend_text_width = metrics.width
          legend_width = legend_text_width +
                        (@legend_labels.length * legend_square_width * 2.7)
          legend_left = (@raw_columns - legend_width) / 2
          legend_increment = legend_width / @legend_labels.length.to_f

          current_x_offset = legend_left
          current_y_offset =  @hide_title ?
                              @top_margin + LEGEND_MARGIN :
                              @top_margin +
                              TITLE_MARGIN + @title_caps_height +
                              LEGEND_MARGIN

          debug { @d.line 0.0, current_y_offset, @raw_columns, current_y_offset }

          @legend_labels.each_with_index do |legend_label, index|

            # Draw label
            @d.fill = @font_color
            @d.font = @font if @font
            @d.pointsize = scale_fontsize(@legend_font_size)
            @d.stroke('transparent')
            @d.font_weight = NormalWeight
            @d.gravity = WestGravity
            @d = @d.annotate_scaled( @base_image,
                              @raw_columns, 1.0,
                              current_x_offset + (legend_square_width * 1.7), current_y_offset,
                              legend_label.to_s, @scale)

            # Now draw box with color of this dataset
            @d = @d.stroke('transparent')
            @d = @d.fill @data[index][DATA_COLOR_INDEX]
            @d = @d.rectangle(current_x_offset,
                              current_y_offset - legend_square_width / 2.0,
                              current_x_offset + legend_square_width,
                              current_y_offset + legend_square_width / 2.0)

            @d.pointsize = @legend_font_size
            metrics = @d.get_type_metrics(@base_image, legend_label.to_s)
            current_string_offset = metrics.width + (legend_square_width * 2.7)
            current_x_offset += current_string_offset
          end
          @color_index = 0
        end