Class: Treemap::SvgOutput
Instance Attribute Summary
Attributes inherited from OutputBase
#color, #height, #layout, #margin_left, #margin_top, #width
Instance Method Summary
collapse
Constructor Details
#initialize {|_self| ... } ⇒ SvgOutput
Returns a new instance of SvgOutput.
19
20
21
22
23
|
# File 'lib/treemap/svg_output.rb', line 19
def initialize
super
yield self if block_given?
end
|
Instance Method Details
#draw_label(node) ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/treemap/svg_output.rb', line 33
def draw_label(node)
label = ""
label += "<text style=\"text-anchor: middle\" font-size=\"15\""
label += " x=\"" + (node.bounds.x1 + node.bounds.width / 2).to_s + "\""
label += " y=\"" + (node.bounds.y1 + node.bounds.height / 2).to_s + "\">"
label += node_label(node)
label += "</text>\n"
label
end
|
#draw_map(node) ⇒ Object
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/treemap/svg_output.rb', line 75
def draw_map(node)
svg = "<svg"
svg += " xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\""
svg += " xmlns:ev=\"http://www.w3.org/2001/xml-events\""
svg += " width=\"" + (node.bounds.width).to_s + "\" height=\"" + (node.bounds.height).to_s + "\">"
svg += draw_node(node)
svg += "</svg>"
svg
end
|
#draw_node(node) ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/treemap/svg_output.rb', line 86
def draw_node(node)
return "" if node.bounds.nil?
svg = ""
svg += "<rect"
svg += " id=\"rect-" + node.id.to_s + "\""
svg += " x=\"" + node.bounds.x1.to_s + "\""
svg += " y=\"" + node.bounds.y1.to_s + "\""
svg += " width=\"" + node.bounds.width.to_s + "\""
svg += " height=\"" + node.bounds.height.to_s + "\""
svg += " fill=\"" + node_color(node) + "\""
svg += " stroke=\"#000000\""
svg += " stroke-width=\"1px\""
svg += " />\n"
svg += draw_node_body(node)
if(!node.children.nil? and node.children.size > 0)
node.children.each do |c|
svg += draw_node(c)
end
end
svg
end
|
#draw_node_body(node) ⇒ Object
29
30
31
|
# File 'lib/treemap/svg_output.rb', line 29
def draw_node_body(node)
draw_label(node)
end
|
#node_color(node) ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/treemap/svg_output.rb', line 45
def node_color(node)
color = "#CCCCCC"
if(!node.color.nil?)
if(not Numeric === node.color)
color = node.color
else
color = "#" + @color.get_hex_color(node.color)
end
end
color
end
|
#node_label(node) ⇒ Object
25
26
27
|
# File 'lib/treemap/svg_output.rb', line 25
def node_label(node)
CGI.escapeHTML(node.label)
end
|
#to_png(node, filename = "treemap.png") ⇒ Object
59
60
61
62
63
64
65
|
# File 'lib/treemap/svg_output.rb', line 59
def to_png(node, filename="treemap.png")
require 'RMagick'
svg = to_svg(node)
img = Magick::Image.from_blob(svg) { self.format = "SVG" }
img[0].write(filename)
end
|
#to_svg(node) ⇒ Object
67
68
69
70
71
72
73
|
# File 'lib/treemap/svg_output.rb', line 67
def to_svg(node)
bounds = self.bounds
@layout.process(node, bounds)
draw_map(node)
end
|