mtah-ruby-treemap 0.0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,114 @@
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 draw_node_body(node)
31
+ draw_label(node)
32
+ end
33
+
34
+ def draw_label(node)
35
+ # center label in box
36
+ label = ""
37
+ label += "<text style=\"text-anchor: middle\" font-size=\"15\""
38
+ label += " x=\"" + (node.bounds.x1 + node.bounds.width / 2).to_s + "\""
39
+ label += " y=\"" + (node.bounds.y1 + node.bounds.height / 2).to_s + "\">"
40
+ label += node_label(node)
41
+ label += "</text>\n"
42
+
43
+ label
44
+ end
45
+
46
+ def node_color(node)
47
+ color = "#CCCCCC"
48
+
49
+ if(!node.color.nil?)
50
+ if(not Numeric === node.color)
51
+ color = node.color
52
+ else
53
+ color = "#" + @color.get_hex_color(node.color)
54
+ end
55
+ end
56
+
57
+ color
58
+ end
59
+
60
+ def to_png(node, filename="treemap.png")
61
+ svg = to_svg(node)
62
+ img = Magick::Image.from_blob(svg) { self.format = "SVG" }
63
+ img[0].write(filename)
64
+ end
65
+
66
+ def to_svg(node)
67
+ bounds = self.bounds
68
+
69
+ @layout.process(node, bounds)
70
+
71
+ draw_map(node)
72
+ end
73
+
74
+ def draw_map(node)
75
+ svg = "<svg"
76
+ svg += " xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\""
77
+ svg += " xmlns:ev=\"http://www.w3.org/2001/xml-events\""
78
+ svg += " width=\"" + (node.bounds.width).to_s + "\" height=\"" + (node.bounds.height).to_s + "\">"
79
+
80
+ svg += draw_node(node)
81
+ svg += "</svg>"
82
+ svg
83
+ end
84
+
85
+ def draw_node(node)
86
+ return "" if node.bounds.nil?
87
+
88
+ svg = ""
89
+ svg += "<rect"
90
+ svg += " id=\"rect-" + node.id.to_s + "\""
91
+ svg += " x=\"" + node.bounds.x1.to_s + "\""
92
+ svg += " y=\"" + node.bounds.y1.to_s + "\""
93
+ svg += " width=\"" + node.bounds.width.to_s + "\""
94
+ svg += " height=\"" + node.bounds.height.to_s + "\""
95
+ #svg += " style=\""
96
+ #svg += " fill: " + node_color(node) + ";"
97
+ #svg += " stroke: #000000;"
98
+ #svg += " stroke-width: 1px;"
99
+ svg += " fill=\"" + node_color(node) + "\""
100
+ svg += " stroke=\"#000000\""
101
+ svg += " stroke-width=\"1px\""
102
+ svg += " />\n"
103
+
104
+ svg += draw_node_body(node)
105
+
106
+ if(!node.children.nil? and node.children.size > 0)
107
+ node.children.each do |c|
108
+ svg += draw_node(c)
109
+ end
110
+ end
111
+
112
+ svg
113
+ end
114
+ end
data/test/tc_color.rb ADDED
@@ -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
data/test/tc_html.rb ADDED
@@ -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
data/test/tc_simple.rb ADDED
@@ -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
data/test/tc_slice.rb ADDED
@@ -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
data/test/tc_svg.rb ADDED
@@ -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
data/test/test_base.rb ADDED
@@ -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,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mtah-ruby-treemap
3
+ version: !ruby/object:Gem::Version
4
+ hash: 65
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 3
10
+ - 1
11
+ version: 0.0.3.1
12
+ platform: ruby
13
+ authors:
14
+ - "Martin H\xC3\xA4ger"
15
+ autorequire: treemap
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2010-08-16 00:00:00 +02:00
20
+ default_executable:
21
+ dependencies: []
22
+
23
+ description: Treemap visualization in ruby
24
+ email: martin.haeger@gmail.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files:
30
+ - README
31
+ - EXAMPLES
32
+ files:
33
+ - Rakefile
34
+ - TODO
35
+ - EXAMPLES
36
+ - README
37
+ - ChangeLog
38
+ - COPYING
39
+ - lib/treemap/layout_base.rb
40
+ - lib/treemap/output_base.rb
41
+ - lib/treemap/gradient_color.rb
42
+ - lib/treemap/slice_layout.rb
43
+ - lib/treemap/rectangle.rb
44
+ - lib/treemap/html_output.rb
45
+ - lib/treemap/color_base.rb
46
+ - lib/treemap/svg_output.rb
47
+ - lib/treemap/squarified_layout.rb
48
+ - lib/treemap/node.rb
49
+ - lib/treemap/image_output.rb
50
+ - lib/treemap.rb
51
+ - test/test_base.rb
52
+ - test/tc_simple.rb
53
+ - test/tc_svg.rb
54
+ - test/tc_squarified.rb
55
+ - test/tc_color.rb
56
+ - test/tc_html.rb
57
+ - test/tc_slice.rb
58
+ has_rdoc: true
59
+ homepage: http://github.com/mtah/ruby-treemap
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options:
64
+ - --main
65
+ - README
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ hash: 3
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ hash: 3
83
+ segments:
84
+ - 0
85
+ version: "0"
86
+ requirements:
87
+ - none
88
+ rubyforge_project:
89
+ rubygems_version: 1.3.7
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: Treemap visualization in ruby
93
+ test_files: []
94
+