Merge tag 'upstream/3.0.717'
[darkstat-debian] / graphjs.h
1 /* this file was automatically generated */
2 static char graph_js[] =
3 "/* darkstat 3\n"
4 " * copyright (c) 2006-2008 Emil Mikulic.\n"
5 " *\n"
6 " * graph.js: graph renderer\n"
7 " *\n"
8 " * You may use, modify and redistribute this file under the terms of the\n"
9 " * GNU General Public License version 2. (see COPYING.GPL)\n"
10 " *\n"
11 " * At some point, this script worked correctly in:\n"
12 " * - Firefox 1.5.0.4, 2.0.0.1, 3.0\n"
13 " * - IE 6.0\n"
14 " * - Opera 8.53, 9.50\n"
15 " * - Konqueror 3.5.9, 4.0.80, 4.0.83\n"
16 " *\n"
17 " * Consumer needs to supply the following variables:\n"
18 " * - graph_width\n"
19 " * - graph_height\n"
20 " * - bar_gap\n"
21 " *\n"
22 " * - graphs [ {id, name, title, bar_secs} ]\n"
23 " * - graphs_uri\n"
24 " *\n"
25 " * - window.onload = graphs_init\n"
26 " */\n"
27 "\n"
28 "function killChildren(elem) {\n"
29 " while (elem.childNodes.length > 0)\n"
30 " elem.removeChild( elem.childNodes.item(0) );\n"
31 "}\n"
32 "\n"
33 "function setClass(elem, c) {\n"
34 " elem.setAttribute(\"class\", c);\n"
35 " elem.setAttribute(\"className\", c); /* for MSIE */\n"
36 "}\n"
37 "\n"
38 "function setStyle(elem, s) {\n"
39 " elem.setAttribute(\"style\", s);\n"
40 " elem.style.cssText = s; /* for MSIE */\n"
41 "}\n"
42 "\n"
43 "function makeElemClass(e, c) {\n"
44 " var r = document.createElement(e);\n"
45 " setClass(r, c);\n"
46 " return r;\n"
47 "}\n"
48 "\n"
49 "function makeClear() {\n"
50 " var r = document.createElement(\"div\");\n"
51 " setStyle(r, \"clear:both\");\n"
52 " return r;\n"
53 "}\n"
54 "\n"
55 "function thousands(n) {\n"
56 " var s = String(n);\n"
57 " var out = \"\";\n"
58 " while (s.length > 3) {\n"
59 " out = \",\" + s.substr(s.length - 3, 3) + out;\n"
60 " s = s.substr(0, s.length - 3);\n"
61 " }\n"
62 " return s+out;\n"
63 "}\n"
64 "\n"
65 "function fkbps(bps) {\n"
66 " bps /= 1024;\n"
67 " return bps.toFixed(1);\n"
68 "}\n"
69 "\n"
70 "function kbps(bps) {\n"
71 " bps /= 1024;\n"
72 " if (bps < 1) return bps.toPrecision(2);\n"
73 " else return bps.toFixed(1);\n"
74 "}\n"
75 "\n"
76 "function min(a,b) { return (a<b)?a:b; }\n"
77 "function max(a,b) { return (a>b)?a:b; }\n"
78 "\n"
79 "var xh, autoreload=false;\n"
80 "\n"
81 "function graphs_init() {\n"
82 " var gr = document.getElementById(\"graphs\");\n"
83 "\n"
84 " /* update message */\n"
85 " var msg = document.createElement(\"div\");\n"
86 " msg.appendChild(document.createTextNode(\"Graphs are being loaded...\"));\n"
87 " msg.appendChild(document.createElement(\"br\"));\n"
88 " msg.appendChild(document.createElement(\"br\"));\n"
89 " killChildren(gr);\n"
90 " gr.appendChild(msg);\n"
91 " graphs.msg = msg;\n"
92 "\n"
93 " for (var i=0; i<graphs.length; i++) {\n"
94 " var g = makeElemClass(\"div\", \"outergraph\");\n"
95 " gr.appendChild(g);\n"
96 " graphs[i].graph = g;\n"
97 " if (i % 2 == 1) gr.appendChild(makeClear());\n"
98 " }\n"
99 "\n"
100 " /* create buttons */\n"
101 " var b_reload = document.createElement(\"a\");\n"
102 " b_reload.setAttribute(\"id\", \"graph_reload\");\n"
103 " b_reload.setAttribute(\"href\", \"javascript:graph_reload()\");\n"
104 " b_reload.appendChild(document.createTextNode(\"reload graphs\"));\n"
105 "\n"
106 " var b_autoreload = document.createElement(\"a\");\n"
107 " b_autoreload.setAttribute(\"id\", \"graph_autoreload\");\n"
108 " b_autoreload.setAttribute(\"href\", \"javascript:graph_autoreload()\");\n"
109 " b_autoreload.appendChild(document.createTextNode(\"off\"));\n"
110 "\n"
111 " var b = document.createElement(\"div\");\n"
112 " b.appendChild(b_reload);\n"
113 " b.appendChild(document.createTextNode(\" - automatic reload is: \"));\n"
114 " b.appendChild(b_autoreload);\n"
115 " gr.appendChild(b);\n"
116 "\n"
117 " graph_reload();\n"
118 "}\n"
119 "\n"
120 "function graph_reload() {\n"
121 " if (!autoreload)\n"
122 " document.getElementById(\"graph_reload\").innerHTML = \"loading...\";\n"
123 " xh = (window.ActiveXObject)\n"
124 " ? new ActiveXObject(\"Microsoft.XMLHTTP\")\n"
125 " : new XMLHttpRequest();\n"
126 " var asyncFlag = true;\n"
127 " xh.open(\"GET\", graphs_uri, asyncFlag);\n"
128 " // try to nerf caching:\n"
129 " xh.setRequestHeader(\"If-Modified-Since\", \"Sat, 1 Jan 2000 00:00:00 GMT\");\n"
130 " xh.onreadystatechange = poll;\n"
131 " xh.send(null);\n"
132 "}\n"
133 "\n"
134 "function graph_autoreload() {\n"
135 " // toggle\n"
136 " autoreload = !autoreload;\n"
137 " document.getElementById(\"graph_autoreload\").innerHTML =\n"
138 " autoreload ? \"on\" : \"off\";\n"
139 " if (autoreload) reload_loop();\n"
140 "}\n"
141 "\n"
142 "function reload_loop() {\n"
143 " if (autoreload) {\n"
144 " graph_reload();\n"
145 " setTimeout(\"reload_loop()\", 1000);\n"
146 " }\n"
147 "}\n"
148 "\n"
149 "function poll() {\n"
150 " var STATE_COMPLETE = 4;\n"
151 " if (xh && xh.readyState == STATE_COMPLETE) {\n"
152 " for (var i=0; i<graphs.length; i++)\n"
153 " {\n"
154 " g = xh.responseXML.getElementsByTagName(graphs[i].name);\n"
155 " buildGraph(graphs[i].graph, graphs[i].title, graphs[i].bar_secs,\n"
156 " g[0].getElementsByTagName(\"e\"));\n"
157 " }\n"
158 " document.getElementById(\"graph_reload\").innerHTML = \"reload graphs\";\n"
159 " killChildren(graphs.msg);\n"
160 " head = xh.responseXML.childNodes[0];\n"
161 " for (var n in {\"tb\":0, \"tp\":0, \"pc\":0, \"pd\":0})\n"
162 " document.getElementById(n).innerHTML = thousands(head.getAttribute(n));\n"
163 " document.getElementById(\"rf\").innerHTML = head.getAttribute(\"rf\");\n"
164 " }\n"
165 "}\n"
166 "\n"
167 "function addBar(graph, title, barclass, width, height, left, bottom) {\n"
168 " if (height == 0) return; /* not visible */\n"
169 " var bar = makeElemClass(\"div\", barclass);\n"
170 " bar.setAttribute(\"title\", title);\n"
171 " setStyle(bar,\n"
172 " \"width:\"+width+\"px; \"+\n"
173 " \"height:\"+height+\"px; \"+\n"
174 " \"position: absolute; \"+\n"
175 " \"left:\"+left+\"px; \"+\n"
176 " \"bottom:\"+bottom+\"px;\");\n"
177 " graph.appendChild(bar);\n"
178 "}\n"
179 "\n"
180 "function buildGraph(graph, title, bar_secs, elems) {\n"
181 " var total_max = 0;\n"
182 " var data = []; /* list of [in, out] */\n"
183 " for (var i=0; i<elems.length; i++) {\n"
184 " var elem = elems.item(i);\n"
185 " var b_pos = Number( elem.getAttribute(\"p\") );\n"
186 " var b_in = Number( elem.getAttribute(\"i\") );\n"
187 " var b_out = Number( elem.getAttribute(\"o\") );\n"
188 " var b_total = b_in + b_out;\n"
189 "/* FIXME: what happens when a bar's value is >4G? */\n"
190 " if (b_total > total_max)\n"
191 " total_max = b_total;\n"
192 " data.push( [b_pos, b_in, b_out] );\n"
193 " }\n"
194 "\n"
195 " var igraph = makeElemClass(\"div\", \"graph\"); // inner graph\n"
196 " setStyle(igraph,\n"
197 " \"width:\"+graph_width+\"px; \"+\n"
198 " \"height:\"+graph_height+\"px; \"+\n"
199 " \"position:relative;\");\n"
200 "\n"
201 " var nbars = data.length;\n"
202 " var b_width = (graph_width - bar_gap * (nbars-1)) / nbars;\n"
203 " var next_xofs = 0;\n"
204 "\n"
205 " var min_i = 0, min_o = 0,\n"
206 " max_i = 0, max_o = 0,\n"
207 " tot_i = 0, tot_o = 0;\n"
208 "\n"
209 " for (var i=0; i<nbars; i++) {\n"
210 " var b_p = data[i][0];\n"
211 " var b_i = data[i][1];\n"
212 " var b_o = data[i][2];\n"
213 "\n"
214 " if (b_i>0) { if (min_i == 0) min_i = b_i; else min_i = min(min_i, b_i); }\n"
215 " max_i = max(max_i, b_i);\n"
216 " tot_i += b_i;\n"
217 "\n"
218 " if (b_o>0) { if (min_o == 0) min_o = b_o; else min_o = min(min_o, b_o); }\n"
219 " max_o = max(max_o, b_o);\n"
220 " tot_o += b_o;\n"
221 "\n"
222 " var xofs = next_xofs;\n"
223 "\n"
224 " next_xofs = Math.round((b_width + bar_gap) * (i+1));\n"
225 " var curr_w = next_xofs - xofs - bar_gap;\n"
226 "\n"
227 " var h_i = Math.round( b_i * graph_height / total_max );\n"
228 " var h_o = Math.round( b_o * graph_height / total_max );\n"
229 "\n"
230 " var label = b_p+\": \"+\n"
231 " thousands(b_i)+\" bytes in, \"+\n"
232 " thousands(b_o)+\" bytes out | \"+\n"
233 " kbps(b_i/bar_secs)+\" KB/s in, \"+\n"
234 " kbps(b_o/bar_secs)+\" KB/s out\";\n"
235 "\n"
236 " addBar(igraph, label, \"bar_in\", curr_w, h_i, xofs, 0);\n"
237 " addBar(igraph, label, \"bar_out\", curr_w, h_o, xofs, h_i);\n"
238 " }\n"
239 "\n"
240 " function legendRow(dir_str, minb, avgb, maxb) {\n"
241 " function makeTD(c, str) {\n"
242 " var r = makeElemClass(\"td\", c);\n"
243 " r.appendChild(document.createTextNode(str));\n"
244 " return r;\n"
245 " }\n"
246 " function addToRow(row, type_str, bytes, trail) {\n"
247 " row.appendChild( makeTD(\"type\", type_str) );\n"
248 " row.appendChild( makeTD(\"rate\", fkbps(bytes/bar_secs)+\" KB/s\"+trail) );\n"
249 " }\n"
250 " var row = document.createElement(\"tr\");\n"
251 " row.appendChild( makeTD(\"dir\", dir_str) );\n"
252 " var cell = makeElemClass(\"td\", \"swatch\");\n"
253 " var swatch = makeElemClass(\"div\", \"bar_\"+dir_str);\n"
254 " setStyle(swatch, \"width:6px; height:6px;\");\n"
255 " cell.appendChild(swatch);\n"
256 " row.appendChild(cell);\n"
257 " addToRow(row, \"min:\", minb, \",\");\n"
258 " addToRow(row, \"avg:\", avgb, \",\");\n"
259 " addToRow(row, \"max:\", maxb, \"\");\n"
260 " return row;\n"
261 " }\n"
262 "\n"
263 " var glegend = makeElemClass(\"div\", \"legend\");\n"
264 " var avg_i = tot_i / nbars,\n"
265 " avg_o = tot_o / nbars;\n"
266 " var tbl = document.createElement(\"table\");\n"
267 " var tb = document.createElement(\"tbody\"); /* for MSIE */\n"
268 " tb.appendChild( legendRow(\"in\", min_i, avg_i, max_i) );\n"
269 " tb.appendChild( legendRow(\"out\", min_o, avg_o, max_o) );\n"
270 " tbl.appendChild(tb);\n"
271 " glegend.appendChild(tbl);\n"
272 " setStyle(glegend, \"width:\"+graph_width+\"px;\");\n"
273 "\n"
274 " var gtitle = makeElemClass(\"div\", \"graphtitle\");\n"
275 " setStyle(gtitle, \"width:\"+graph_width+\"px;\");\n"
276 " gtitle.appendChild(document.createTextNode(title));\n"
277 "\n"
278 " killChildren(graph);\n"
279 " graph.appendChild(igraph);\n"
280 " graph.appendChild(glegend);\n"
281 " graph.appendChild(gtitle);\n"
282 "}\n";
283 static const size_t graph_js_len = sizeof(graph_js) - 1;