File: indent.rb

package info (click to toggle)
ruby-rubyvis 0.6.1%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,812 kB
  • sloc: ruby: 11,114; javascript: 25; makefile: 2
file content (71 lines) | stat: -rw-r--r-- 1,947 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
# = Indented Tree
# Indented trees are widely-used to represent file systems, among other applications. Although indented trees require much vertical space and do not easily facilitate multiscale inference, they do allow efficient interactive exploration of the tree to find a specific node.
# In addition, the indent layout allows rapid scanning of node labels, and multivariate data such as file size can be displayed adjacent to the hierarchy.
#
# Uses Protovis API
$:.unshift(File.dirname(__FILE__)+"/../../lib")
require 'rubyvis'


def get_files(path)
  h={}
  Dir.glob("#{path}/*").each {|e|
    next if File.expand_path(e)=~/pkg|web|vendor|doc|~/
    pa=File.expand_path(e) 
    if File.stat(pa).directory?
      h[File.basename(pa)]=get_files(pa)
    else
      h[File.basename(pa)]=File.stat(pa).size
    end
  }
  h
end

files=get_files(File.expand_path(File.dirname(__FILE__)+"/../../lib/"))


root = Rubyvis.dom(files)
    .root("rubyvis")
    .sort(lambda {|a,b| a.node_name<=>b.node_name})

#/* Recursively compute the package sizes. */
root.visit_after {|n,i| 
  if (n.first_child)
    n.node_value= Rubyvis.sum(n.child_nodes , lambda {|nn|  nn.node_value})
  end
}

def t(d)
  d.parent_node ? (t(d.parent_node) + "." + d.node_name) : d.node_name
end

vis = Rubyvis::Panel.new()
    .width(260)
    .height((root.nodes.size + 1)* 12)
    .margin(5)

layout = vis.add(pv.Layout.Indent)
.nodes(lambda {root.nodes})
    .depth(12)
    .breadth(12)

layout.link.add(pv.Line)

node = layout.node.add(pv.Panel)
.top(lambda {|n| n.y - 6})
    .height(12)
    .right(6)
    .strokeStyle(nil)
    
node.anchor("left").add(pv.Dot)
    .strokeStyle("#1f77b4")
    .fillStyle(lambda {|n| n.first_child ? "#aec7e8" : "#ff7f0e"})
    .title(lambda {|d| t(d)})
  .anchor("right").add(pv.Label)
  .text(lambda {|n| n.node_name})

node.anchor("right").add(pv.Label)
.text(lambda {|n| (n.node_value >> 10).to_s + "KB"})

vis.render()
puts vis.to_svg