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



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/scruffy/components/legend.rb', line 6

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