<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Overfloater &#187; 1.0</title>
	<atom:link href="http://blog.thejit.org/category/10/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.thejit.org</link>
	<description>Data Visualization, JavaScript and Computer Science related stuff</description>
	<lastBuildDate>Sun, 18 Jul 2010 13:38:10 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Hypertree quick tutorial</title>
		<link>http://blog.thejit.org/2008/05/07/hypertree-quick-tutorial/</link>
		<comments>http://blog.thejit.org/2008/05/07/hypertree-quick-tutorial/#comments</comments>
		<pubDate>Wed, 07 May 2008 18:37:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[1.0]]></category>
		<category><![CDATA[JavaScript information visualization toolkit (JIT)]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[hyperbolic tree]]></category>
		<category><![CDATA[information visualization]]></category>

		<guid isPermaLink="false">http://blogngb.woot.com.ar/?p=9</guid>
		<description><![CDATA[This tutorial requires you to have read Feeding JSON tree structures to the JIT and on controllers first.
Note: I&#8217;ll be using Mootools for this tutorial. However, this visualization can be used without the Mootools library. You can find a Mootools build in the extras folder of the library.
Hi, this is going to be a quick [...]]]></description>
			<content:encoded><![CDATA[<p><em>This tutorial requires you to have read <a href="http://blog.thejit.org/?p=7">Feeding JSON tree structures to the JIT</a> and <a href="http://blog.thejit.org/?p=8">on controllers</a> first</em>.</p>
<p><b>Note:</b> I&#8217;ll be using Mootools for this tutorial. However, this visualization can be used without the Mootools library. You can find a Mootools build in the <em>extras folder</em> of the library.</p>
<p>Hi, this is going to be a quick tutorial on how to set the hyperbolic tree up and running.</p>
<p>We are going to work with this tree JSON structure:</p>
<div style="height:300px;overflow:scroll;">
<pre name="code" class="js:nogutter">var json = {"id":"node02",
"name":"0.2",
"data":[
{"key":"key1",
"value":9},
{"key":"key2",
"value":71}],
"children":[
{"id":"node13",
"name":"1.3",
"data":[
{"key":"key1",
"value":6},
{"key":"key2",
"value":-9}],
"children":[
{"id":"node24",
"name":"2.4",
"data":[
{"key":"key1",
"value":3},
{"key":"key2",
"value":-42}],
"children":[]},
{"id":"node25",
"name":"2.5",
"data":[
{"key":"key1",
"value":2},
{"key":"key2",
"value":59}],
"children":[]},
{"id":"node26",
"name":"2.6",
"data":[
{"key":"key1",
"value":2},
{"key":"key2",
"value":-50}],
"children":[]},
{"id":"node27",
"name":"2.7",
"data":[
{"key":"key1",
"value":3},
{"key":"key2",
"value":-78}],
"children":[]}]},
{"id":"node18",
"name":"1.8",
"data":[
{"key":"key1",
"value":1},
{"key":"key2",
"value":79}],
"children":[
{"id":"node29",
"name":"2.9",
"data":[
{"key":"key1",
"value":2},
{"key":"key2",
"value":77}],
"children":[]},
{"id":"node210",
"name":"2.10",
"data":[
{"key":"key1",
"value":6},
{"key":"key2",
"value":30}],
"children":[]},
{"id":"node211",
"name":"2.11",
"data":[
{"key":"key1",
"value":3},
{"key":"key2",
"value":-44}],
"children":[]}]},
{"id":"node112",
"name":"1.12",
"data":[
{"key":"key1",
"value":7},
{"key":"key2",
"value":94}],
"children":[
{"id":"node213",
"name":"2.13",
"data":[
{"key":"key1",
"value":3},
{"key":"key2",
"value":99}],
"children":[]},
{"id":"node214",
"name":"2.14",
"data":[
{"key":"key1",
"value":4},
{"key":"key2",
"value":-72}],
"children":[]},
{"id":"node215",
"name":"2.15",
"data":[
{"key":"key1",
"value":10},
{"key":"key2",
"value":17}],
"children":[]},
{"id":"node216",
"name":"2.16",
"data":[
{"key":"key1",
"value":6},
{"key":"key2",
"value":53}],
"children":[]},
{"id":"node217",
"name":"2.17",
"data":[
{"key":"key1",
"value":4},
{"key":"key2",
"value":-96}],
"children":[]},
{"id":"node218",
"name":"2.18",
"data":[
{"key":"key1",
"value":10},
{"key":"key2",
"value":-84}],
"children":[]},
{"id":"node219",
"name":"2.19",
"data":[
{"key":"key1",
"value":9},
{"key":"key2",
"value":-4}],
"children":[]},
{"id":"node220",
"name":"2.20",
"data":[
{"key":"key1",
"value":2},
{"key":"key2",
"value":66}],
"children":[]}]},
{"id":"node121",
"name":"1.21",
"data":[
{"key":"key1",
"value":10},
{"key":"key2",
"value":0}],
"children":[
{"id":"node222",
"name":"2.22",
"data":[
{"key":"key1",
"value":7},
{"key":"key2",
"value":66}],
"children":[]},
{"id":"node223",
"name":"2.23",
"data":[
{"key":"key1",
"value":10},
{"key":"key2",
"value":-78}],
"children":[]},
{"id":"node224",
"name":"2.24",
"data":[
{"key":"key1",
"value":2},
{"key":"key2",
"value":-91}],
"children":[]},
{"id":"node225",
"name":"2.25",
"data":[
{"key":"key1",
"value":7},
{"key":"key2",
"value":-51}],
"children":[]},
{"id":"node226",
"name":"2.26",
"data":[
{"key":"key1",
"value":2},
{"key":"key2",
"value":25}],
"children":[]},
{"id":"node227",
"name":"2.27",
"data":[
{"key":"key1",
"value":7},
{"key":"key2",
"value":30}],
"children":[]},
{"id":"node228",
"name":"2.28",
"data":[
{"key":"key1",
"value":3},
{"key":"key2",
"value":-43}],
"children":[]}]},
{"id":"node129",
"name":"1.29",
"data":[
{"key":"key1",
"value":3},
{"key":"key2",
"value":-89}],
"children":[
{"id":"node230",
"name":"2.30",
"data":[
{"key":"key1",
"value":6},
{"key":"key2",
"value":96}],
"children":[]},
{"id":"node231",
"name":"2.31",
"data":[
{"key":"key1",
"value":1},
{"key":"key2",
"value":-21}],
"children":[]},
{"id":"node232",
"name":"2.32",
"data":[
{"key":"key1",
"value":2},
{"key":"key2",
"value":17}],
"children":[]},
{"id":"node233",
"name":"2.33",
"data":[
{"key":"key1",
"value":6},
{"key":"key2",
"value":42}],
"children":[]}]},
{"id":"node134",
"name":"1.34",
"data":[
{"key":"key1",
"value":2},
{"key":"key2",
"value":84}],
"children":[
{"id":"node235",
"name":"2.35",
"data":[
{"key":"key1",
"value":9},
{"key":"key2",
"value":-14}],
"children":[]},
{"id":"node236",
"name":"2.36",
"data":[
{"key":"key1",
"value":7},
{"key":"key2",
"value":60}],
"children":[]},
{"id":"node237",
"name":"2.37",
"data":[
{"key":"key1",
"value":1},
{"key":"key2",
"value":-71}],
"children":[]}]},
{"id":"node138",
"name":"1.38",
"data":[
{"key":"key1",
"value":9},
{"key":"key2",
"value":-70}],
"children":[
{"id":"node239",
"name":"2.39",
"data":[
{"key":"key1",
"value":10},
{"key":"key2",
"value":-69}],
"children":[]},
{"id":"node240",
"name":"2.40",
"data":[
{"key":"key1",
"value":4},
{"key":"key2",
"value":-31}],
"children":[]},
{"id":"node241",
"name":"2.41",
"data":[
{"key":"key1",
"value":2},
{"key":"key2",
"value":-5}],
"children":[]},
{"id":"node242",
"name":"2.42",
"data":[
{"key":"key1",
"value":7},
{"key":"key2",
"value":62}],
"children":[]},
{"id":"node243",
"name":"2.43",
"data":[
{"key":"key1",
"value":9},
{"key":"key2",
"value":91}],
"children":[]}]},
{"id":"node144",
"name":"1.44",
"data":[
{"key":"key1",
"value":6},
{"key":"key2",
"value":10}],
"children":[
{"id":"node245",
"name":"2.45",
"data":[
{"key":"key1",
"value":7},
{"key":"key2",
"value":-2}],
"children":[]},
{"id":"node246",
"name":"2.46",
"data":[
{"key":"key1",
"value":1},
{"key":"key2",
"value":93}],
"children":[]},
{"id":"node247",
"name":"2.47",
"data":[
{"key":"key1",
"value":2},
{"key":"key2",
"value":70}],
"children":[]},
{"id":"node248",
"name":"2.48",
"data":[
{"key":"key1",
"value":4},
{"key":"key2",
"value":-40}],
"children":[]}]},
{"id":"node149",
"name":"1.49",
"data":[
{"key":"key1",
"value":6},
{"key":"key2",
"value":-58}],
"children":[
{"id":"node250",
"name":"2.50",
"data":[
{"key":"key1",
"value":4},
{"key":"key2",
"value":-41}],
"children":[]},
{"id":"node251",
"name":"2.51",
"data":[
{"key":"key1",
"value":7},
{"key":"key2",
"value":-91}],
"children":[]},
{"id":"node252",
"name":"2.52",
"data":[
{"key":"key1",
"value":5},
{"key":"key2",
"value":-21}],
"children":[]},
{"id":"node253",
"name":"2.53",
"data":[
{"key":"key1",
"value":2},
{"key":"key2",
"value":-69}],
"children":[]},
{"id":"node254",
"name":"2.54",
"data":[
{"key":"key1",
"value":10},
{"key":"key2",
"value":5}],
"children":[]},
{"id":"node255",
"name":"2.55",
"data":[
{"key":"key1",
"value":5},
{"key":"key2",
"value":-43}],
"children":[]},
{"id":"node256",
"name":"2.56",
"data":[
{"key":"key1",
"value":6},
{"key":"key2",
"value":64}],
"children":[]},
{"id":"node257",
"name":"2.57",
"data":[
{"key":"key1",
"value":10},
{"key":"key2",
"value":-60}],
"children":[]}]},
{"id":"node158",
"name":"1.58",
"data":[
{"key":"key1",
"value":7},
{"key":"key2",
"value":-63}],
"children":[
{"id":"node259",
"name":"2.59",
"data":[
{"key":"key1",
"value":2},
{"key":"key2",
"value":86}],
"children":[]},
{"id":"node260",
"name":"2.60",
"data":[
{"key":"key1",
"value":4},
{"key":"key2",
"value":13}],
"children":[]},
{"id":"node261",
"name":"2.61",
"data":[
{"key":"key1",
"value":2},
{"key":"key2",
"value":-70}],
"children":[]},
{"id":"node262",
"name":"2.62",
"data":[
{"key":"key1",
"value":5},
{"key":"key2",
"value":-83}],
"children":[]},
{"id":"node263",
"name":"2.63",
"data":[
{"key":"key1",
"value":2},
{"key":"key2",
"value":-98}],
"children":[]},
{"id":"node264",
"name":"2.64",
"data":[
{"key":"key1",
"value":2},
{"key":"key2",
"value":-79}],
"children":[]},
{"id":"node265",
"name":"2.65",
"data":[
{"key":"key1",
"value":9},
{"key":"key2",
"value":10}],
"children":[]},
{"id":"node266",
"name":"2.66",
"data":[
{"key":"key1",
"value":6},
{"key":"key2",
"value":17}],
"children":[]},
{"id":"node267",
"name":"2.67",
"data":[
{"key":"key1",
"value":8},
{"key":"key2",
"value":26}],
"children":[]}]}]};</pre>
</div>
<p>Put this HTML in your page:</p>
<pre name="code" class="html:nogutter">&lt;html&gt;
&lt;head&gt;

&lt;link type="text/css" rel="stylesheet" href="/static/css/example-hypertree.css" /&gt;

&lt;!--[if IE]&gt;
&lt;script type="text/javascript" src="/static/js/excanvas.js"&gt;&lt;/script&gt;
&lt;![endif]--&gt;
&lt;script type="text/javascript" src="/static/js/mootools-1.2.js" &gt;&lt;/script&gt;
&lt;script type="text/javascript" src="/static/js/hypertree/Hypertree.js" &gt;&lt;/script&gt;
&lt;script type="text/javascript" src="/static/js/example/example-hypertree.js" &gt;&lt;/script&gt;

&lt;/head&gt;
&lt;body onload="init();"&gt;
&lt;div id="infovis"&gt;&lt;/div&gt;

&lt;/body&gt;
&lt;/html&gt;</pre>
<p><strong>Note:</strong> You&#8217;ll probably have to change the paths to the css and javascript files.</p>
<p>Now, create a hypertree-example.css and put this code in it:</p>
<pre name="code" class="css:nogutter">
html, body {
  width:100%;
  height:100%;
  background-color:#444;
  text-align:center;
  overflow:hidden;
  font-size:10px;
  font-family:Verdana, Geneva, Arial, Helvetica, sans-serif;
}

#infovis {
  background-color:#222;
  width:900px;
  height:500px;
}

.node {
  border: 1px solid #555;
  background-color: #ccc;
  color:#222;
  cursor:pointer;
  padding:2px;
  display:none;
}

.hidden {
  display:none;
}</pre>
<p><strong>Note:</strong> I like the canvas black, just like my coffee.</p>
<p>Finally, create an example-hypertree.js file and put this code in it:</p>
<pre name="code" class="js:nogutter">function init() {

var json = //the json structure mentioned above...
  //Create a new canvas instance.
  var canvas = new Canvas('mycanvas', {
    //Where to inject the canvas. Any HTML container will do.
    'injectInto':'infovis',
    //Width and height of canvas, default's to 200.
    'width': 900,
    'height':500,
    //Canvas styles.
    'styles': {
        'fillStyle': '#ddd',
        'strokeStyle': '#dd00bb'
    }
  });
//create a new hypertree instance.
var ht= new Hypertree(canvas);
//feed the hypertree with a JSON structure.
ht.loadTreeFromJSON(json);
//compute node positions
ht.compute();
//plot the hypertree on canvas
ht.plot();
}</pre>
<p>You should see a hypertree up and running (don&#8217;t worry if it doesn&#8217;t move, we&#8217;ll add click events later).</p>
<h4>Some notes:</h4>
<ul>
<li> For more information about what kind of DOM structure the Canvas object creates and what kind of configuration it takes, please see <a href="http://blog.thejit.org/2009/01/12/a-new-canvas-element/">this post</a>, and the <a href="http://blog.thejit.org/wp-content/jit-1.0a/doc/core/files/Canvas-js.html">Canvas object reference</a>.</li>
<li> If you want to know more about the canvas <em>styles</em> properties you can see <a href="http://developer.mozilla.org/en/docs/Canvas_tutorial:Applying_styles_and_colors" target="_blank">this</a> section from the canvas tutorial.</li>
<li> The Hypertree animation can be triggered by adding custom events to the node labels or by adding a custom event to the canvas object. This will be discussed below.</li>
</ul>
<h4>Customizing the Hypertree</h4>
<p>Let&#8217;s add some labels!</p>
<p>First, strip off the <em>display:none;</em> line from the <em>.node</em> class in your CSS file.<br />
It should look like this:</p>
<pre name="code" class="css:nogutter">
.node {
  border: 1px solid #555;
  background-color: #ccc;
  color:#222;
  cursor:pointer;
  padding:2px;
}
</pre>
<p>Now we are going to add a javascript controller in order to put the name of the nodes into the labels. Since we only need to do this once, we&#8217;ll use the <em>onCreateLabel</em> method. If you don&#8217;t know what I&#8217;m talking about you should probably read the <a href="http://blog.thejit.org/2008/04/27/on-controllers/">on controllers</a> post first.</p>
<p>So the JavaScript file should look like this now:</p>
<pre name="code" class="js:nogutter">function init() {
  var json = //json data mentioned above...
  //Create a new canvas instance.
  var canvas = new Canvas('mycanvas', {
    //Where to inject the canvas. Any HTML container will do.
    'injectInto':'infovis',
    //Width and height of canvas, default's to 200.
    'width': 900,
    'height':500,
    //Canvas styles.
    'styles': {
        'fillStyle': '#ddd',
        'strokeStyle': '#dd00bb'
    }
  });
  var ht= new Hypertree(canvas, {

    onCreateLabel: function(domElement, node) {
      $(domElement).set('html', node.name + " and text");
    }

  });

  ht.loadTreeFromJSON(json);
  ht.compute();
  ht.plot();
}</pre>
<p>You should see some labels now.<br />
The thing is that&#8230; well, they are not centered. So we&#8217;ll just add an onPlaceLabel method to the controller in order to do that, since the onPlaceLabel method is called after labels have been placed.</p>
<p>So the js code should now look like:</p>
<pre name="code" class="js:nogutter">function init() {
  var json = //json data...
  //Create a new canvas instance.
  var canvas = new Canvas('mycanvas', {
    //Where to inject the canvas. Any HTML container will do.
    'injectInto':'infovis',
    //Width and height of canvas, default's to 200.
    'width': 900,
    'height':500,
    //Canvas styles.
    'styles': {
        'fillStyle': '#ddd',
        'strokeStyle': '#dd00bb'
    }
  });
  var ht= new Hypertree(canvas, {
    onCreateLabel: function(domElement, node) {
      $(domElement).set('html', node.name + " and text");
    },

    //Take the left style property and
    // substract half of the label actual width.
    onPlaceLabel: function(tag, node) {
      var width = tag.offsetWidth;
      var intX = tag.style.left.toInt();
      intX -= width/2;
      tag.style.left = intX + 'px';
    }

  });

  ht.loadTreeFromJSON(json);
  ht.compute();
  ht.plot();
}</pre>
<p>You should see some centered labels now!</p>
<p>Finally, we&#8217;ll add an <em>onclick</em> event handler in order to move the tree when clicking on a label, hows that!</p>
<p>We&#8217;ll add this method on the onCreateLabel method, just because we only want to add label event handlers once.<br />
So the code should look like:</p>
<pre name="code" class="js:nogutter">function init() {
  var json = //some json data
  //Create a new canvas instance.
  var canvas = new Canvas('mycanvas', {
    //Where to inject the canvas. Any HTML container will do.
    'injectInto':'infovis',
    //Width and height of canvas, default's to 200.
    'width': 900,
    'height':500,
    //Canvas styles.
    'styles': {
        'fillStyle': '#ddd',
        'strokeStyle': '#dd00bb'
    }
  });
  var ht= new Hypertree(canvas, {
    onCreateLabel: function(domElement, node) {
      $(domElement).set('html', node.name + " and text").addEvents({
          //Call the "onclick" method from
          //the hypertree to move the hypertree
          //correspondingly.
          //This method takes the clicked node's id.
          'click': function(e) {
          ht.onClick(node.id);
          }
        });

    },

    //Take the left style property and
    //substract half of the label actual width.
    onPlaceLabel: function(tag, node) {
      var width = tag.offsetWidth;
      var intX = tag.style.left.toInt();
      intX -= width/2;
      tag.style.left = intX + 'px';
    }

  });

  ht.loadTreeFromJSON(json);
  ht.compute();
  ht.plot();
}</pre>
<p>If you want to see more advanced examples please check the &#8220;examples&#8221; folder in the <a href="http://blog.thejit.org/wp-content/jit.zip" target="_blank">library</a>.</p>
<p>Remember that you have more controller methods!</p>
<p>Remember also that you can change the animation time and the frames per second in the animation with <em>Config.animationTime</em> and <em>Config.fps</em>.<br />
Hope it was helpful.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.thejit.org/2008/05/07/hypertree-quick-tutorial/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
