Class: Treemap::SquarifiedLayout
- Inherits:
-
SliceLayout
- Object
- LayoutBase
- SliceLayout
- Treemap::SquarifiedLayout
- Defined in:
- lib/treemap/squarified_layout.rb
Constant Summary
Constants inherited from SliceLayout
Treemap::SliceLayout::Horizontal, Treemap::SliceLayout::Vertical
Instance Attribute Summary
Attributes inherited from LayoutBase
Instance Method Summary collapse
- #aspect_ratio(bounds, node_prop, row_prop, axis) ⇒ Object
-
#aspect_ratio_method2(bounds, max, proportion, axis) ⇒ Object
XXX another way of computing the aspect ratio.
- #axis(bounds) ⇒ Object
- #process(node, bounds, axis = nil) ⇒ Object
- #squarify_children(node, bounds, axis) ⇒ Object
Methods inherited from SliceLayout
#flip, #horizontal?, #process_children, #sum, #vertical?
Methods inherited from LayoutBase
Constructor Details
This class inherits a constructor from Treemap::LayoutBase
Instance Method Details
#aspect_ratio(bounds, node_prop, row_prop, axis) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/treemap/squarified_layout.rb', line 93 def aspect_ratio(bounds, node_prop, row_prop, axis) height = bounds.height * row_prop width = bounds.width * node_prop if(axis == Vertical) width = bounds.width * row_prop height = bounds.height * node_prop end return 0 if width == 0 and height == 0 a = 0; b = 0; if(width > 0) a = height.to_f / width.to_f end if(height > 0) b = width.to_f / height.to_f end ratio = [a, b].max ratio end |
#aspect_ratio_method2(bounds, max, proportion, axis) ⇒ Object
XXX another way of computing the aspect ratio
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/treemap/squarified_layout.rb', line 124 def aspect_ratio_method2(bounds, max, proportion, axis) large = bounds.height small = bounds.width if(axis == Vertical) large = bounds.width small = bounds.height end ratio = (large * proportion).to_f / ((small * max).to_f / proportion.to_f).to_f if(ratio < 1) ratio = 1.to_f / ratio.to_f end ratio end |
#axis(bounds) ⇒ Object
117 118 119 120 121 |
# File 'lib/treemap/squarified_layout.rb', line 117 def axis(bounds) # XXX experiment with switching # axis = super(bounds) # flip(axis) end |
#process(node, bounds, axis = nil) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/treemap/squarified_layout.rb', line 17 def process(node, bounds, axis=nil) bounds = bounds.clone node.bounds = bounds.clone if(@position == :absolute) bounds.x2 = bounds.width bounds.y2 = bounds.height bounds.x1 = 0 bounds.y1 = 0 end if(!node.leaf?) squarify_children(node, bounds, flip(axis)) end end |
#squarify_children(node, bounds, axis) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 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 |
# File 'lib/treemap/squarified_layout.rb', line 35 def squarify_children(node, bounds, axis) parent_bounds = bounds.clone bounds = bounds.clone node.children.sort! {|a,b| b.size <=> a.size} if(node.children.size < 2) process_children(node.children, bounds, flip(axis)) end parent_size = node.size first_child = node.children.first row_size = first_child.size row_max = row_size.to_f / parent_size.to_f total = row_max prev_aspect = aspect_ratio(bounds, first_child.size.to_f / row_size.to_f, total, axis) row = [first_child] node.children[1 .. node.children.size-1].each do |c| child_prop = c.size.to_f / parent_size.to_f aspect = aspect_ratio(bounds, c.size.to_f / row_size.to_f, total + child_prop, axis) if(aspect > prev_aspect) newb = bounds.clone if(axis == Vertical) newb.x2 = bounds.x1 + ((bounds.width * total)).round else newb.y2 = bounds.y1 + ((bounds.height * total)).round end process_children(row, newb, flip(axis)) if(axis == Vertical) bounds.x1 = newb.x2 else bounds.y1 = newb.y2 end axis = flip(axis) parent_size -= row_size row_size = c.size total = row_max = row_size.to_f / parent_size.to_f prev_aspect = aspect_ratio(bounds, c.size.to_f / row_size.to_f, total, axis) row = [c] else row_size += c.size total += child_prop prev_aspect = aspect row.push(c) end end process_children(row, bounds, flip(axis)) end |