Module: Gruff::Mini::Legend
Overview
A module to handle the small legend.
Instance Attribute Summary collapse
-
#hide_mini_legend ⇒ Object
Returns the value of attribute hide_mini_legend.
-
#legend_position ⇒ Object
Returns the value of attribute legend_position.
Instance Method Summary collapse
- #calculate_legend_width ⇒ Object
- #calculate_line_height ⇒ Object
-
#draw_vertical_legend ⇒ Object
Draw the legend beneath the existing graph.
-
#expand_canvas_for_vertical_legend ⇒ Object
The canvas needs to be bigger so we can put the legend beneath it.
- #initialize ⇒ Object
-
#truncate_legend_label(label) ⇒ Object
Shorten long labels so they will fit on the canvas.
Instance Attribute Details
#hide_mini_legend ⇒ Object
Returns the value of attribute hide_mini_legend.
7 8 9 |
# File 'lib/gruff/mini/legend.rb', line 7 def hide_mini_legend @hide_mini_legend end |
#legend_position ⇒ Object
Returns the value of attribute legend_position.
7 8 9 |
# File 'lib/gruff/mini/legend.rb', line 7 def legend_position @legend_position end |
Instance Method Details
#calculate_legend_width ⇒ Object
43 44 45 46 |
# File 'lib/gruff/mini/legend.rb', line 43 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_height ⇒ Object
39 40 41 |
# File 'lib/gruff/mini/legend.rb', line 39 def calculate_line_height calculate_caps_height(@legend_font_size) * 1.7 end |
#draw_vertical_legend ⇒ Object
Draw the legend beneath the existing graph.
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 |
# File 'lib/gruff/mini/legend.rb', line 51 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 case @legend_position when :right 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 @legend_labels.each_with_index do |legend_label, index| # Draw label label = truncate_legend_label(legend_label) text_renderer = Gruff::Renderer::Text.new(label, font: @font, size: @legend_font_size, color: @font_color) x_offset = current_x_offset + (legend_square_width * 1.7) text_renderer.add_to_render_queue(@raw_columns, 1.0, x_offset, current_y_offset, Magick::WestGravity) # Now draw box with color of this dataset rect_renderer = Gruff::Renderer::Rectangle.new(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) current_y_offset += calculate_line_height end end |
#expand_canvas_for_vertical_legend ⇒ Object
The canvas needs to be bigger so we can put the legend beneath it.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/gruff/mini/legend.rb', line 18 def return if @hide_mini_legend @legend_labels = store.data.map(&:label) legend_height = scale_fontsize(store.length * calculate_line_height + @top_margin + @bottom_margin) @original_rows = @raw_rows @original_columns = @raw_columns case @legend_position when :right @rows = [@rows, legend_height].max @columns += calculate_legend_width + @left_margin else @rows += store.length * calculate_caps_height(scale_fontsize(@legend_font_size)) * 1.7 end Gruff::Renderer.setup(@columns, @rows, @font, @scale, @theme_options) end |
#initialize ⇒ Object
9 10 11 12 13 |
# File 'lib/gruff/mini/legend.rb', line 9 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...
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/gruff/mini/legend.rb', line 90 def truncate_legend_label(label) truncated_label = label.to_s font_size = scale_fontsize(@legend_font_size) max_width = @columns - @legend_left_margin - @right_margin while calculate_width(font_size, truncated_label) > max_width && truncated_label.length > 1 truncated_label = truncated_label[0..truncated_label.length - 2] end truncated_label + (truncated_label.length < label.to_s.length ? '...' : '') end |