6 # The canvas needs to be bigger so we can put the legend beneath it.
8 def expand_canvas_for_vertical_legend
9 @original_rows = @raw_rows
10 @rows += @data.length * calculate_caps_height(scale_fontsize(@legend_font_size)) * 1.7
15 # Draw the legend beneath the existing graph.
17 def draw_vertical_legend
19 @legend_labels = @data.collect {|item| item[Gruff::Base::DATA_LABEL_INDEX] }
21 legend_square_width = 40.0 # small square with color of this item
22 legend_square_margin = 10.0
23 @legend_left_margin = 40.0
24 legend_top_margin = 40.0
26 # May fix legend drawing problem at small sizes
27 @d.font = @font if @font
28 @d.pointsize = @legend_font_size
30 current_x_offset = @graph_left + @legend_left_margin
31 current_y_offset = @original_rows + legend_top_margin
33 debug { @d.line 0.0, current_y_offset, @raw_columns, current_y_offset }
35 @legend_labels.each_with_index do |legend_label, index|
39 @d.font = @font if @font
40 @d.pointsize = scale_fontsize(@legend_font_size)
41 @d.stroke = 'transparent'
42 @d.font_weight = Magick::NormalWeight
43 @d.gravity = Magick::WestGravity
44 @d = @d.annotate_scaled( @base_image,
46 current_x_offset + (legend_square_width * 1.7), current_y_offset,
47 truncate_legend_label(legend_label), @scale)
49 # Now draw box with color of this dataset
50 @d = @d.stroke 'transparent'
51 @d = @d.fill @data[index][Gruff::Base::DATA_COLOR_INDEX]
52 @d = @d.rectangle(current_x_offset,
53 current_y_offset - legend_square_width / 2.0,
54 current_x_offset + legend_square_width,
55 current_y_offset + legend_square_width / 2.0)
57 current_y_offset += calculate_caps_height(@legend_font_size) * 1.7
63 # Shorten long labels so they will fit on the canvas.
67 def truncate_legend_label(label)
68 truncated_label = label.to_s
69 while calculate_width(scale_fontsize(@legend_font_size), truncated_label) > (@columns - @legend_left_margin - Gruff::Base::RIGHT_MARGIN) && (truncated_label.length > 1)
70 truncated_label = truncated_label[0..truncated_label.length-2]
72 truncated_label + (truncated_label.length < label.to_s.length ? "…" : '')