Class: Treemap::SvgOutput

Inherits:
OutputBase show all
Defined in:
lib/treemap/svg_output.rb

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.

Yields:

  • (_self)

Yield Parameters:



20
21
22
23
24
# File 'lib/treemap/svg_output.rb', line 20

def initialize
    super

    yield self if block_given?
end

Instance Method Details

#draw_label(node) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/treemap/svg_output.rb', line 34

def draw_label(node)
    # center label in box
    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



74
75
76
77
78
79
80
81
82
83
# File 'lib/treemap/svg_output.rb', line 74

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



85
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
# File 'lib/treemap/svg_output.rb', line 85

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 += " style=\""
    #svg += " fill: " + node_color(node) + ";"
    #svg += " stroke: #000000;"
    #svg += " stroke-width: 1px;"
    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



30
31
32
# File 'lib/treemap/svg_output.rb', line 30

def draw_node_body(node)
    draw_label(node)
end

#node_color(node) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/treemap/svg_output.rb', line 46

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



26
27
28
# File 'lib/treemap/svg_output.rb', line 26

def node_label(node)
    CGI.escapeHTML(node.label)
end

#to_png(node, filename = "treemap.png") ⇒ Object



60
61
62
63
64
# File 'lib/treemap/svg_output.rb', line 60

def to_png(node, filename="treemap.png")
    svg = to_svg(node)
    img = Magick::Image.from_blob(svg) { self.format = "SVG" }
    img[0].write(filename)
end

#to_svg(node) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/treemap/svg_output.rb', line 66

def to_svg(node)
    bounds = self.bounds

    @layout.process(node, bounds)

    draw_map(node)
end