Class: Scruffy::Components::Legend

Inherits:
Base
  • Object
show all
Defined in:
lib/scruffy/components/legend.rb

Constant Summary collapse

FONT_SIZE =
80

Instance Attribute Summary

Attributes inherited from Base

#id, #options, #position, #size, #visible

Instance Method Summary collapse

Methods inherited from Base

#initialize, #process, #render

Constructor Details

This class inherits a constructor from Scruffy::Components::Base

Instance Method Details

#draw(svg, bounds, options = {}) ⇒ Object



48
49
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
# File 'lib/scruffy/components/legend.rb', line 48

def draw(svg, bounds, options={})
  vertical = options[:vertical_legend]
  legend_info = relevant_legend_info(options[:layers])
  @line_height, x, y, size = 0
  if vertical
    set_line_height = 0.08 * bounds[:height]
    @line_height = bounds[:height] / legend_info.length
    @line_height = set_line_height if @line_height >
    set_line_height
  else
    set_line_height = 0.90 * bounds[:height]
    @line_height = set_line_height
  end
  
  text_height = @line_height * FONT_SIZE / 100
  # #TODO how does this related to @points?
  active_width, points = layout(legend_info, vertical)
  
  offset = (bounds[:width] - active_width) / 2    # Nudge over a bit for true centering
  
  # Render Legend
  points.each_with_index do |point, idx|
    if vertical
      x = 0
      y = point
      size = @line_height * 0.5
    else
      x = offset + point
      y = 0
      size = relative(50)
    end
    
    # "#{x} #{y} #{@line_height} #{size}"
    
    svg.rect(:x => x, 
             :y => y, 
             :width => size, 
             :height => size,
             :fill => legend_info[idx][:color])
    
    svg.text(legend_info[idx][:title], 
             :x => x + @line_height, 
             :y => y + text_height * 0.75,
      'font-size' => text_height, 
      'font-family' => options[:theme].font_family,
    :style => "color: #{options[:theme].marker || 'white'}",
    :fill => (options[:theme].marker || 'white'))
  end
end