Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 297
- Log:
Updated to AWStats 7.0.
- Author:
- rool
- Date:
- Fri Mar 18 13:33:29 +0000 2011
- Size:
- 19234 Bytes
- Properties:
- Property svn:executable is set to *
1 | #!/usr/bin/perl |
2 | #----------------------------------------------------------------------------- |
3 | # GraphGoogleChartApi AWStats plugin |
4 | # Allow AWStats to replace bar graphs with a Google Graph image |
5 | #----------------------------------------------------------------------------- |
6 | # Perl Required Modules: None |
7 | #----------------------------------------------------------------------------- |
8 | # $Revision: 1.5 $ - $Author: eldy $ - $Date: 2010/06/22 21:35:24 $ |
9 | # |
10 | # Changelog |
11 | # |
12 | # 1.0 - Initial release by george@dynapres.nl |
13 | # 1.1 - Changed scaling: making it independent of chart series |
14 | # 1.2 - Added pie charts, visualization hook, map and axis labels by Chris Larsen |
15 | |
16 | # <----- |
17 | # ENTER HERE THE USE COMMAND FOR ALL REQUIRED PERL MODULES |
18 | # -----> |
19 | #use strict; |
20 | no strict "refs"; |
21 | |
22 | |
23 | #----------------------------------------------------------------------------- |
24 | # PLUGIN VARIABLES |
25 | #----------------------------------------------------------------------------- |
26 | # <----- |
27 | # ENTER HERE THE MINIMUM AWSTATS VERSION REQUIRED BY YOUR PLUGIN |
28 | # AND THE NAME OF ALL FUNCTIONS THE PLUGIN MANAGE. |
29 | my $PluginNeedAWStatsVersion = "7.0"; |
30 | my $PluginHooksFunctions = "Init ShowGraph AddHTMLHeader"; |
31 | my $PluginName = "graphgooglechartapi"; |
32 | my $ChartProtocol = "http://"; |
33 | my $ChartURI = "chart.apis.google.com/chart?"; # Don't put the HTTP part here! |
34 | my $ChartIndex = 0; |
35 | my $title; |
36 | my $type; |
37 | my $imagewidth = 640; # maximum image width. |
38 | my $imageratio = .25; # Height is defaulted to 25% of width |
39 | my $pieratio = .20; # Height for pie charts should be different |
40 | my $mapratio = .62; # Height for maps is different |
41 | my $labellength; |
42 | my @blocklabel = (); |
43 | my @vallabel = (); |
44 | my @valcolor = (); |
45 | my @valmax = (); |
46 | my @valtotal = (); |
47 | my @valaverage = (); |
48 | my @valdata = (); |
49 | # -----> |
50 | |
51 | # <----- |
52 | # IF YOUR PLUGIN NEED GLOBAL VARIABLES, THEY MUST BE DECLARED HERE. |
53 | use vars qw/ |
54 | $DirClasses |
55 | $URLIndex |
56 | /; |
57 | # -----> |
58 | |
59 | #----------------------------------------------------------------------------- |
60 | # PLUGIN FUNCTION: Init_pluginname |
61 | #----------------------------------------------------------------------------- |
62 | sub Init_graphgooglechartapi { |
63 | my $InitParams = shift; |
64 | my $checkversion = &Check_Plugin_Version($PluginNeedAWStatsVersion); |
65 | |
66 | # <----- |
67 | # ENTER HERE CODE TO DO INIT PLUGIN ACTIONS |
68 | $DirClasses = $InitParams; |
69 | # -----> |
70 | |
71 | $title = ""; |
72 | $type = ""; |
73 | $labellength=2; |
74 | $ChartIndex = -1; |
75 | |
76 | return ($checkversion?$checkversion:"$PluginHooksFunctions"); |
77 | } |
78 | |
79 | #------------------------------------------------------- |
80 | # PLUGIN FUNCTION: ShowGraph_pluginname |
81 | # UNIQUE: YES (Only one plugin using this function can be loaded) |
82 | # Prints the proper chart depending on the $type provided |
83 | # Parameters: $title $type $imagewidth \@blocklabel,\@vallabel,\@valcolor,\@valmax,\@valtotal |
84 | # Input: None |
85 | # Output: HTML code for awgraphapplet insertion |
86 | # Return: 0 OK, 1 Error |
87 | #------------------------------------------------------- |
88 | sub ShowGraph_graphgooglechartapi() { |
89 | $title = shift; |
90 | $type = shift; |
91 | $imagewidth = shift || 640; |
92 | $blocklabel = shift; |
93 | $vallabel = shift; |
94 | $valcolor = shift; |
95 | $valmax = shift; |
96 | $valtotal = shift; |
97 | $valaverage = shift; |
98 | $valdata = shift; |
99 | |
100 | # check width |
101 | if ($imagewidth < 1){$imagewidth=640;} |
102 | |
103 | if ($type eq 'month') { |
104 | $labellength=3; |
105 | print Get_Img_Tag(Graph_Monthly(), $title); |
106 | } |
107 | elsif ($type eq 'daysofmonth') { |
108 | $labellength=2; |
109 | print Get_Img_Tag(Graph_Daily(), $title); |
110 | } |
111 | elsif ($type eq 'daysofweek') { |
112 | $labellength=3; |
113 | print Get_Img_Tag(Graph_Weekly(), $title); |
114 | } |
115 | elsif ($type eq 'hours') { |
116 | $labellength=2; |
117 | print Get_Img_Tag(Graph_Hourly(), $title); |
118 | } |
119 | elsif ($type eq 'cluster'){ |
120 | $labellength=32; |
121 | print Get_Img_Tag(Graph_Pie(), $title); |
122 | } |
123 | elsif ($type eq 'filetypes'){ |
124 | $labellength=4; |
125 | print Get_Img_Tag(Graph_Pie(), $title); |
126 | } |
127 | elsif ($type eq 'httpstatus'){ |
128 | $labellength=4; |
129 | print Get_Img_Tag(Graph_Pie(), $title); |
130 | } |
131 | elsif ($type eq 'browsers'){ |
132 | $labellength=32; |
133 | print Get_Img_Tag(Graph_Pie(), $title); |
134 | } |
135 | elsif ($type eq 'downloads'){ |
136 | $labellength=32; |
137 | print Get_Img_Tag(Graph_Pie(), $title); |
138 | } |
139 | elsif ($type eq 'pages'){ |
140 | $labellength=32; |
141 | print Get_Img_Tag(Graph_Pie(), $title); |
142 | } |
143 | elsif ($type eq 'oss'){ |
144 | $labellength=32; |
145 | print Get_Img_Tag(Graph_Pie(), $title); |
146 | } |
147 | elsif ($type eq 'hosts'){ |
148 | $labellength=32; |
149 | print Get_Img_Tag(Graph_Pie(), $title); |
150 | } |
151 | elsif ($type eq 'countries_map'){ |
152 | print Chart_Map(); |
153 | } |
154 | else { |
155 | debug("Unknown type parameter in ShowGraph_graphgooglechartapi function: $title",1); |
156 | #error("Unknown type parameter in ShowGraph_graphgooglechartapi function"); |
157 | } |
158 | |
159 | return 0; |
160 | } |
161 | |
162 | #------------------------------------------------------- |
163 | # PLUGIN FUNCTION: AddHTMLHeader_pluginname |
164 | # UNIQUE: NO |
165 | # Prints javascript includes for Google Visualizations |
166 | # Parameters: None |
167 | # Input: None |
168 | # Output: HTML code for Google Visualizations |
169 | # Return: 0 OK, 1 Error |
170 | #------------------------------------------------------- |
171 | sub AddHTMLHeader_graphgooglechartapi(){ |
172 | print "<script type='text/javascript' src='http://www.google.com/jsapi'></script>\n"; |
173 | } |
174 | |
175 | #------------------------------------------------------- |
176 | # PLUGIN FUNCTION: Graph_Monthly |
177 | # Prints the image code to display a column chart of monthly usage |
178 | # Parameters: None |
179 | # Input: None |
180 | # Output: HTML code to print a chart |
181 | # Return: 0 OK, 1 Error |
182 | #------------------------------------------------------- |
183 | sub Graph_Monthly(){ |
184 | my $chxt = "chxt="; |
185 | my $chxl = "chxl="; |
186 | my $chxs = "chxs="; |
187 | my $chco = "chco="; |
188 | my $chg = "chg="; |
189 | my $chs = "chs="; |
190 | my $cht = "cht=bvg"; |
191 | my $chd = "chd=t:"; |
192 | my $cba = "chbh=a"; # shows the whole month |
193 | my $graphwidth = $imagewidth; |
194 | my $graphheight = int ($imagewidth * $imageratio); |
195 | |
196 | # round max values |
197 | foreach my $i(0..(scalar @$valmax)){ |
198 | @$valmax[$i] = Round_Up(@$valmax[$i]); |
199 | } |
200 | |
201 | # setup axis |
202 | $chxt .= "x,y,y,r"; # add an x for years |
203 | |
204 | # get the month labels |
205 | $chxl .= "0:|"; |
206 | $chxl .= Get_Labels(); |
207 | # get the hits/pages max |
208 | $chxl .= "1:|0|".Get_Suffixed((@$valmax[0]/2),0)."|".Get_Suffixed(@$valmax[0],0)."|"; |
209 | # get the visitors/pages max |
210 | $chxl .= "2:|0|".Get_Suffixed((@$valmax[2]/2),0)."|".Get_Suffixed(@$valmax[2],0)."|"; |
211 | # get bytes |
212 | $chxl .= "3:|0|".Get_Suffixed((@$valmax[4]/2),1)."|".Get_Suffixed(@$valmax[4],1); |
213 | # TODO add the year at the start and end |
214 | |
215 | # set the axis colors |
216 | $chxs .= "1,".@$valcolor[0]."|2,".@$valcolor[2]."|3,".@$valcolor[4]; |
217 | |
218 | # dump colors |
219 | foreach my $i(0..(scalar @$valcolor)){ |
220 | $chco .= @$valcolor[$i]; |
221 | if ($i < (scalar @$valcolor)-1){ $chco .= ",";} |
222 | } |
223 | |
224 | # grid lines |
225 | $chg .= "0,50"; |
226 | |
227 | # size |
228 | $chs .= $graphwidth."x".$graphheight; |
229 | |
230 | # finally get the data |
231 | $chd .= Get_Column_Data(); |
232 | |
233 | # string and dump |
234 | return "$cht&$chxl&$chxt&$chxs&$chco&$chg&$chs&$chd&$cba"; |
235 | } |
236 | |
237 | #------------------------------------------------------- |
238 | # PLUGIN FUNCTION: Graph_Daily |
239 | # Prints the image code to display a column chart of daily usage |
240 | # Parameters: None |
241 | # Input: None |
242 | # Output: HTML code to print a chart |
243 | # Return: 0 OK, 1 Error |
244 | #------------------------------------------------------- |
245 | sub Graph_Daily(){ |
246 | my $chxt = "chxt="; |
247 | my $chxl = "chxl="; |
248 | my $chxs = "chxs="; |
249 | my $chco = "chco="; |
250 | my $chg = "chg="; |
251 | my $chs = "chs="; |
252 | my $cht = "cht=bvg"; |
253 | my $chd = "chd=t:"; |
254 | my $cba = "chbh=a"; # shows the whole month |
255 | my $graphwidth = $imagewidth; |
256 | my $graphheight = int ($imagewidth * $imageratio); |
257 | |
258 | # round max values |
259 | foreach my $i(0..(scalar @$valmax)){ |
260 | @$valmax[$i] = Round_Up(@$valmax[$i]); |
261 | } |
262 | |
263 | # setup axis |
264 | $chxt .= "x,y,y,r"; # add an x for years |
265 | |
266 | # setup axis labels |
267 | # get day labels |
268 | $chxl .= "0:|"; |
269 | $chxl .= Get_Labels(); |
270 | # get the hits/pages max |
271 | $chxl .= "1:|0|".Get_Suffixed((@$valmax[0]/2),0)."|".Get_Suffixed(@$valmax[0],0)."|"; |
272 | # get the visitors/pages max |
273 | $chxl .= "2:|0|".Get_Suffixed((@$valmax[1]/2),0)."|".Get_Suffixed(@$valmax[1],0)."|"; |
274 | # get bytes |
275 | $chxl .= "3:|0|".Get_Suffixed((@$valmax[3]/2),1)."|".Get_Suffixed(@$valmax[3],1); |
276 | # TODO month name |
277 | |
278 | # set the axis colors |
279 | $chxs .= "1,".@$valcolor[0]."|2,".@$valcolor[1]."|3,".@$valcolor[3]; |
280 | |
281 | # dump colors |
282 | foreach my $i(0..(scalar @$valcolor)){ |
283 | $chco .= @$valcolor[$i]; |
284 | if ($i < (scalar @$valcolor)-1){ $chco .= ",";} |
285 | } |
286 | |
287 | # grid lines |
288 | $chg .= "0,50"; |
289 | |
290 | # size |
291 | $chs .= $graphwidth."x".$graphheight; |
292 | |
293 | # finally get the data |
294 | $chd .= Get_Column_Data(); |
295 | |
296 | # string and dump |
297 | return "$cht&$chxl&$chxt&$chxs&$chco&$chg&$chs&$chd&$cba"; |
298 | } |
299 | |
300 | #------------------------------------------------------- |
301 | # PLUGIN FUNCTION: Graph_Weekly |
302 | # Prints the image code to display a column chart of weekly usage |
303 | # Parameters: None |
304 | # Input: None |
305 | # Output: HTML code to print a chart |
306 | # Return: 0 OK, 1 Error |
307 | #------------------------------------------------------- |
308 | sub Graph_Weekly(){ |
309 | my $chxt = "chxt="; |
310 | my $chxl = "chxl="; |
311 | my $chxs = "chxs="; |
312 | my $chco = "chco="; |
313 | my $chg = "chg="; |
314 | my $chs = "chs="; |
315 | my $cht = "cht=bvg"; |
316 | my $chd = "chd=t:"; |
317 | my $cba = "chbh=a"; # shows the whole month |
318 | my $graphwidth = int ($imagewidth * .75); # to maintain old look/ratio, reduce width of the weekly |
319 | my $graphheight = int ($imagewidth * $imageratio); |
320 | |
321 | # round max values |
322 | foreach my $i(0..(scalar @$valmax)){ |
323 | @$valmax[$i] = Round_Up(@$valmax[$i]); |
324 | } |
325 | |
326 | # setup axis |
327 | $chxt .= "x,y,y,r"; # add an x for years |
328 | |
329 | # setup axis labels |
330 | # get the day labels |
331 | $chxl .= "0:|"; |
332 | $chxl .= Get_Labels(); |
333 | # get the hits/pages max |
334 | $chxl .= "1:|0|".Get_Suffixed((@$valmax[0]/2),0)."|".Get_Suffixed(@$valmax[0],0)."|"; |
335 | # get the visitors/pages max |
336 | $chxl .= "2:|0|".Get_Suffixed((@$valmax[1]/2),0)."|".Get_Suffixed(@$valmax[1],0)."|"; |
337 | # get bytes |
338 | $chxl .= "3:|0|".Get_Suffixed((@$valmax[2]/2),1)."|".Get_Suffixed(@$valmax[2],1); |
339 | |
340 | # set the axis colors |
341 | $chxs .= "1,".@$valcolor[0]."|2,".@$valcolor[1]."|3,".@$valcolor[2]; |
342 | |
343 | # dump colors |
344 | foreach my $i(0..(scalar @$valcolor)){ |
345 | $chco .= @$valcolor[$i]; |
346 | if ($i < (scalar @$valcolor)-1){ $chco .= ",";} |
347 | } |
348 | |
349 | # grid lines |
350 | $chg .= "0,50"; |
351 | |
352 | # size |
353 | $chs .= $graphwidth."x".$graphheight; |
354 | |
355 | # finally get the data |
356 | $chd .= Get_Column_Data(); |
357 | |
358 | # string and dump |
359 | return "$cht&$chxl&$chxt&$chxs&$chco&$chg&$chs&$chd&$cba"; |
360 | } |
361 | |
362 | #------------------------------------------------------- |
363 | # PLUGIN FUNCTION: Graph_Hourly |
364 | # Prints the image code to display a column chart of hourly usage |
365 | # Parameters: None |
366 | # Input: None |
367 | # Output: HTML code to print a chart |
368 | # Return: 0 OK, 1 Error |
369 | #------------------------------------------------------- |
370 | sub Graph_Hourly(){ |
371 | my $chxt = "chxt="; |
372 | my $chxl = "chxl="; |
373 | my $chxs = "chxs="; |
374 | my $chco = "chco="; |
375 | my $chg = "chg="; |
376 | my $chs = "chs="; |
377 | my $cht = "cht=bvg"; |
378 | my $chd = "chd=t:"; |
379 | my $cba = "chbh=a"; # shows the whole month |
380 | my $graphwidth = $imagewidth; |
381 | my $graphheight = int ($imagewidth * $imageratio); |
382 | |
383 | # round max values |
384 | foreach my $i(0..(scalar @$valmax - 1)){ |
385 | @$valmax[$i] = Round_Up(@$valmax[$i]); |
386 | } |
387 | |
388 | # setup axis |
389 | $chxt .= "x,y,y,r"; # add an x for years |
390 | |
391 | # setup axis labels |
392 | $chxl .= "0:|"; |
393 | $chxl .= Get_Labels(); |
394 | # get the hits/pages max |
395 | $chxl .= "1:|0|".Get_Suffixed((@$valmax[0]/2),0)."|".Get_Suffixed(@$valmax[0],0)."|"; |
396 | # get the visitors/pages max |
397 | $chxl .= "2:|0|".Get_Suffixed((@$valmax[1]/2),0)."|".Get_Suffixed(@$valmax[1],0)."|"; |
398 | # get bytes |
399 | $chxl .= "3:|0|".Get_Suffixed((@$valmax[2]/2),1)."|".Get_Suffixed(@$valmax[2],1); |
400 | # TODO years |
401 | |
402 | # set the axis colors |
403 | $chxs .= "1,".@$valcolor[0]."|2,".@$valcolor[1]."|3,".@$valcolor[2]; |
404 | |
405 | # dump colors |
406 | foreach my $i(0..(scalar @$valcolor)){ |
407 | $chco .= @$valcolor[$i]; |
408 | if ($i < (scalar @$valcolor)-1){ $chco .= ",";} |
409 | } |
410 | |
411 | # grid lines |
412 | $chg .= "0,50"; |
413 | |
414 | # size |
415 | $chs .= $graphwidth."x".$graphheight; |
416 | |
417 | # finally get the data |
418 | $chd .= Get_Column_Data(); |
419 | |
420 | # string and dump |
421 | return "$cht&$chxl&$chxt&$chxs&$chco&$chg&$chs&$chd&$cba"; |
422 | } |
423 | |
424 | #------------------------------------------------------- |
425 | # PLUGIN FUNCTION: Graph_Pie |
426 | # Prints the image code to display a pie chart of the provided data |
427 | # Parameters: None |
428 | # Input: None |
429 | # Output: HTML code to print a chart |
430 | # Return: 0 OK, 1 Error |
431 | #------------------------------------------------------- |
432 | sub Graph_Pie(){ |
433 | my $chl = "chl="; |
434 | my $chs = "chs="; |
435 | my $chco = "chco="; |
436 | my $cht = "cht=p3"; |
437 | my $chd = "chd=t:"; |
438 | my $graphwidth = $imagewidth; |
439 | my $graphheight = int ($imagewidth * $pieratio); |
440 | |
441 | # get labels |
442 | $chl .= Get_Labels(); |
443 | |
444 | # get data, just read off the array for however many labels we have |
445 | foreach my $i (0..((scalar @$blocklabel)-1)) { |
446 | $chd .= int(@$valdata[$i]); |
447 | $chd .= ($i < ((scalar @$blocklabel)-1) ? "," : ""); |
448 | } |
449 | |
450 | # get color, just the first color passed |
451 | $chco .= @$valcolor[0]; |
452 | |
453 | # set size |
454 | $chs .= $graphwidth."x".$graphheight; |
455 | |
456 | return "$cht&$chs&$chco&$chl&$chd"; |
457 | } |
458 | |
459 | #------------------------------------------------------- |
460 | # PLUGIN FUNCTION: Chart_Map |
461 | # Prints a Javascript and DIV tag to display a Google Visualization GeoMap |
462 | # that uses the Flash plugin to display a map of the world shaded to reflect |
463 | # the provided data |
464 | # Parameters: None |
465 | # Input: None |
466 | # Output: Javascript and DIV tag |
467 | # Return: 0 OK, 1 Error |
468 | #------------------------------------------------------- |
469 | sub Chart_Map(){ |
470 | my $graphwidth = $imagewidth; |
471 | my $graphheight = int ($imagewidth * $mapratio); |
472 | |
473 | # Assume we've already included the proper headers so just call our script inline |
474 | print "\n<script type='text/javascript'>\n"; |
475 | print "google.load('visualization', '1', {'packages': ['geomap']});\n"; |
476 | print "google.setOnLoadCallback(drawMap);\n"; |
477 | print "function drawMap() {\n\tvar data = new google.visualization.DataTable();\n"; |
478 | |
479 | # get the total number of rows |
480 | print "\tdata.addRows(".scalar @$blocklabel.");\n"; |
481 | print "\tdata.addColumn('string', 'Country');\n"; |
482 | print "\tdata.addColumn('number', 'Hits');\n"; |
483 | |
484 | # loop and dump |
485 | my $i = 0; |
486 | for ($i .. (scalar @$blocklabel - 1)) { |
487 | print "\tdata.setValue($i, 0, \"".@$blocklabel[$i]."\");\n"; |
488 | print "\tdata.setValue($i, 1, ".@$valdata[$i].");\n"; |
489 | $i++; |
490 | # Google's Geomap only supports up to 400 entries |
491 | if ($i >= 400){ last; } |
492 | } |
493 | |
494 | print "\tvar options = {};\n"; |
495 | print "\toptions['dataMode'] = 'regions';\n"; |
496 | print "\toptions['width'] = $graphwidth;\n"; |
497 | print "\toptions['height'] = $graphheight;\n"; |
498 | print "\tvar container = document.getElementById('$title');\n"; |
499 | print "\tvar geomap = new google.visualization.GeoMap(container);\n"; |
500 | print "\tgeomap.draw(data, options);\n"; |
501 | print "};\n"; |
502 | print "</script>\n"; |
503 | |
504 | # print the div tag that will contain the map |
505 | print "<div id='$title'></div>\n"; |
506 | return; |
507 | } |
508 | |
509 | #------------------------------------------------------- |
510 | # PLUGIN FUNCTION: Get_Column_Data |
511 | # Loops through the data array and prints a CHD string to send to a Google |
512 | # chart via the API |
513 | # Parameters: None |
514 | # Input: @valcolor, @blocklabel, @valdata, @valmax |
515 | # Output: None |
516 | # Return: A pipe delimited string of data. REQUIRES the "chd=t:" prepended |
517 | #------------------------------------------------------- |
518 | # Returns a string with the CHD data |
519 | sub Get_Column_Data(){ |
520 | my $chd = ""; |
521 | |
522 | # use the # of colors to determine how many values we have |
523 | $x= scalar @$valcolor; |
524 | for ($serie = 0; $serie <= $x; $serie++) { |
525 | foreach my $j (1.. (scalar @$blocklabel)) { |
526 | if ($j > 1) { $chd .= ","; } |
527 | $val = @$valdata[($j-1)*$x+$serie]; |
528 | # convert our values to a percent of max |
529 | $chd .= (@$valmax[$serie] > 0 ? int(($val / Round_Up(@$valmax[$serie])) * 100) : 0); |
530 | } |
531 | if ($serie < $x) { |
532 | $chd .= "|"; |
533 | } |
534 | } |
535 | |
536 | # return |
537 | return $chd; |
538 | } |
539 | |
540 | #------------------------------------------------------- |
541 | # PLUGIN FUNCTION: Get_Labels |
542 | # Returns a CHXL string with labels to send to the Google chart API. Long labels |
543 | # are shortened to $labellength |
544 | # TODO - better shortening method instead of just lopping off the end of strings |
545 | # Parameters: None |
546 | # Input: @blocklabel, $labellength |
547 | # Output: None |
548 | # Return: A pipe delimited string of labels. REQUIRES the "chxl=" prepended |
549 | #------------------------------------------------------- |
550 | sub Get_Labels(){ |
551 | my $chxl = ""; |
552 | foreach my $i (1..(scalar @$blocklabel)) { |
553 | $temp = @$blocklabel[$i-1]; |
554 | if (length($temp) > $labellength){ |
555 | $temp = (substr($temp,0,$labellength)); |
556 | } |
557 | $chxl .= "$temp|"; |
558 | } |
559 | $chxl =~ s/&//; |
560 | return $chxl; |
561 | } |
562 | |
563 | #------------------------------------------------------- |
564 | # PLUGIN FUNCTION: Round_Up |
565 | # Rounds a number up to the next most significant digit, i.e. 1234 becomes 2000 |
566 | # Useful for getting the max values of our graph |
567 | # Parameters: $num |
568 | # Input: None |
569 | # Output: None |
570 | # Return: The rounded number |
571 | #------------------------------------------------------- |
572 | sub Round_Up(){ |
573 | my $num = shift; |
574 | $num = int($num); |
575 | if ($num < 1){ return $num; } |
576 | |
577 | # under 100, just increment and dump |
578 | if ($num < 100){return $num++; } |
579 | |
580 | $i = int(substr($num,0,2))+1; |
581 | |
582 | # pad with 0s |
583 | $l = length($i); |
584 | while ($l<(length($num))){ |
585 | $i .= "0"; |
586 | $l++; |
587 | } |
588 | return $i; |
589 | } |
590 | |
591 | #------------------------------------------------------- |
592 | # PLUGIN FUNCTION: Get_Suffixed |
593 | # Converts a number for axis labels and appends the scientific notation suffix |
594 | # or proper size in bytes |
595 | # Parameters: $num |
596 | # Input: @Message array from AWStats |
597 | # Output: None |
598 | # Return: A number with suffix, i.e. 400 MB or 200 K |
599 | #------------------------------------------------------- |
600 | sub Get_Suffixed(){ |
601 | my $num = shift || 0; |
602 | my $isbytes = shift || 0; |
603 | my $float = 0; |
604 | if ( $num >= ( 1 << 30 ) ) { |
605 | $float = (split(/\./, $num / 1000000000))[1]; |
606 | if ($float){ |
607 | return sprintf( "%.1f", $num / 1000000000 ) . ($isbytes ? " $Message[110]" : " B"); |
608 | }else{ |
609 | return sprintf( "%.0f", $num / 1000000000 ) . ($isbytes ? " $Message[110]" : " B"); |
610 | } |
611 | } |
612 | if ( $num >= ( 1 << 20 ) ) { |
613 | $float = (split(/\./, $num / 1000000))[1]; |
614 | if ($float){ |
615 | return sprintf( "%.1f", $num / 1000000 ) . ($isbytes ? " $Message[109]" : " M"); |
616 | }else{ |
617 | return sprintf( "%.0f", $num / 1000000 ) . ($isbytes ? " $Message[109]" : " M"); |
618 | } |
619 | } |
620 | if ( $num >= ( 1 << 10 ) ) { |
621 | $float = (split(/\./, $num / 1000))[1]; |
622 | if ($float){ |
623 | return sprintf( "%.1f", $num / 1000 ) . ($isbytes ? " $Message[108]" : " K"); |
624 | }else{ |
625 | return sprintf( "%.0f", $num / 1000 ) . ($isbytes ? " $Message[108]" : " K"); |
626 | } |
627 | } |
628 | return int($num); |
629 | } |
630 | |
631 | #------------------------------------------------------- |
632 | # PLUGIN FUNCTION: Get_Img_Tag |
633 | # Builds the full IMG tag to place in HTML that will call the Google Charts API |
634 | # Parameters: $params, $title |
635 | # Input: $ChartProtocol, $ChartURI, $ChartIndex |
636 | # Output: None |
637 | # Return: An HTML IMG tag |
638 | #------------------------------------------------------- |
639 | sub Get_Img_Tag(){ |
640 | my $params = shift || ""; |
641 | my $title = shift || ""; |
642 | my $tag = "<img src=\"$ChartProtocol"; |
643 | # for optimization, we can prepend a number to the host address and google will |
644 | # use different servers to generate our images. This is important if we have multiple |
645 | # images on the same page |
646 | # TODO - debug why chart index isn't working as Google says it should |
647 | # if ($URLIndex < 0){ |
648 | $tag .= "$ChartURI"; |
649 | # }else{ |
650 | # $tag .= "$ChartIndex.$ChartURI"; |
651 | # } |
652 | $ChartIndex = ($ChartIndex >= 9 ? 0 : $ChartIndex + 1); |
653 | $tag .= $params; |
654 | $tag .= "\" alt=\"$title\"/>"; |
655 | } |
656 | |
657 | 1; # Do not remove this line |