Class: Treemap::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/treemap/node.rb

Overview

A generic tree node class which is used to represent the data to be treemap’ed. The Layout and Output classes expect an object of this type to perform the treemap calculations on.

Create a simple tree:

root = Treemap::Node.new
root.new_child(:size => 6)
root.new_child(:size => 6)
root.new_child(:size => 4)
root.new_child(:size => 3)
root.new_child(:size => 2)
root.new_child(:size => 2)
root.new_child(:size => 1)

Initialize values:

root = Treemap::Node.new(:label => "All", :size => 100, :color => "#FFCCFF")
child1 = Treemap::Node.new(:label => "Child 1", :size => 50)
child2 = Treemap::Node.new(:label => "Child 2", :size => 50)
root.add_child(child1)
root.add_child(child2)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Node

Create a new Node. You can initialize the node by passing in a hash with any of the following keys:

  • :size - The size that this node represents. For non-leaf nodes the size must be equal to the sum of the sizes of it’s children. If size is nil then the value will be calculated by recursing the children.

  • :label - The label for this node. Used when displaying. Defaults to “node”

  • :color - The background fill color in hex to render when drawing the square. If the value is a number a color will be calculated. An example string color would be: ##FFFFFF (white)

  • :id - A unique id to assign to this node. Default id will be generated if one is not provided.

  • :object - An object, that this node might contain.



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/treemap/node.rb', line 57

def initialize(opts = {})
    @size = opts[:size]
    @label = opts[:label]
    @color = opts[:color]
    @id = opts[:id]
    @object = opts[:object]
    @children = []

    if(@id.nil?)
        make_id
    end
end

Instance Attribute Details

#boundsObject

Returns the value of attribute bounds.



38
39
40
# File 'lib/treemap/node.rb', line 38

def bounds
  @bounds
end

#childrenObject (readonly)

Returns the value of attribute children.



39
40
41
# File 'lib/treemap/node.rb', line 39

def children
  @children
end

#colorObject

Returns the value of attribute color.



38
39
40
# File 'lib/treemap/node.rb', line 38

def color
  @color
end

#idObject

Returns the value of attribute id.



38
39
40
# File 'lib/treemap/node.rb', line 38

def id
  @id
end

#labelObject

Returns the value of attribute label.



38
39
40
# File 'lib/treemap/node.rb', line 38

def label
  @label
end

#objectObject

Returns the value of attribute object.



38
39
40
# File 'lib/treemap/node.rb', line 38

def object
  @object
end

#parentObject

Returns the value of attribute parent.



38
39
40
# File 'lib/treemap/node.rb', line 38

def parent
  @parent
end

#sizeObject

Returns the value of attribute size.



38
39
40
# File 'lib/treemap/node.rb', line 38

def size
  @size
end

Instance Method Details

#add_child(node) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/treemap/node.rb', line 78

def add_child(node)
    # XXX check to see that a node with the same label doesn't already exist.
    #     having 2 nodes with the same label at the same depth
    #     doesn't seem to make sense
    node.parent = self
    @children.push(node)
end

#depthObject

Returns the depth of the node. 0 for root.



73
74
75
76
# File 'lib/treemap/node.rb', line 73

def depth
    return 0 if parent.nil?
    1 + self.parent.depth
end

#findObject



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

def find
    @children.find { |c| yield(c) }
end

#font_size(base_size) ⇒ Object

Unscientific formula to calculate the font size depending on the node’s area



119
120
121
# File 'lib/treemap/node.rb', line 119

def font_size(base_size)
    (base_size * Math.sqrt(self.bounds.width * self.bounds.height) / 125).to_i
end

#leaf?Boolean

Returns:

  • (Boolean)


128
129
130
131
# File 'lib/treemap/node.rb', line 128

def leaf?
    return true if @children.nil?
    !(@children.size > 0)
end

#new_child(*args) ⇒ Object

Creates a new node and adds it as a child. See new method.



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

def new_child(*args)
    node = Treemap::Node.new(*args)
    self.add_child(node)
end

#to_sObject



98
99
100
101
102
103
104
105
106
# File 'lib/treemap/node.rb', line 98

def to_s
    str = "[:id => " + id + " :label => " + label
    str += " :size => " + size.to_s + " :color => " + color.to_s
    if(not(bounds.nil?))
        str += " :bounds => " + bounds.to_s
    end
    str += "]"
    str
end