Class: Rubyvis::Layout::Pack

Inherits:
Hierarchy show all
Defined in:
lib/rubyvis/layout/pack.rb

Overview

Implements a hierarchical layout using circle-packing. The meaning of the exported mark prototypes changes slightly in the space-filling implementation:<ul>

<li>node - for rendering nodes; typically a Rubyvis::Dot.

<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>The pack 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.

>“Visualization of large hierarchical data by circle packing”</a> by W. Wang,

  1. Wang, G. Dai, and H. Wang, ACM CHI 2006.

/

See Also:

  • href="http://portal.acm.org/citation.cfm?id=1124772.1124851"

Instance Attribute Summary

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

#initializePack

Returns a new instance of Pack.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rubyvis/layout/pack.rb', line 41

def initialize
  super
  @node.
  shape_radius(lambda {|n| n.radius }).
  stroke_style("rgb(31, 119, 180)").
  fill_style("rgba(31, 119, 180, 0.25)")


  @node_label.text_align("center")

  @link=nil

  @radius = lambda { 1 }
end

Class Method Details

.defaultsObject

Default properties for circle-packing layouts. The default spacing parameter is 1 and the default order is “ascending”.



84
85
86
87
88
# File 'lib/rubyvis/layout/pack.rb', line 84

def self.defaults
  Rubyvis::Layout::Pack.new.mark_extend(Rubyvis::Layout::Hierarchy.defaults).
  spacing(1).
  order("ascending")
end

Instance Method Details

#bound(n) ⇒ Object



154
155
156
157
158
159
# File 'lib/rubyvis/layout/pack.rb', line 154

def bound(n)
  @x_min = [n.x - n.radius, @x_min].min
  @x_max = [n.x + n.radius, @x_max].max
  @y_min = [n.y - n.radius, @y_min].min
  @y_max = [n.y + n.radius, @y_max].max
end

#build_implied(s) ⇒ Object



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/rubyvis/layout/pack.rb', line 313

def build_implied(s)
  return nil if hierarchy_build_implied(s)
  @s=s
  nodes = s.nodes
  root = nodes[0]
  radii(nodes)

  # Recursively compute the layout. #/
  root.x = 0
  root.y = 0
  root.radius = pack_tree(root)

  w = self.width
  h = self.height
  k = 1.0 / [2.0 * root.radius / w, 2.0 * root.radius / h].max
  transform(root, w / 2.0, h / 2.0, k)
end

#insert(a, b) ⇒ Object



160
161
162
163
164
165
166
# File 'lib/rubyvis/layout/pack.rb', line 160

def insert(a,b)
  c = a.n
  a.n = b
  b._p = a
  b.n = c
  c._p = b
end

#intersects(a, b) ⇒ Object



171
172
173
174
175
176
# File 'lib/rubyvis/layout/pack.rb', line 171

def intersects(a, b)
  dx = b.x - a.x
  dy = b.y - a.y
  dr = a.radius + b.radius
  (dr * dr - dx * dx - dy * dy) > 0.001 # within epsilon
end

#pack_circle(nodes) ⇒ Object



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
# File 'lib/rubyvis/layout/pack.rb', line 211

def pack_circle(nodes)
  @x_min = Infinity
  @x_max = -Infinity
  @y_min = Infinity
  @y_max = -Infinity
  a=b=c=j=k=nil


  # Create first node.
  a = nodes[0];
  a.x = -a.radius
  a.y = 0
  bound(a)

  # Create second node. #/
  if (nodes.size > 1)
    b = nodes[1]
    b.x = b.radius
    b.y = 0
    bound(b)

    # Create third node and build chain.
    if (nodes.size > 2)
      c = nodes[2]
      place(a, b, c)
      bound(c)
      insert(a, c)
      a._p = c
      insert(c, b)
      b = a.n

      # Now iterate through the rest.
      i=3
      while(i < nodes.size) do
        c=nodes[i]
        place(a, b, c)

        # Search for the closest intersection. #/
        isect = 0
        s1 = 1
        s2 = 1

        j=b.n
        while(j!=b) do
          if (intersects(j, c))
            isect=1
            break
          end
          j=j.n
          s1+=1
        end

        if isect==1
          k=a._p
          while(k!=j._p) do
            if(intersects(k,c))
              if(s2 < s1)
                isect=-1
                j=k
              end
              break
            end
            k=k._p
            s2+=1
          end
        end


        # Update node chain. #/
        if (isect == 0)
          insert(a, c)
          b = c
          bound(c)
        elsif (isect > 0)
          splice(a, j)
          b = j
          i-=1
        elsif (isect < 0)
          splice(j, b)
          a = j
          i-=1
        end
        i+=1
      end


    end
  end

  # Re-center the circles and return the encompassing radius. #/
  cx = (@x_min + @x_max) / 2.0
  cy = (@y_min + @y_max) / 2.0
  cr = 0
  nodes.each do |n|
    n.x -= cx
    n.y -= cy
    cr = [cr, n.radius + Math.sqrt(n.x * n.x + n.y * n.y)].max
  end
  cr + @s.spacing
end

#pack_tree(n) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/rubyvis/layout/pack.rb', line 132

def pack_tree(n)
  nodes = []
  c=n.first_child
  while(c)
    c.radius=pack_tree(c) if c.first_child
    c.n=c._p=c
    nodes.push(c)
    c=c.next_sibling
  end

  # Sort.
  case @s.order
  when "ascending"
    nodes.sort {|a,b| a.radius<=>b.radius}
  when 'descending'
    nodes.sort {|a,b| b.radius<=>a.radius}
  when 'reverse'
    nodes.reverse
  end

  return pack_circle(nodes)
end

#place(a, b, c) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/rubyvis/layout/pack.rb', line 179

def place(a, b, c)
  da = b.radius + c.radius
  db = a.radius + c.radius
  dx = b.x - a.x
  dy = b.y - a.y
  dc = Math.sqrt(dx * dx + dy * dy)
  cos = (db * db + dc * dc - da * da) / (2.0 * db * dc)
  theta = Math.acos(cos)
  x = cos * db
  h = Math.sin(theta) * db
  dx = dx/dc
  dy = dy/dc
  c.x = a.x + x * dx + h * dy
  c.y = a.y + x * dy - h * dx
end

#radii(nodes) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/rubyvis/layout/pack.rb', line 121

def radii(nodes)
  stack=Mark.stack
  stack.unshift(nil)
  nodes.each {|c|
    if !c.first_child
      stack[0]=c
      c.radius = @radius.js_apply(self, stack)
    end
  }
  stack.shift
end

#size(f) ⇒ 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)

    the new sizing function.



110
111
112
113
114
115
116
117
118
# File 'lib/rubyvis/layout/pack.rb', line 110

def size(f)
  if f.is_a? Proc
    @radius=lambda {|*args| Math.sqrt(f.js_apply(self,args))}
  else
    f=Math.sqrt(f)
    @radius=lambda {f}
  end
  self
end

#splice(a, b) ⇒ Object



167
168
169
170
# File 'lib/rubyvis/layout/pack.rb', line 167

def splice(a, b)
  a.n = b
  b._p = a
end

#transform(n, x, y, k) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/rubyvis/layout/pack.rb', line 196

def transform(n, x, y, k)
  c=n.first_child
  while(c) do
    c.x += n.x
    c.y += n.y
    transform(c, x, y, k)
    c=c.next_sibling
  end
  n.x = x + k * n.x
  n.y = y + k * n.y
  n.radius *= k
  n.mid_angle=0 # Undefined on protovis
end