ruby-treemap 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,96 @@
1
+ #
2
+ # svg_output.rb - RubyTreemap
3
+ #
4
+ # Copyright (c) 2006 by Andrew Bruno <aeb@qnot.org>
5
+ #
6
+ # This program is free software; you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation; either version 2 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ #
12
+
13
+ require 'cgi'
14
+ require 'RMagick'
15
+
16
+ require File.dirname(__FILE__) + "/output_base"
17
+
18
+ class Treemap::SvgOutput < Treemap::OutputBase
19
+
20
+ def initialize
21
+ super
22
+
23
+ yield self if block_given?
24
+ end
25
+
26
+ def node_label(node)
27
+ CGI.escapeHTML(node.label)
28
+ end
29
+
30
+ def node_color(node)
31
+ color = "#CCCCCC"
32
+
33
+ if(!node.color.nil?)
34
+ if(not Numeric === node.color)
35
+ color = node.color
36
+ else
37
+ color = "#" + @color.get_hex_color(node.color)
38
+ end
39
+ end
40
+
41
+ color
42
+ end
43
+
44
+ def to_png(node, filename="treemap.png")
45
+ svg = to_svg(node)
46
+ img = Magick::Image.from_blob(svg) { self.format = "SVG" }
47
+ img[0].write(filename)
48
+ end
49
+
50
+ def to_svg(node)
51
+ bounds = self.bounds
52
+
53
+ @layout.process(node, bounds)
54
+
55
+ draw_map(node)
56
+ end
57
+
58
+ def draw_map(node)
59
+ svg = "<svg"
60
+ svg += " xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\""
61
+ svg += " xmlns:ev=\"http://www.w3.org/2001/xml-events\""
62
+ svg += " width=\"" + (node.bounds.width).to_s + "\" height=\"" + (node.bounds.height).to_s + "\">"
63
+
64
+ svg += draw_node(node)
65
+ svg += "</svg>"
66
+ svg
67
+ end
68
+
69
+ def draw_node(node)
70
+ return "" if node.bounds.nil?
71
+
72
+ svg = ""
73
+ svg += "<rect"
74
+ svg += " id=\"rect-" + node.id.to_s + "\""
75
+ svg += " x=\"" + node.bounds.x1.to_s + "\""
76
+ svg += " y=\"" + node.bounds.y1.to_s + "\""
77
+ svg += " width=\"" + node.bounds.width.to_s + "\""
78
+ svg += " height=\"" + node.bounds.height.to_s + "\""
79
+ #svg += " style=\""
80
+ #svg += " fill: " + node_color(node) + ";"
81
+ #svg += " stroke: #000000;"
82
+ #svg += " stroke-width: 1px;"
83
+ svg += " fill=\"" + node_color(node) + "\""
84
+ svg += " stroke=\"#000000\""
85
+ svg += " stroke-width=\"1px\""
86
+ svg += " />"
87
+
88
+ if(!node.children.nil? and node.children.size > 0)
89
+ node.children.each do |c|
90
+ svg += draw_node(c)
91
+ end
92
+ end
93
+
94
+ svg
95
+ end
96
+ end
@@ -0,0 +1,17 @@
1
+
2
+ require "test_base"
3
+
4
+ class TestColor < Test::Unit::TestCase
5
+ def test_color
6
+ g = Treemap::GradientColor.new do |g|
7
+ g.min_color = "FC0404"
8
+ g.mean_color = "DBD9DC"
9
+ g.max_color = "0092C8"
10
+ g.increment = 0.5
11
+ end
12
+
13
+ File.open(File.dirname(__FILE__) + '/color_test.html', "w") do |f|
14
+ f.puts g.to_html
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,22 @@
1
+
2
+ require "test_base"
3
+
4
+ class TestHTMLOutput < Test::Unit::TestCase
5
+ include TestTreemapBase
6
+
7
+ def setup
8
+ @root = setup_root
9
+ end
10
+
11
+ def test_html_output
12
+ output = Treemap::HtmlOutput.new do |o|
13
+ o.width = 800
14
+ o.height = 600
15
+ o.center_labels_at_depth = 1
16
+ end
17
+
18
+ File.open(File.dirname(__FILE__) + '/html_test.html', "w") do |f|
19
+ f.puts output.to_html(@root)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,30 @@
1
+
2
+ require "test_base"
3
+
4
+ class TestSimple < Test::Unit::TestCase
5
+ def setup
6
+ @root = Treemap::Node.new
7
+ @root.new_child(:size => 6)
8
+ @root.new_child(:size => 6)
9
+ @root.new_child(:size => 4)
10
+ @root.new_child(:size => 3)
11
+ @root.new_child(:size => 2)
12
+ @root.new_child(:size => 2)
13
+ @root.new_child(:size => 1)
14
+ end
15
+
16
+ def test_simple
17
+ output = Treemap::ImageOutput.new do |o|
18
+ o.width = 600
19
+ o.height = 400
20
+ o.layout = Treemap::SquarifiedLayout.new
21
+ end
22
+
23
+ bounds = Treemap::Rectangle.new(0, 0, 600, 400)
24
+ output.layout.process(@root, bounds)
25
+
26
+ Treemap::dump_tree(@root)
27
+
28
+ output.to_png(@root, File.dirname(__FILE__) + '/simple_test.png')
29
+ end
30
+ end
@@ -0,0 +1,25 @@
1
+
2
+ require "test_base"
3
+
4
+ class TestSlice < Test::Unit::TestCase
5
+ include TestTreemapBase
6
+
7
+ def setup
8
+ @root = setup_root
9
+ end
10
+
11
+ def test_slice
12
+ output = Treemap::ImageOutput.new do |o|
13
+ o.width = 800
14
+ o.height = 600
15
+ o.layout = Treemap::SliceLayout.new
16
+ end
17
+
18
+ output.to_png(@root, File.dirname(__FILE__) + '/slice_test.png')
19
+
20
+ bounds = Treemap::Rectangle.new(0, 0, 800, 600)
21
+ output.layout.process(@root, bounds)
22
+
23
+ Treemap::dump_tree(@root)
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+
2
+ require "test_base"
3
+
4
+ class TestSquarified < Test::Unit::TestCase
5
+ include TestTreemapBase
6
+
7
+ def setup
8
+ @root = setup_root
9
+ end
10
+
11
+ def test_squarified
12
+ output = Treemap::ImageOutput.new do |o|
13
+ o.width = 800
14
+ o.height = 600
15
+ o.layout = Treemap::SquarifiedLayout.new
16
+ end
17
+
18
+ output.to_png(@root, File.dirname(__FILE__) + '/squarified_test.png')
19
+
20
+ bounds = Treemap::Rectangle.new(0, 0, 800, 600)
21
+ output.layout.process(@root, bounds)
22
+
23
+ Treemap::dump_tree(@root)
24
+ end
25
+ end
@@ -0,0 +1,21 @@
1
+
2
+ require "test_base"
3
+
4
+ class TestSvgOutput < Test::Unit::TestCase
5
+ include TestTreemapBase
6
+
7
+ def setup
8
+ @root = setup_root
9
+ end
10
+
11
+ def test_svg_output
12
+ output = Treemap::SvgOutput.new do |o|
13
+ o.width = 800
14
+ o.height = 600
15
+ end
16
+
17
+ File.open(File.dirname(__FILE__) + '/svg_test.svg', "w") do |f|
18
+ f.puts output.to_svg(@root)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,39 @@
1
+
2
+ require "test/unit"
3
+ require "treemap"
4
+ begin
5
+ require "RMagick"
6
+ require "treemap/image_output"
7
+ require "treemap/svg_output"
8
+ rescue LoadError
9
+ end
10
+
11
+ module TestTreemapBase
12
+ def setup_root
13
+ root = Treemap::Node.new(:size => 100, :label => "all")
14
+
15
+ root.new_child(:size => 12, :label => "Child1")
16
+ root.new_child(:size => 8, :label => "Child2")
17
+
18
+ child3 = Treemap::Node.new(:size => 10, :label => "Child3")
19
+ [2,2,2,3,1].each do |n|
20
+ child3.new_child(:size => n, :label => "Child3-"+n.to_s)
21
+ end
22
+ root.add_child(child3)
23
+
24
+ child4 = Treemap::Node.new(:size => 70, :label => "Child4")
25
+ [5, 40].each do |n|
26
+ child4.new_child(:size => n, :label => "Child4-"+n.to_s)
27
+ end
28
+
29
+ child5 = Treemap::Node.new(:size => 25, :label => "Child5")
30
+ [6,8,11].each do |n|
31
+ child5.new_child(:size => n, :label => "Child5-"+n.to_s)
32
+ end
33
+
34
+ child4.add_child(child5)
35
+ root.add_child(child4)
36
+
37
+ root
38
+ end
39
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: ruby-treemap
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.0.1
7
+ date: 2006-07-17 00:00:00 -07:00
8
+ summary: Treemap visualization in ruby
9
+ require_paths:
10
+ - lib
11
+ email: aeb@qnot.org
12
+ homepage: http://rubytreemap.rubyforge.org/
13
+ rubyforge_project:
14
+ description: Treemap visualization in ruby
15
+ autorequire: treemap
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Andrew Bruno
31
+ files:
32
+ - Rakefile
33
+ - TODO
34
+ - EXAMPLES
35
+ - README
36
+ - ChangeLog
37
+ - COPYING
38
+ - lib/treemap
39
+ - lib/treemap.rb
40
+ - lib/treemap/squarified_layout.rb
41
+ - lib/treemap/svg_output.rb
42
+ - lib/treemap/image_output.rb
43
+ - lib/treemap/html_output.rb
44
+ - lib/treemap/rectangle.rb
45
+ - lib/treemap/node.rb
46
+ - lib/treemap/layout_base.rb
47
+ - lib/treemap/color_base.rb
48
+ - lib/treemap/gradient_color.rb
49
+ - lib/treemap/output_base.rb
50
+ - lib/treemap/slice_layout.rb
51
+ - test/tc_squarified.rb
52
+ - test/tc_simple.rb
53
+ - test/tc_color.rb
54
+ - test/tc_slice.rb
55
+ - test/tc_svg.rb
56
+ - test/test_base.rb
57
+ - test/tc_html.rb
58
+ test_files: []
59
+
60
+ rdoc_options:
61
+ - --main
62
+ - README
63
+ extra_rdoc_files:
64
+ - README
65
+ - EXAMPLES
66
+ executables: []
67
+
68
+ extensions: []
69
+
70
+ requirements:
71
+ - none
72
+ dependencies: []
73
+