Class: CTioga2::Graphics::Legends::MultiColumnLegend

Inherits:
LegendItem
  • Object
show all
Defined in:
lib/ctioga2/graphics/legends/multicols.rb

Overview

TODO:

Add support for tiling on lines too ;-)…

TODO:

Add support for filling up to some width/height.

TODO:

Choose the order (vert first or horiz first)

This class is an item that holds other items, and displays them in columns.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cols = 2) ⇒ MultiColumnLegend

Returns a new instance of MultiColumnLegend.



41
42
43
44
45
# File 'lib/ctioga2/graphics/legends/multicols.rb', line 41

def initialize(cols = 2)
  super()
  @storage = LegendStorage.new
  @style = Styles::MultiColumnLegendStyle.new
end

Instance Attribute Details

#storageObject

The underlying storage



36
37
38
# File 'lib/ctioga2/graphics/legends/multicols.rb', line 36

def storage
  @storage
end

#styleObject

The style



39
40
41
# File 'lib/ctioga2/graphics/legends/multicols.rb', line 39

def style
  @style
end

Instance Method Details

#add_item(item) ⇒ Object

Adds an item to the underlying storage.



48
49
50
# File 'lib/ctioga2/graphics/legends/multicols.rb', line 48

def add_item(item)
  @storage.add_item(item)
end

#draw(t, legend_style, x, y) ⇒ Object

Draws all the legends



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
# File 'lib/ctioga2/graphics/legends/multicols.rb', line 53

def draw(t, legend_style, x, y)
  items = storage.harvest_contents()
  size(t, legend_style)

  index = 0
  
  dy = 0
  dx = 0
  cur_height = legend_style.dy_to_figure(t)
  
  for it in items
    w, h = it.size(t, legend_style)
    col = index % @style.columns

    # Flush
    if col == 0
      dy -= cur_height unless index == 0
      dx = 0
      cur_height = h
    else
      dx += @column_widths[col - 1]
    end


    if cur_height < h
      cur_height = h
    end

    it.draw(t, legend_style, x + dx, y + dy)
    index += 1
  end
  
end

#size(t, legend_style) ⇒ Object

Computes the size of all the legends, and also update the



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ctioga2/graphics/legends/multicols.rb', line 89

def size(t, legend_style)
  items = storage.harvest_contents()

  # Always that much !
  cur_height = legend_style.dy_to_figure(t)
  widths = [0] * @style.columns
  height = 0
  index = 0
  for it in items
    w,h = it.size(t, legend_style)
    col = index % @style.columns

    # Flush
    if col == 0
      height += cur_height unless index == 0
      cur_height = h
    end
    if widths[col] < w
      widths[col] = w
    end
    if cur_height < h
      cur_height = h
    end
    index += 1
  end

  height += cur_height
  # Now add padding to the columns widths:
  (widths.size()-1).times do |i|
    widths[i] += @style.dx.to_figure(t, :x)
  end

  @column_widths = widths
  width = widths.reduce(:+) # Do padding !
  return [width, height]
end