Class: Treemap::HtmlOutput

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

Instance Attribute Summary collapse

Attributes inherited from OutputBase

#color, #height, #layout, #margin_left, #margin_top, #width

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ HtmlOutput

Returns a new instance of HtmlOutput.

Yields:

  • (_self)

Yield Parameters:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/treemap/html_output.rb', line 20

def initialize
    super

    # default options for HtmlOutput
    @full_html = true
    @center_labels_at_depth = nil
    @center_labels_at_z = 100
    @stylesheets = ""
    @javascripts = ""
    @base_font_size = 14

    yield self if block_given?

    @layout.position = :absolute
end

Instance Attribute Details

#base_font_sizeObject

Returns the value of attribute base_font_size.



18
19
20
# File 'lib/treemap/html_output.rb', line 18

def base_font_size
  @base_font_size
end

#center_labels_at_depthObject

Returns the value of attribute center_labels_at_depth.



18
19
20
# File 'lib/treemap/html_output.rb', line 18

def center_labels_at_depth
  @center_labels_at_depth
end

#center_labels_at_zObject

Returns the value of attribute center_labels_at_z.



18
19
20
# File 'lib/treemap/html_output.rb', line 18

def center_labels_at_z
  @center_labels_at_z
end

#full_htmlObject

Returns the value of attribute full_html.



18
19
20
# File 'lib/treemap/html_output.rb', line 18

def full_html
  @full_html
end

#javascriptsObject

Returns the value of attribute javascripts.



18
19
20
# File 'lib/treemap/html_output.rb', line 18

def javascripts
  @javascripts
end

#metadata=(value) ⇒ Object

Sets the attribute metadata

Parameters:

  • value

    the value to set the attribute metadata to.



18
19
20
# File 'lib/treemap/html_output.rb', line 18

def metadata=(value)
  @metadata = value
end

#stylesheetsObject

Returns the value of attribute stylesheets.



18
19
20
# File 'lib/treemap/html_output.rb', line 18

def stylesheets
  @stylesheets
end

Instance Method Details

#default_cssObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/treemap/html_output.rb', line 36

def default_css
    css = <<CSS
.node {
border: 1px solid black;
}
.label {
color: #FFFFFF;
font-size: 11px;
}
.label-heading {
color: #FFFFFF;
font-size: 14pt;
font-weight: bold;
text-decoration: underline;
}
CSS
end

#draw_label(node) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/treemap/html_output.rb', line 104

def draw_label(node)
    label= "<span"
    if(!@center_labels_at_depth.nil? and @center_labels_at_depth == node.depth)
        px_per_point = 20
        label_size = node.label.length * px_per_point

        label += " style=\""
        label += "overflow: hidden; position: absolute;"
        label += "margin-top: " + (node.bounds.height/2 - node.font_size(@base_font_size)/2).to_s + "px;"

        label += "left: 0px; top: 0px; width: 100%; height: 100%; line-height: 100%; text-align: center;"
        label += "z-index: #{@center_labels_at_z};"
        label += "font-size:#{node.font_size(@base_font_size)}px;"
        label += "\""
        label += " class=\"label-heading\""
    else
        label += " class=\"label\""
        label += " style=\"font-size:#{node.font_size(@base_font_size)}px\""
    end
    label += ">"
    label += node_label(node)
    label += "</span>"

    label
end

#draw_map(node) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/treemap/html_output.rb', line 80

def draw_map(node)
    html = ""

    if(@full_html)
        html += "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
        html += "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"
        html += "<html><head>"
        html += "<title>Treemap - #{node_label(node)}</title>"
        html += "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />"
        html += "<style type=\"text/css\">" + default_css + "</style>"
        html += self.stylesheets
        html += self.javascripts
        html += "</head><body>"
    end

    html += draw_node(node)

    if(@full_html)
        html += "</body></html>"
    end

    html
end

#draw_node(node) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/treemap/html_output.rb', line 135

def draw_node(node)
    return "" if node.bounds.nil?

    root_class = node.depth == 0 ? " treemap-root" : ""
  
    html = "<div id=\"node-#{node.id}\"" 
    html += " style=\""
    html += "overflow: hidden; position: absolute; display: inline;"
    html += "left: #{node.bounds.x1}px; top: #{node.bounds.y1}px;"
    html += "width: #{node.bounds.width}px; height: #{node.bounds.height}px;"
    html += "background-color: " + node_color(node) + ";"
    if(!@center_labels_at_depth.nil? and @center_labels_at_depth == node.depth)
        html += "border: 1px solid black;"
    end
    html += "\" class=\"#{(node)}node#{root_class}\""
    html += ">"

    html += draw_node_body(node)

    if(!node.children.nil? and node.children.size > 0)
        node.children.each do |c|
            html += draw_node(c)
        end
    end

    html += "</div>"
end

#draw_node_body(node) ⇒ Object

Subclass can override to add more html inside <div/> of node



131
132
133
# File 'lib/treemap/html_output.rb', line 131

def draw_node_body(node)
    draw_label(node)
end

#node_color(node) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/treemap/html_output.rb', line 58

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



54
55
56
# File 'lib/treemap/html_output.rb', line 54

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

#to_html(node) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/treemap/html_output.rb', line 72

def to_html(node)
    @bounds = self.bounds

    @layout.process(node, @bounds)

    draw_map(node)
end