Class: Treemap::SliceLayout

Inherits:
LayoutBase show all
Defined in:
lib/treemap/slice_layout.rb

Direct Known Subclasses

SquarifiedLayout

Constant Summary collapse

Vertical =
1
Horizontal =
2

Instance Attribute Summary

Attributes inherited from LayoutBase

#color, #position

Instance Method Summary collapse

Methods inherited from LayoutBase

#initialize

Constructor Details

This class inherits a constructor from Treemap::LayoutBase

Instance Method Details

#axis(bounds) ⇒ Object



93
94
95
96
# File 'lib/treemap/slice_layout.rb', line 93

def axis(bounds)
    return Horizontal if horizontal?(bounds)
    Vertical
end

#flip(axis) ⇒ Object



80
81
82
83
# File 'lib/treemap/slice_layout.rb', line 80

def flip(axis)
    return Horizontal if axis == Vertical
    Vertical
end

#horizontal?(bounds) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/treemap/slice_layout.rb', line 89

def horizontal?(bounds)
    bounds.width < bounds.height
end

#process(node, bounds, axis = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/treemap/slice_layout.rb', line 19

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?)
        process_children(node.children, bounds, axis)
    end
end

#process_children(children, bounds, axis = nil) ⇒ Object



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
# File 'lib/treemap/slice_layout.rb', line 44

def process_children(children, bounds, axis=nil)
    parent_bounds = bounds.clone
    bounds = bounds.clone

    axis = axis(bounds) if(axis.nil?)

    width = axis == Vertical ? bounds.width : bounds.height

    sum = sum(children)

    # XXX should we sort? seems to produce better map but not tested on 
    # larger data sets
    # children.sort {|c1,c2| c2.size <=> c1.size}.each do |c|
    children.each do |c|
        size = ((c.size.to_f / sum.to_f)*width).round

        if(axis == Vertical) 
            bounds.x2 = bounds.x1 + size
        else
            bounds.y2 = bounds.y1 + size
        end

        process(c, bounds, flip(axis))

        axis == Vertical ? bounds.x1 = bounds.x2 : bounds.y1 = bounds.y2
    end

    last = children.last
    if(axis == Vertical)
        last.bounds.x2 = parent_bounds.x2
    else
       last.bounds.y2 = parent_bounds.y2
    end

end

#sum(children) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/treemap/slice_layout.rb', line 36

def sum(children)
    sum = 0
    children.each do |c|
        sum += c.size
    end
    sum
end

#vertical?(bounds) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/treemap/slice_layout.rb', line 85

def vertical?(bounds)
    bounds.width > bounds.height
end