lib/gruff/base.rb
# Draws a legend with the names of the datasets matched
# to the colors used to draw them.
Draws a legend with the names of the datasets matched
to the colors used to draw them.
Draws a legend with the names of the datasets matched to the colors used to draw them .
def draw_legend
      return if @hide_legend
      legend_labels = store.data.map(&:label)
      legend_square_width = @legend_box_size 
      label_widths = calculate_legend_label_widths_for_each_line(legend_labels, legend_square_width)
      current_x_offset = center(label_widths.first.sum)
      current_y_offset = begin
        if @legend_at_bottom
          @graph_bottom + @legend_margin + @legend_caps_height + LABEL_MARGIN
        else
          hide_title? ? @top_margin + @title_margin : @top_margin + @title_margin + @title_caps_height
        end
      end
      legend_labels.each_with_index do |legend_label, index|
        next if legend_label.empty?
        text_renderer = Gruff::Renderer::Text.new(renderer, legend_label, font: @legend_font)
        text_renderer.add_to_render_queue(@raw_columns, 1.0, current_x_offset + (legend_square_width * 1.7), current_y_offset, Magick::WestGravity)
        rect_renderer = Gruff::Renderer::Rectangle.new(renderer, color: store.data[index].color)
        rect_renderer.render(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)
        width = calculate_width(@legend_font, legend_label)
        current_x_offset += width + (legend_square_width * 2.7)
        label_widths.first.shift
        if label_widths.first.empty?
          label_widths.shift
          current_x_offset = center(label_widths.first.sum) unless label_widths.empty?
          line_height = [@legend_caps_height, legend_square_width].max + @legend_margin
          unless label_widths.empty?
            current_y_offset += line_height
          end
        end
      end
    end
def draw_legend return if @hide_legend legend_labels = store . data . map ( & :label ) legend_square_width = @legend_box_size label_widths = calculate_legend_label_widths_for_each_line ( legend_labels , legend_square_width ) current_x_offset = center ( label_widths . first . sum ) current_y_offset = begin if @legend_at_bottom @graph_bottom + @legend_margin + @legend_caps_height + LABEL_MARGIN else hide_title? ? @top_margin + @title_margin : @top_margin + @title_margin + @title_caps_height end end legend_labels . each_with_index do | legend_label , index | next if legend_label . empty? text_renderer = Gruff :: Renderer :: Text . new ( renderer , legend_label , font : @legend_font ) text_renderer . add_to_render_queue ( @raw_columns , 1.0 , current_x_offset + ( legend_square_width * 1.7 ) , current_y_offset , Magick :: WestGravity ) rect_renderer = Gruff :: Renderer :: Rectangle . new ( renderer , color : store . data [ index ] . color ) rect_renderer . render ( 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 ) width = calculate_width ( @legend_font , legend_label ) current_x_offset += width + ( legend_square_width * 2.7 ) label_widths . first . shift if label_widths . first . empty? label_widths . shift current_x_offset = center ( label_widths . first . sum ) unless label_widths . empty? line_height = [ @legend_caps_height , legend_square_width ] . max + @legend_margin unless label_widths . empty? current_y_offset += line_height end end end end
Draws a legend with the names of the datasets matched
to the colors used to draw them.
Draws a legend with the names of the datasets matched to the colors used to draw them .
# small square with color of this item # Draw label # Now draw box with color of this dataset # Handle wrapping # Wrap to next line and shrink available graph dimensions