Implements the TM class and other derived classes.
A Treemap is an information visualization technique, proven very useful when displaying large hierarchical structures on a constrained space. The idea behind a Treemap is to describe hierarchical relations as ‘containment’. That means that if node B is child of node A, then B ‘is contained’ in A.
Squarified Treemaps (Mark Bruls, Kees Huizing, and Jarke J. van Wijk)
http://www.win.tue.nl/~vanwijk/stm.pdf
Tree visualization with tree-maps: 2-d space-filling approach (Ben Shneiderman)
http://hcil.cs.umd.edu/trs/91-03/91-03.html
This visualization was built from scratch, taking only these papers as inspiration, and only shares some features with the Treemap papers mentioned above.
| Treemap.js | Implements the TM class and other derived classes. |
| TM | Abstract Treemap object. |
| Functions | |
| each | Traverses head and leaf nodes applying a given function |
| createBox | Constructs the proper DOM layout from a json node. |
| plot | Renders the Treemap. |
| headBox | Creates the head div dom element that usually contains the name of a parent JSON tree node. |
| bodyBox | Creates the body div dom element that usually contains a subtree dom element layout. |
| contentBox | Creates the content div dom element that usually contains a leaf div dom element or head and body div dom elements. |
| leafBox | Creates the leaf div dom element that usually contains nothing else. |
| setColor | Calculates an hexa color string based on the $color data node property. |
| enter | Sets the elem parameter as root and performs the layout. |
| onLeftClick | Sets the elem parameter as root and performs the layout. |
| out | Sets the parent node of the currently shown subtree as root and performs the layout. |
| onRightClick | Sets the parent node of the currently shown subtree as root and performs the layout. |
| view | Sets the root of the treemap to the specified node id and performs the layout. |
| resetPath | Sets an ‘in-path’ className for leaf and head elements which belong to the path between the given tree node and the visualization’s root node. |
| initializeElements | Traverses the DOM tree applying the onCreateElement method. |
| destroyElements | Traverses the tree applying the onDestroyElement method. |
| empty | Empties the Treemap container (trying also to garbage collect things). |
| loadTree | Loads the subtree specified by id and plots it on the layout container. |
| TM. | A JavaScript implementation of the Slice and Dice Treemap algorithm. |
| Functions | |
| loadJSON | Loads the specified JSON tree and lays it on the main container. |
| compute | Called by loadJSON to calculate recursively all node positions and lay out the tree. |
| TM.Area | Abstract Treemap class containing methods that are common to aspect ratio related algorithms such as TM.Squarified and TM.Strip. |
| Functions | |
| loadJSON | Loads the specified JSON tree and lays it on the main container. |
| computeDim | Computes dimensions and positions of a group of nodes according to a custom layout row condition. |
| worstAspectRatio | Calculates the worst aspect ratio of a group of rectangles. |
| avgAspectRatio | Calculates the average aspect ratio of a group of rectangles. |
| TM. | A JavaScript implementation of the Squarified Treemap algorithm. |
| Functions | |
| compute | Called by loadJSON to calculate recursively all node positions and lay out the tree. |
| processChildrenLayout | Computes children real areas and other useful parameters for performing the Squarified algorithm. |
| squarify | Performs an heuristic method to calculate div elements sizes in order to have a good aspect ratio. |
| layoutRow | Performs the layout of an array of nodes. |
| TM. | A JavaScript implementation of the Strip Treemap algorithm. |
| Functions | |
| compute | Called by loadJSON to calculate recursively all node positions and lay out the tree. |
| processChildrenLayout | Computes children real areas and other useful parameters for performing the Strip algorithm. |
| stripify | Performs an heuristic method to calculate div elements sizes in order to have a good compromise between aspect ratio and order. |
| layoutRow | Performs the layout of an array of nodes. |
Abstract Treemap object.
TM.Squarified, TM.Strip and TM.SliceAndDice.
Implements layout and configuration options inherited by TM.Squarified, TM.Strip and TM.SliceAndDice.
All Treemap constructors take the same configuration object as parameter.
Two special data keys are read from the JSON tree structure loaded by Loader.loadJSON to calculate node’s color and dimensions. These properties are $area (for nodes dimensions) and $color. Both of these properties are floats.
This means that the tree structure defined in Loader.loadJSON should now look more like this
var json = {
"id": "aUniqueIdentifier",
"name": "usually a nodes name",
"data": {
"$area": 33, //some float value
"$color": 36, //-optional- some float value
"some key": "some value",
"some other key": "some other value"
},
"children": [ 'other nodes or empty' ]
};If you want to know more about JSON tree structures and the data property please read Loader.loadJSON.
General
Nodes
There are two kinds of Treemap nodes.

Inner nodes are nodes having children, like Pearl Jam.
These nodes are represented by three div elements. A content element, a head element (where the title goes) and a body element, where the children are laid out.
<div class="content"> <div class="head">Pearl Jam</div> <div class="body">...other nodes here...</div> </div>
Leaves are optionally colored nodes laying at the “bottom” of the tree. For example, Yield, Vs. and Riot Act are leaves.
These nodes are represented by two div elements. A content element and a wrapped leaf element
<div class="content"> <div class="leaf">Yield</div> </div>
There are some configuration properties regarding Treemap nodes
Color
Color is an object containing as properties
Tips
Tips is an object containing as properties
| tooltip | The tooltip div element. |
| node | The corresponding JSON tree node (See also <Loader.loadJSON>). |
| isLeaf | Whether is a leaf or inner node. |
| domElement | The current hovered DOM element. |
Controller options
You can also implement controller functions inside the configuration object. These functions are
| content | The div wrapper element with content className. |
| node | The corresponding JSON tree node (See also <Loader.loadJSON>). |
| isLeaf | Whether is a leaf or inner node. If the node’s an inner tree node, elem1 and elem2 will become the head and body div elements respectively. If the node’s a leaf, then elem1 will become the div leaf element. |
See also TM.Squarified, TM.SliceAndDice and TM.Strip.
| Functions | |
| each | Traverses head and leaf nodes applying a given function |
| createBox | Constructs the proper DOM layout from a json node. |
| plot | Renders the Treemap. |
| headBox | Creates the head div dom element that usually contains the name of a parent JSON tree node. |
| bodyBox | Creates the body div dom element that usually contains a subtree dom element layout. |
| contentBox | Creates the content div dom element that usually contains a leaf div dom element or head and body div dom elements. |
| leafBox | Creates the leaf div dom element that usually contains nothing else. |
| setColor | Calculates an hexa color string based on the $color data node property. |
| enter | Sets the elem parameter as root and performs the layout. |
| onLeftClick | Sets the elem parameter as root and performs the layout. |
| out | Sets the parent node of the currently shown subtree as root and performs the layout. |
| onRightClick | Sets the parent node of the currently shown subtree as root and performs the layout. |
| view | Sets the root of the treemap to the specified node id and performs the layout. |
| resetPath | Sets an ‘in-path’ className for leaf and head elements which belong to the path between the given tree node and the visualization’s root node. |
| initializeElements | Traverses the DOM tree applying the onCreateElement method. |
| destroyElements | Traverses the tree applying the onDestroyElement method. |
| empty | Empties the Treemap container (trying also to garbage collect things). |
| loadTree | Loads the subtree specified by id and plots it on the layout container. |
each: function( f )
Traverses head and leaf nodes applying a given function
| f | A function that takes as parameters the same as the onCreateElement and onDestroyElement methods described in TM. |
createBox: function( json, coord, html )
Constructs the proper DOM layout from a json node.
If the node’s an inner node, this method calls TM.contentBox, TM.bodyBox and TM.leafBox to create the following HTML structure
<div class="content"> <div class="head">[Node name]</div> <div class="body">[Node's children]</div> </div>
If the node’s a leaf node, it creates the following structure by calling TM.contentBox, TM.leafBox
<div class="content"> <div class="leaf">[Node name]</div> </div>
| json | A JSON subtree. See also Loader.loadJSON. |
| coord | A coordinates object specifying width, height, left and top style properties. |
| html | html to inject into the body element if the node is an inner Tree node. |
The HTML structure described above.
headBox: function( json, coord )
Creates the head div dom element that usually contains the name of a parent JSON tree node.
| json | A JSON subtree. See also Loader.loadJSON. |
| coord | width and height base coordinate object. |
A new head div dom element that has head as class name.
contentBox: function( json, coord, html )
Creates the content div dom element that usually contains a leaf div dom element or head and body div dom elements.
| json | A JSON node. See also Loader.loadJSON. |
| coord | An object containing width, height, left and top coordinates. |
| html | input html wrapped by this tag. |
A new content div dom element that has content as class name.
leafBox: function( json, coord )
Creates the leaf div dom element that usually contains nothing else.
| json | A JSON subtree. See also Loader.loadJSON. |
| coord | base with and height coordinate object. |
A new leaf div dom element having leaf as class name.
setColor: function( json )
Calculates an hexa color string based on the $color data node property.
This method is called by TM.leafBox to assign an hexadecimal color to each leaf node.
This color is calculated by making a linear interpolation between $color max and min values and RGB max and min values so that
hex = (maxColorValue - minColorValue) / (maxValue - minValue) * (x - minValue) + minColorValue
where x range is [minValue, maxValue] and
are defined in the TM configuration object.
This method is called by TM.leafBox iif Color.allow is setted to true.
Sometimes linear interpolation for coloring is just not enough. In that case you can re-implement this method so that it fits your coloring needs.
Some people might find useful to implement their own coloring interpolation method and to assign the resulting hex string to the $color property. In that case we could re-implement the TM.setColor method like this
//TM.Strip, TM.SliceAndDice also work
TM.Squarified.implement({
'setColor': function(json) {
return json.data.$color;
}
});So that it returns the previously assigned hex string.
| json | A JSON tree node. |
A String that represents a color in hex value.
enter: function( elem )
Sets the elem parameter as root and performs the layout.
| elem | A JSON Tree node. See also Loader.loadJSON. |
onLeftClick: function( elem )
Sets the elem parameter as root and performs the layout. This method is called when addLeftClickHandler is true and a node is left-clicked. You can override this method to add some custom behavior when the node is left clicked though.
An Example for overriding this method could be
//TM.Strip or TM.SliceAndDice also work
TM.Squarified.implement({
'onLeftClick': function(elem) {
//some custom code...
}
});| elem | A JSON Tree node. See also Loader.loadJSON. |
onRightClick: function()
Sets the parent node of the currently shown subtree as root and performs the layout. This method is called when addRightClickHandler is true and a node is right-clicked. You can override this method to add some custom behavior when the node is right-clicked though.
An Example for overriding this method could be
//TM.Strip or TM.SliceAndDice also work
TM.Squarified.implement({
'onRightClick': function() {
//some custom code...
}
});
resetPath: function( tree )
Sets an ‘in-path’ className for leaf and head elements which belong to the path between the given tree node and the visualization’s root node.
| tree | A JSON tree node. See also Loader.loadJSON. |
A JavaScript implementation of the Slice and Dice Treemap algorithm.
The TM.SliceAndDice constructor takes an optional configuration object described in TM.
This visualization (as all other Treemap visualizations) is fed with JSON Tree structures.
The $area node data key is required for calculating rectangles dimensions.
The $color node data key is required if Color allow is true and is used for calculating leaves colors.
| config | Configuration defined in TM. |
Here’s a way of instanciating the TM.SliceAndDice will all its optional configuration features
var tm = new TM.SliceAndDice({
orientation: "h",
titleHeight: 13,
rootId: 'infovis',
offset:4,
levelsToShow: 3,
addLeftClickHandler: false,
addRightClickHandler: false,
selectPathOnHover: false,
Color: {
allow: false,
minValue: -100,
maxValue: 100,
minColorValue: [255, 0, 50],
maxColorValue: [0, 255, 50]
},
Tips: {
allow: false,
offsetX; 20,
offsetY: 20,
onShow: function(tooltip, node, isLeaf, domElement) {}
},
onBeforeCompute: function(node) {
//Some stuff on before compute...
},
onAfterCompute: function() {
//Some stuff on after compute...
},
onCreateElement: function(content, node, isLeaf, head, body) {
//Some stuff onCreateElement
},
onDestroyElement: function(content, node, isLeaf, head, body) {
//Some stuff onDestroyElement
},
request: false
});
tm.loadJSON(json);
loadJSON: function ( json )
Loads the specified JSON tree and lays it on the main container.
| json | A JSON Tree. See also Loader.loadJSON. |
compute: function( par, json, orientation )
Called by loadJSON to calculate recursively all node positions and lay out the tree.
| par | The parent node of the json subtree. |
| json | A JSON subtree. See also Loader.loadJSON. |
| orientation | The current orientation. This value is switched recursively. |
Abstract Treemap class containing methods that are common to aspect ratio related algorithms such as TM.Squarified and TM.Strip.
| Functions | |
| loadJSON | Loads the specified JSON tree and lays it on the main container. |
| computeDim | Computes dimensions and positions of a group of nodes according to a custom layout row condition. |
| worstAspectRatio | Calculates the worst aspect ratio of a group of rectangles. |
| avgAspectRatio | Calculates the average aspect ratio of a group of rectangles. |
loadJSON: function ( json )
Loads the specified JSON tree and lays it on the main container.
| json | A JSON tree. See also Loader.loadJSON. |
computeDim: function( tail, initElem, w, coord, comp )
Computes dimensions and positions of a group of nodes according to a custom layout row condition.
| tail | An array of nodes. |
| initElem | An array of nodes (containing the initial node to be laid). |
| w | A fixed dimension where nodes will be layed out. |
| coord | A coordinates object specifying width, height, left and top style properties. |
| comp | A custom comparison function |
worstAspectRatio: function( ch, w )
Calculates the worst aspect ratio of a group of rectangles.
http://en.wikipedia.org/wiki/Aspect_ratio
| ch | An array of nodes. |
| w | The fixed dimension where rectangles are being laid out. |
The worst aspect ratio.
avgAspectRatio: function( ch, w )
Calculates the average aspect ratio of a group of rectangles.
http://en.wikipedia.org/wiki/Aspect_ratio
| ch | An array of nodes. |
| w | The fixed dimension where rectangles are being laid out. |
The average aspect ratio.
A JavaScript implementation of the Squarified Treemap algorithm.
The TM.Squarified constructor takes an optional configuration object described in TM.
This visualization (as all other Treemap visualizations) is fed with JSON Tree structures.
The $area node data key is required for calculating rectangles dimensions.
The $color node data key is required if Color allow is true and is used for calculating leaves colors.
| config | Configuration defined in TM. |
Here’s a way of instanciating the TM.Squarified will all its optional configuration features
var tm = new TM.Squarified({
titleHeight: 13,
rootId: 'infovis',
offset:4,
levelsToShow: 3,
addLeftClickHandler: false,
addRightClickHandler: false,
selectPathOnHover: false,
Color: {
allow: false,
minValue: -100,
maxValue: 100,
minColorValue: [255, 0, 50],
maxColorValue: [0, 255, 50]
},
Tips: {
allow: false,
offsetX: 20,
offsetY: 20,
onShow: function(tooltip, node, isLeaf, domElement) {}
},
onBeforeCompute: function(node) {
//Some stuff on before compute...
},
onAfterCompute: function() {
//Some stuff on after compute...
},
onCreateElement: function(content, node, isLeaf, head, body) {
//Some stuff onCreateElement
},
onDestroyElement: function(content, node, isLeaf, head, body) {
//Some stuff onDestroyElement
},
request: false
});
tm.loadJSON(json);| Functions | |
| compute | Called by loadJSON to calculate recursively all node positions and lay out the tree. |
| processChildrenLayout | Computes children real areas and other useful parameters for performing the Squarified algorithm. |
| squarify | Performs an heuristic method to calculate div elements sizes in order to have a good aspect ratio. |
| layoutRow | Performs the layout of an array of nodes. |
compute: function( json, coord )
Called by loadJSON to calculate recursively all node positions and lay out the tree.
| json | A JSON tree. See also Loader.loadJSON. |
| coord | A coordinates object specifying width, height, left and top style properties. |
processChildrenLayout: function( par, ch, coord )
Computes children real areas and other useful parameters for performing the Squarified algorithm.
| par | The parent node of the json subtree. |
| ch | An Array of nodes |
| coord | A coordinates object specifying width, height, left and top style properties. |
squarify: function( tail, initElem, w, coord )
Performs an heuristic method to calculate div elements sizes in order to have a good aspect ratio.
| tail | An array of nodes. |
| initElem | An array of nodes, containing the initial node to be laid out. |
| w | A fixed dimension where nodes will be laid out. |
| coord | A coordinates object specifying width, height, left and top style properties. |
A JavaScript implementation of the Strip Treemap algorithm.
The TM.Strip constructor takes an optional configuration object described in TM.
This visualization (as all other Treemap visualizations) is fed with JSON Tree structures.
The $area node data key is required for calculating rectangles dimensions.
The $color node data key is required if Color allow is true and is used for calculating leaves colors.
| config | Configuration defined in TM. |
Here’s a way of instanciating the TM.Strip will all its optional configuration features
var tm = new TM.Strip({
titleHeight: 13,
orientation: "h",
rootId: 'infovis',
offset:4,
levelsToShow: 3,
addLeftClickHandler: false,
addRightClickHandler: false,
selectPathOnHover: false,
Color: {
allow: false,
minValue: -100,
maxValue: 100,
minColorValue: [255, 0, 50],
maxColorValue: [0, 255, 50]
},
Tips: {
allow: false,
offsetX: 20,
offsetY: 20,
onShow: function(tooltip, node, isLeaf, domElement) {}
},
onBeforeCompute: function(node) {
//Some stuff on before compute...
},
onAfterCompute: function() {
//Some stuff on after compute...
},
onCreateElement: function(content, node, isLeaf, head, body) {
//Some stuff onCreateElement
},
onDestroyElement: function(content, node, isLeaf, head, body) {
//Some stuff onDestroyElement
},
request: false
});
tm.loadJSON(json);| Functions | |
| compute | Called by loadJSON to calculate recursively all node positions and lay out the tree. |
| processChildrenLayout | Computes children real areas and other useful parameters for performing the Strip algorithm. |
| stripify | Performs an heuristic method to calculate div elements sizes in order to have a good compromise between aspect ratio and order. |
| layoutRow | Performs the layout of an array of nodes. |
compute: function( json, coord )
Called by loadJSON to calculate recursively all node positions and lay out the tree.
| json | A JSON subtree. See also Loader.loadJSON. |
| coord | A coordinates object specifying width, height, left and top style properties. |
processChildrenLayout: function( par, ch, coord )
Computes children real areas and other useful parameters for performing the Strip algorithm.
| par | The parent node of the json subtree. |
| ch | An Array of nodes |
| coord | A coordinates object specifying width, height, left and top style properties. |
stripify: function( tail, initElem, w, coord )
Performs an heuristic method to calculate div elements sizes in order to have a good compromise between aspect ratio and order.
| tail | An array of nodes. |
| initElem | An array of nodes. |
| w | A fixed dimension where nodes will be layed out. |
| coord | A coordinates object specifying width, height, left and top style properties. |
Traverses head and leaf nodes applying a given function
each: function( f )
Constructs the proper DOM layout from a json node.
createBox: function( json, coord, html )
Renders the Treemap.
plot: function( json )
Creates the head div dom element that usually contains the name of a parent JSON tree node.
headBox: function( json, coord )
Creates the body div dom element that usually contains a subtree dom element layout.
bodyBox: function( html, coord )
Creates the content div dom element that usually contains a leaf div dom element or head and body div dom elements.
contentBox: function( json, coord, html )
Creates the leaf div dom element that usually contains nothing else.
leafBox: function( json, coord )
Calculates an hexa color string based on the $color data node property.
setColor: function( json )
Sets the elem parameter as root and performs the layout.
enter: function( elem )
Sets the elem parameter as root and performs the layout.
onLeftClick: function( elem )
Sets the parent node of the currently shown subtree as root and performs the layout.
out: function()
Sets the parent node of the currently shown subtree as root and performs the layout.
onRightClick: function()
Sets the root of the treemap to the specified node id and performs the layout.
view: function( id )
Sets an ‘in-path’ className for leaf and head elements which belong to the path between the given tree node and the visualization’s root node.
resetPath: function( tree )
Traverses the DOM tree applying the onCreateElement method.
initializeElements: function()
Traverses the tree applying the onDestroyElement method.
destroyElements: function()
Empties the Treemap container (trying also to garbage collect things).
empty: function()
Loads the subtree specified by id and plots it on the layout container.
loadTree: function( id )
Loads the specified JSON tree and lays it on the main container.
loadJSON: function ( json )
Called by loadJSON to calculate recursively all node positions and lay out the tree.
compute: function( par, json, orientation )
Loads the specified JSON tree and lays it on the main container.
loadJSON: function ( json )
Computes dimensions and positions of a group of nodes according to a custom layout row condition.
computeDim: function( tail, initElem, w, coord, comp )
Calculates the worst aspect ratio of a group of rectangles.
worstAspectRatio: function( ch, w )
Calculates the average aspect ratio of a group of rectangles.
avgAspectRatio: function( ch, w )
Called by loadJSON to calculate recursively all node positions and lay out the tree.
compute: function( json, coord )
Computes children real areas and other useful parameters for performing the Squarified algorithm.
processChildrenLayout: function( par, ch, coord )
Performs an heuristic method to calculate div elements sizes in order to have a good aspect ratio.
squarify: function( tail, initElem, w, coord )
Performs the layout of an array of nodes.
layoutRow: function( ch, w, coord )
Called by loadJSON to calculate recursively all node positions and lay out the tree.
compute: function( json, coord )
Computes children real areas and other useful parameters for performing the Strip algorithm.
processChildrenLayout: function( par, ch, coord )
Performs an heuristic method to calculate div elements sizes in order to have a good compromise between aspect ratio and order.
stripify: function( tail, initElem, w, coord )
Performs the layout of an array of nodes.
layoutRow: function( ch, w, coord )
Loads a JSON structure to the visualization.
loadJSON: function( json, i )