Class: Rubyvis::Layout::Treemap
Overview
Implements a space-filling rectangular layout, with the hierarchy represented via containment. Treemaps represent nodes as boxes, with child nodes placed within parent boxes. The size of each box is proportional to the size of the node in the tree. This particular algorithm is taken from Bruls, D.M., C. Huizing, and J.J. van Wijk, <a href=“www.win.tue.nl/~vanwijk/stm.pdf”>“Squarified Treemaps”</a> in Data Visualization 2000, Proceedings of the Joint Eurographics and IEEE TCVG Sumposium on Visualization, 2000, pp. 33-42.
<p>The meaning of the exported mark prototypes changes slightly in the space-filling implementation:<ul>
<li>node
- for rendering nodes; typically a pv.Bar. The node data is populated with dx
and dy
attributes, in addition to the standard x
and y
position attributes.
<p><li>leaf
- for rendering leaf nodes only, with no fill or stroke style by default; typically a Rubyvis::Panel or another layout!
<p><li>link
- unsupported; undefined. Links are encoded implicitly in the arrangement of the space-filling nodes.
<p><li>label
- for rendering node labels; typically a Rubyvis::Label.
</ul>For more details on how to use this layout, see Rubyvis::Layout::Hierarchy.
Instance Attribute Summary
Attributes inherited from Network
Attributes inherited from Panel
Attributes inherited from Mark
#_properties, #binds, #child_index, #parent, #proto, #root, #scale, #scene, #target
Class Method Summary collapse
-
.defaults ⇒ Object
Default propertiess for treemap layouts.
Instance Method Summary collapse
- #_size(d) ⇒ Object
- #build_implied(s) ⇒ Object
-
#initialize ⇒ Treemap
constructor
A new instance of Treemap.
- #leaf ⇒ Object
- #link ⇒ Object
-
#padding(n) ⇒ Object
Alias for setting the left, right, top and bottom padding properties simultaneously.
-
#size(f) ⇒ Object
Specifies the sizing function.
Methods inherited from Hierarchy
#hierarchy_build_implied, #links
Methods inherited from Network
#_link, #_node, #_node_label, #build_properties, #network_build_implied, #nodes, #reset
Methods inherited from Rubyvis::Layout
Arc, Cluster, Grid, Hierarchy, Horizon, Indent, Matrix, Network, Pack, Partition, Stack, Tree, Treemap, attr_accessor_dsl, #build_properties, #layout_build_implied, #layout_build_properties
Methods inherited from Panel
#add, #anchor, #bind, #build_instance, #children_inspect, #panel_build_implied, #to_svg, #type
Methods inherited from Bar
Methods inherited from Mark
#add, #anchor, #area, attr_accessor_dsl, #bar, #bind, #build, #build_instance, #build_properties, #context, #context_apply, #context_clear, #cousin, #delete_index, #dot, #event, #execute, #first, #image, index, #index, index=, #index=, #index_defined?, #instance, #instances, #label, #last, #layout_arc, #layout_cluster, #layout_grid, #layout_horizon, #layout_indent, #layout_matrix, #layout_pack, #layout_partition, #layout_partition_fill, #layout_stack, #layout_tree, #layout_treemap, #line, #margin, #mark_anchor, #mark_bind, #mark_build_implied, #mark_build_instance, #mark_build_properties, #mark_extend, mark_method, #panel, #properties, properties, property_method, #property_value, #render, #rule, scene, scene=, #sibling, stack, stack=, #type, #wedge
Constructor Details
#initialize ⇒ Treemap
Returns a new instance of Treemap.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/rubyvis/layout/treemap.rb', line 38 def initialize super @size=lambda {|d| d.node_value.to_f} @node.stroke_style("#fff"). fill_style("rgba(31, 119, 180, .25)"). width(lambda {|n| n.dx}). height(lambda {|n| n.dy }) @node_label. visible(lambda {|n| !n.first_child }). left(lambda {|n| n.x + (n.dx / 2.0) }). top(lambda {|n| n.y + (n.dy / 2.0) }). text_align("center"). text_angle(lambda {|n| n.dx > n.dy ? 0 : -Math::PI / 2.0 }) end |
Class Method Details
.defaults ⇒ Object
Default propertiess for treemap layouts. The default mode is “squarify” and the default order is “ascending”.
148 149 150 151 152 |
# File 'lib/rubyvis/layout/treemap.rb', line 148 def self.defaults Rubyvis::Layout::Treemap.new.mark_extend(Rubyvis::Layout::Hierarchy.defaults). mode("squarify"). # squarify, slice-and-dice, slice, dice order('ascending') # ascending, descending, reverse, nil end |
Instance Method Details
#_size(d) ⇒ Object
159 160 161 |
# File 'lib/rubyvis/layout/treemap.rb', line 159 def _size(d) @size.call(d) end |
#build_implied(s) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/rubyvis/layout/treemap.rb', line 182 def build_implied(s) return nil if hierarchy_build_implied(s) that=self nodes = s.nodes root = nodes[0] stack = Mark.stack left = s.padding_left right = s.padding_right top = s.padding_top bottom = s.padding_bottom left||=0 right||=0 top||=0 bottom||=0 size=lambda {|n| n.size} round = s.round ? lambda {|a| a.round } : lambda {|a| a.to_f} mode = s.mode slice=lambda { |row, sum, horizontal, x, y, w, h| # puts "slice:#{sum},#{horizontal},#{x},#{y},#{w},#{h}" d=0 row.size.times {|i| n=row[i] # puts "i:#{i},d:#{d}" if horizontal n.x = x + d n.y = y d += n.dx = round.call(w * n.size / sum.to_f) n.dy = h else n.x = x n.y = y + d n.dx = w d += n.dy = round.call(h * n.size / sum.to_f) end # puts "n.x:#{n.x}, n.y:#{n.y}, n.dx:#{n.dx}, n.dy:#{n.dy}" } if (row.last) # correct on-axis rounding error n=row.last if (horizontal) n.dx += w - d else n.dy += h - d end end } ratio=lambda {|row, l| rmax = -Infinity rmin = Infinity s = 0 row.each_with_index {|v,i| r = v.size rmin = r if (r < rmin) rmax = r if (r > rmax) s += r } s = s * s l = l * l [l * rmax / s.to_f, s.to_f / (l * rmin)].max } layout=lambda {|n,i| x = n.x + left y = n.y + top w = n.dx - left - right h = n.dy - top - bottom # puts "Layout: '#{n.node_name}', #{n.x}, #{n.y}, #{n.dx}, #{n.dy}" #/* Assume squarify by default. */ if (mode != "squarify") slice.call(n.child_nodes, n.size, ( mode == "slice" ? true : mode == "dice" ? false : (i & 1)!=0), x, y, w, h) else row = [] mink = Infinity l = [w,h].min k = w * h / n.size.to_f #/* Abort if the size is nonpositive. */ if (n.size > 0) #/* Scale the sizes to fill the current subregion. */ n.visit_before {|n1,i1| n1.size *= k } #/** @private Position the specified nodes along one dimension. */ position=lambda {|row1| horizontal = w == l sum = Rubyvis.sum(row1, size) r = l>0 ? round.call(sum / l.to_f) : 0 slice.call(row1, sum, horizontal, x, y, horizontal ? w : r, horizontal ? r : h) if horizontal y += r h -= r else x += r w -= r end l = [w, h].min horizontal } children = n.child_nodes.dup # copy while (children.size>0) do child = children[children.size - 1] if (child.size==0) children.pop next end row.push(child) k = ratio.call(row, l) if (k <= mink) children.pop mink = k else row.pop position.call(row) row.clear mink = Infinity end end #/* correct off-axis rounding error */ if (position.call(row)) row.each {|v| v.dy+=h } else row.each {|v| v.dx+=w } end end end } stack.unshift(nil) root.visit_after {|nn,i| nn.depth = i nn.x = nn.y = nn.dx = nn.dy = 0 if nn.first_child nn.size=Rubyvis.sum(nn.child_nodes, lambda {|v| v.size}) else stack[0]=nn nn.size=that._size(stack[0]) end } stack.shift() #/* Sort. */ case s.order when 'ascending' root.sort(lambda {|a,b| a.size<=>b.size}) when 'descending' root.sort(lambda {|a,b| b.size<=>a.size}) when 'reverse' root.reverse end # /* Recursively compute the layout. */ root.x = 0; root.y = 0; root.dx = s.width root.dy = s.height root.visit_before {|n,i| layout.call(n,i)} end |
#leaf ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/rubyvis/layout/treemap.rb', line 55 def leaf m=Rubyvis::Mark.new. mark_extend(self.node). fill_style(nil). stroke_style(nil). visible(lambda {|n| !n.first_child }) m.parent = self m end |
#link ⇒ Object
64 65 66 |
# File 'lib/rubyvis/layout/treemap.rb', line 64 def link nil end |
#padding(n) ⇒ Object
Alias for setting the left, right, top and bottom padding properties simultaneously.
156 157 158 |
# File 'lib/rubyvis/layout/treemap.rb', line 156 def padding(n) padding_left(n).padding_right(n).padding_top(n).padding_bottom(n) end |
#size(f) ⇒ Object
Specifies the sizing function. By default, the size function uses the node_value
attribute of nodes as a numeric value: <p>The sizing function is invoked for each leaf node in the tree, per the nodes
property. For example, if the tree data structure represents a file system, with files as leaf nodes, and each file has a bytes
attribute, you can specify a size function as:
<pre> .size(function(d) d.bytes)</pre>
176 177 178 179 |
# File 'lib/rubyvis/layout/treemap.rb', line 176 def size(f) @size=Rubyvis.functor(f) self end |