Thursday, October 1, 2009

Ruby treemap

I came across ruby treemap library and tried simple example with it. RubyTreemap is a library for generating treemaps in ruby in multiple formats such as png, svg and html.

Treemaps are for visualizing data sets and are commonly used to display hierarchical data.

Some good links :-

http://rubytreemap.rubyforge.org/

http://www.oreillynet.com/ruby/blog/2006/07/treemap_on_rails.html

A Node in the treemap has a size and a color. The size can be any value and is specific to your data set. So for example, in a treemap of the book sales, a given node's size could be equal to it's total sales for the day. For all non-leaf nodes the size value must be equal to the sum of the sizes of it's children. If the size is nil it will be calculated by recursively summing the size of the child nodes. The color for a node can be either a value usually a percentage (a rate of change) or a hex string color.

Installation and dependencies :-

-- ruby script/plugin install http://github.com/rails/acts_as_tree.git

-- ruby script/plugin install http://code.qnot.org/svn/projects/acts_as_treemap

-- RMagick (rmagick.rubyforge.org/)
Download and extract RMagick-2.10.0-ImageMagick-6.5.3-10-Q8.zip from
http://rubyforge.org/frs/?group_id=12 :-

gem install rmagick --local (installing rmagick-2.10.0-x86-mswin32 )

-- gem install ruby-treemap --source http://gems.rubyforge.org

Our simple Book Model :-



Here is the table that represents books as treemap :-



and here is the generated treemap :-



You can render the treemap in your view by :-


where @root is the parent treemap node.

We can also override the methods in the Treemap::HtmlOutput class to modify the label names, making lables as hyperlinks, etc.

If we want the rate of change of color, we can modify the model entry as below :-

acts_as_treemap :label => :name, :size => :total, :color => :total

So the rate change of color of blocks will be proportionate to the size of the book sales.

No comments:

Post a Comment