| Module | Gruff::Mini::Legend |
| In: |
lib/gruff/mini/legend.rb
|
| hide_mini_legend | [RW] |
Draw the legend beneath the existing graph.
# File lib/gruff/mini/legend.rb, line 21
21: def draw_vertical_legend
22: return if @hide_mini_legend
23:
24: @legend_labels = @data.collect {|item| item[Gruff::Base::DATA_LABEL_INDEX] }
25:
26: legend_square_width = 40.0 # small square with color of this item
27: legend_square_margin = 10.0
28: @legend_left_margin = 100.0
29: legend_top_margin = 40.0
30:
31: # May fix legend drawing problem at small sizes
32: @d.font = @font if @font
33: @d.pointsize = @legend_font_size
34:
35: current_x_offset = @legend_left_margin
36: current_y_offset = @original_rows + legend_top_margin
37:
38: debug { @d.line 0.0, current_y_offset, @raw_columns, current_y_offset }
39:
40: @legend_labels.each_with_index do |legend_label, index|
41:
42: # Draw label
43: @d.fill = @font_color
44: @d.font = @font if @font
45: @d.pointsize = scale_fontsize(@legend_font_size)
46: @d.stroke = 'transparent'
47: @d.font_weight = Magick::NormalWeight
48: @d.gravity = Magick::WestGravity
49: @d = @d.annotate_scaled( @base_image,
50: @raw_columns, 1.0,
51: current_x_offset + (legend_square_width * 1.7), current_y_offset,
52: truncate_legend_label(legend_label), @scale)
53:
54: # Now draw box with color of this dataset
55: @d = @d.stroke 'transparent'
56: @d = @d.fill @data[index][Gruff::Base::DATA_COLOR_INDEX]
57: @d = @d.rectangle(current_x_offset,
58: current_y_offset - legend_square_width / 2.0,
59: current_x_offset + legend_square_width,
60: current_y_offset + legend_square_width / 2.0)
61:
62: current_y_offset += calculate_caps_height(@legend_font_size) * 1.7
63: end
64: @color_index = 0
65: end
The canvas needs to be bigger so we can put the legend beneath it.
# File lib/gruff/mini/legend.rb, line 10
10: def expand_canvas_for_vertical_legend
11: return if @hide_mini_legend
12:
13: @original_rows = @raw_rows
14: @rows += @data.length * calculate_caps_height(scale_fontsize(@legend_font_size)) * 1.7
15: render_background
16: end
Shorten long labels so they will fit on the canvas.
Department of Hu...
# File lib/gruff/mini/legend.rb, line 72
72: def truncate_legend_label(label)
73: truncated_label = label.to_s
74: while calculate_width(scale_fontsize(@legend_font_size), truncated_label) > (@columns - @legend_left_margin - @right_margin) && (truncated_label.length > 1)
75: truncated_label = truncated_label[0..truncated_label.length-2]
76: end
77: truncated_label + (truncated_label.length < label.to_s.length ? "..." : '')
78: end