class CTioga2::Graphics::Legends::MultiColumnLegend
This class is an item that holds other items, and displays them in columns.
@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)
Attributes
storage[RW]
The underlying storage
style[RW]
The style
Public Class Methods
new(cols = 2)
click to toggle source
Calls superclass method
CTioga2::Graphics::Legends::LegendItem::new
# File lib/ctioga2/graphics/legends/multicols.rb, line 41 def initialize(cols = 2) super() @storage = LegendStorage.new @style = Styles::MultiColumnLegendStyle.new end
Public Instance Methods
add_item(item)
click to toggle source
Adds an item to the underlying storage.
# File lib/ctioga2/graphics/legends/multicols.rb, line 48 def add_item(item) @storage.add_item(item) end
draw(t, legend_style, x, y)
click to toggle source
Draws all the legends
# 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)
click to toggle source
Computes the size of all the legends, and also update the @column_widths array.
# 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