Class: Treemap::Node
- Inherits:
-
Object
- Object
- Treemap::Node
- 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
-
#bounds ⇒ Object
Returns the value of attribute bounds.
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#color ⇒ Object
Returns the value of attribute color.
-
#id ⇒ Object
Returns the value of attribute id.
-
#label ⇒ Object
Returns the value of attribute label.
-
#object ⇒ Object
Returns the value of attribute object.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#size ⇒ Object
Returns the value of attribute size.
Instance Method Summary collapse
- #add_child(node) ⇒ Object
-
#depth ⇒ Object
Returns the depth of the node.
- #find ⇒ Object
-
#font_size(base_size) ⇒ Object
Unscientific formula to calculate the font size depending on the node’s area.
-
#initialize(opts = {}) ⇒ Node
constructor
Create a new Node.
- #leaf? ⇒ Boolean
-
#new_child(*args) ⇒ Object
Creates a new node and adds it as a child.
- #to_s ⇒ Object
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.
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/treemap/node.rb', line 59 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
#bounds ⇒ Object
Returns the value of attribute bounds.
40 41 42 |
# File 'lib/treemap/node.rb', line 40 def bounds @bounds end |
#children ⇒ Object (readonly)
Returns the value of attribute children.
41 42 43 |
# File 'lib/treemap/node.rb', line 41 def children @children end |
#color ⇒ Object
Returns the value of attribute color.
40 41 42 |
# File 'lib/treemap/node.rb', line 40 def color @color end |
#id ⇒ Object
Returns the value of attribute id.
40 41 42 |
# File 'lib/treemap/node.rb', line 40 def id @id end |
#label ⇒ Object
Returns the value of attribute label.
40 41 42 |
# File 'lib/treemap/node.rb', line 40 def label @label end |
#object ⇒ Object
Returns the value of attribute object.
40 41 42 |
# File 'lib/treemap/node.rb', line 40 def object @object end |
#parent ⇒ Object
Returns the value of attribute parent.
40 41 42 |
# File 'lib/treemap/node.rb', line 40 def parent @parent end |
#size ⇒ Object
Returns the value of attribute size.
40 41 42 |
# File 'lib/treemap/node.rb', line 40 def size @size end |
Instance Method Details
#add_child(node) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/treemap/node.rb', line 80 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 |
#depth ⇒ Object
Returns the depth of the node. 0 for root.
75 76 77 78 |
# File 'lib/treemap/node.rb', line 75 def depth return 0 if parent.nil? 1 + self.parent.depth end |
#find ⇒ Object
96 97 98 |
# File 'lib/treemap/node.rb', line 96 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
121 122 123 |
# File 'lib/treemap/node.rb', line 121 def font_size(base_size) (base_size * Math.sqrt(self.bounds.width * self.bounds.height) / 125).to_i end |
#leaf? ⇒ Boolean
130 131 132 133 |
# File 'lib/treemap/node.rb', line 130 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.
91 92 93 94 |
# File 'lib/treemap/node.rb', line 91 def new_child(*args) node = Treemap::Node.new(*args) self.add_child(node) end |
#to_s ⇒ Object
100 101 102 103 104 105 106 107 108 |
# File 'lib/treemap/node.rb', line 100 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 |