Module: Gruff::Mini::Legend

Included in:
Bar, Pie, SideBar
Defined in:
lib/gruff/mini/legend.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#hide_mini_legendObject

Returns the value of attribute hide_mini_legend.



5
6
7
# File 'lib/gruff/mini/legend.rb', line 5

def hide_mini_legend
  @hide_mini_legend
end

#legend_positionObject

Returns the value of attribute legend_position.



5
6
7
# File 'lib/gruff/mini/legend.rb', line 5

def legend_position
  @legend_position
end

Instance Method Details

#calculate_legend_widthObject



42
43
44
45
# File 'lib/gruff/mini/legend.rb', line 42

def calculate_legend_width
  width = @legend_labels.map { |label| calculate_width(@legend_font_size, label) }.max
  scale_fontsize(width + 40*1.7)
end

#calculate_line_heightObject



38
39
40
# File 'lib/gruff/mini/legend.rb', line 38

def calculate_line_height
  calculate_caps_height(@legend_font_size) * 1.7
end

#draw_vertical_legendObject

Draw the legend beneath the existing graph.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/gruff/mini/legend.rb', line 50

def draw_vertical_legend
  return if @hide_mini_legend

  legend_square_width = 40.0 # small square with color of this item
  @legend_left_margin = 100.0
  legend_top_margin = 40.0

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

  case @legend_position
  when :right then
    current_x_offset = @original_columns + @left_margin
    current_y_offset = @top_margin + legend_top_margin
  else
    current_x_offset = @legend_left_margin
    current_y_offset = @original_rows + legend_top_margin
  end

  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 = Magick::NormalWeight
    @d.gravity = Magick::WestGravity
    @d = @d.annotate_scaled( @base_image,
                             @raw_columns, 1.0,
                             current_x_offset + (legend_square_width * 1.7), current_y_offset,
                             truncate_legend_label(legend_label), @scale)

    # Now draw box with color of this dataset
    @d = @d.stroke 'transparent'
    @d = @d.fill @data[index][Gruff::Base::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)

    current_y_offset += calculate_line_height
  end
  @color_index = 0
end

#expand_canvas_for_vertical_legendObject

The canvas needs to be bigger so we can put the legend beneath it.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/gruff/mini/legend.rb', line 16

def expand_canvas_for_vertical_legend
  return if @hide_mini_legend

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

  legend_height = scale_fontsize(
                                 @data.length * calculate_line_height +
                                 @top_margin + @bottom_margin)

  @original_rows = @raw_rows
  @original_columns = @raw_columns

  case @legend_position
  when :right then
    @rows = [@rows, legend_height].max
    @columns += calculate_legend_width + @left_margin
  else
    @rows += @data.length * calculate_caps_height(scale_fontsize(@legend_font_size)) * 1.7
  end
  render_background
end

#initializeObject



7
8
9
10
11
# File 'lib/gruff/mini/legend.rb', line 7

def initialize(*)
  @hide_mini_legend = false
  @legend_position = nil
  super
end

#truncate_legend_label(label) ⇒ Object

Shorten long labels so they will fit on the canvas.

Department of Hu...


104
105
106
107
108
109
110
# File 'lib/gruff/mini/legend.rb', line 104

def truncate_legend_label(label)
  truncated_label = label.to_s
  while calculate_width(scale_fontsize(@legend_font_size), truncated_label) > (@columns - @legend_left_margin - @right_margin) && (truncated_label.length > 1)
    truncated_label = truncated_label[0..truncated_label.length-2]
  end
  truncated_label + (truncated_label.length < label.to_s.length ? "..." : '')
end