Class: Rubyvis::Layout::Partition

Inherits:
Hierarchy show all
Includes:
NodeLink
Defined in:
lib/rubyvis/layout/partition.rb

Overview

Implemeents a hierarchical layout using the partition (or sunburst, icicle) algorithm. This layout provides both node-link and space-filling implementations of partition diagrams. In many ways it is similar to pv.Layout.Cluster, except that leaf nodes are positioned based on their distance from the root.

<p>The partition layout support dynamic sizing for leaf nodes, if a #size psuedo-property is specified. The default size function returns 1, causing all leaf nodes to be sized equally, and all internal nodes to be sized by the number of leaf nodes they have as descendants.

<p>The size function can be used in conjunction with the order property, which allows the nodes to the sorted by the computed size. Note: for sorting based on other data attributes, simply use the default null for the order property, and sort the nodes beforehand using the pv.Dom operator.

<p>For more details on how to use this layout, see Rubyvis::Layout::Hierarchy.

See Also:

  • pvpv.Layoutpv.Layout.Partitionpv.Layout.Partition.Fill

Direct Known Subclasses

Fill

Defined Under Namespace

Classes: Fill

Instance Attribute Summary

Attributes included from NodeLink

#_h, #_ir, #_or, #_orient, #_w

Attributes inherited from Network

#_id, #link, #node, #node_label

Attributes inherited from Panel

#_canvas, #children, #root

Attributes inherited from Mark

#_properties, #binds, #child_index, #parent, #proto, #root, #scale, #scene, #target

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NodeLink

#node_link_build_implied

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

#type, #width

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

#initializePartition

Constructs a new, empty partition layout. Layouts are not typically constructed directly; instead, they are added to an existing panel via pv.Mark#add.



37
38
39
# File 'lib/rubyvis/layout/partition.rb', line 37

def initialize
  super
end

Class Method Details

.defaultsObject

Default properties for partition layouts. The default orientation is “top”.



92
93
94
95
# File 'lib/rubyvis/layout/partition.rb', line 92

def self.defaults
  Rubyvis::Layout::Partition.new.mark_extend(Rubyvis::Layout::Hierarchy.defaults).
    orient("top")
end

Instance Method Details

#_size(f) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/rubyvis/layout/partition.rb', line 96

def _size(f)
  if @_size.nil?
    1
  else
    @_size.call(f)
  end
end

#build_implied(s) ⇒ Object



125
126
127
# File 'lib/rubyvis/layout/partition.rb', line 125

def build_implied(s)
  partition_build_implied(s)
end

#orderObject

:attr: outer_radius

The outer radius; defaults to fill the containing panel, based on the height and width of the layout. If the layout has no height and width specified, it will extend to fill the enclosing panel.



89
# File 'lib/rubyvis/layout/partition.rb', line 89

attr_accessor_dsl :order, :orient , :inner_radius, :outer_radius

#partition_build_implied(s) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/rubyvis/layout/partition.rb', line 128

def partition_build_implied(s)
  
  return false if hierarchy_build_implied(s)
  that = self
  root = s.nodes[0]

  stack = Rubyvis::Mark.stack
  max_depth = 0

  # Recursively compute the tree depth and node size. #/
  stack.unshift(nil)
  root.visit_after {|n,i| 
    max_depth=i if i>max_depth
    if n.first_child
      n.size = Rubyvis.sum(n.child_nodes, lambda {|v| v.size})
    else
      stack[0]=n
      n.size=that._size(stack[0])
    end
    }
    
  stack.shift
  # # Order #/
  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})
  end
  # Compute the unit breadth and depth of each node. #/
  ds = 1 / max_depth.to_f
  root.min_breadth = 0
  root.breadth = 0.5
  root.max_breadth = 1
  root.visit_before {|n,i| 
    b = n.min_breadth
    ss = n.max_breadth - b
    c = n.first_child
    while(c) do
      c.min_breadth=b
      b+=(c.size/n.size.to_f)*ss
      
      c.max_breadth=b
      c.breadth=(b+c.min_breadth) / 2.0
      c=c.next_sibling
      
    end
  }
  root.visit_after {|n,i|
    n.min_depth=(i-1)*ds
    n.max_depth=n.depth=i*ds
  }
  
  node_link_build_implied(s)
  false
end

#size(f = nil, &block) ⇒ Object

Specifies the sizing function. By default, a sizing function is disabled and all nodes are given constant size. The sizing function is invoked for each leaf node in the tree (passed to the constructor).

<p>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>

As with other properties, a size function may specify additional arguments to access the data associated with the layout and any enclosing panels.

Parameters:

  • f (function) (defaults to: nil)

    the new sizing function.



119
120
121
122
123
124
# File 'lib/rubyvis/layout/partition.rb', line 119

def size(f=nil,&block)
  f=block if f.nil?
  raise "You should pass a proc" if f.nil?
  @_size=f
  self
end