PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/webfrontend/www/graphs.inc.php

https://github.com/mobilipia/IPTV-Analyzer
PHP | 295 lines | 169 code | 53 blank | 73 comment | 20 complexity | c70d9b2923f76b62b9b6963ba2744bee MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * Common graph functions.
  4. *
  5. * Mostly based upon PEAR module Image_Graph.
  6. *
  7. */
  8. require_once 'Image/Graph.php';
  9. require_once 'Image/Canvas.php';
  10. ######
  11. # Helper function for calculating the width for the "bar" (for graphs
  12. # using the Bar Plot), which is a percentage,
  13. #
  14. # The width is adjusted with: $Plot->setBarWidth($res, "%")
  15. #
  16. function calcBarWidthPercentage($bucketsz, $tstampF, $tstampT, $records=NULL)
  17. {
  18. $min_bucketsz = 10;
  19. $percent = 1.0;
  20. # Calc based on the number of data records
  21. if (is_numeric($records) && $records > 0) {
  22. $percent = 100 / $records;
  23. }
  24. # Calc based on the timeperiod and bucket size
  25. if (is_numeric($bucketsz) && $bucketsz > 0) {
  26. $period = $tstampT - $tstampF;
  27. if ($period > 0) {
  28. if ($bucketsz < $min_bucketsz)
  29. $bucketsz = $min_bucketsz;
  30. // Calc the max possible date records
  31. $max_records = $period / $bucketsz;
  32. if ($max_records > 0 && $max_records > $records) {
  33. // This is often the case
  34. $percent = 100 / $max_records;
  35. }
  36. }
  37. }
  38. return $percent;
  39. }
  40. function setBarWidth(& $Plot, $bucketsz, $tstampF, $tstampT, $records=NULL)
  41. {
  42. $percent = calcBarWidthPercentage($bucketsz, $tstampF, $tstampT, $records);
  43. $Plot->setBarWidth($percent,"%");
  44. }
  45. ######
  46. # Generate graph names
  47. function generateFilename($prefix, & $input, $suffix = 'png')
  48. {
  49. $str_parts=array();
  50. if (!is_array($input)) {
  51. $err = "Cannot generate a filename, \$input must be an array";
  52. trigger_error("$err", E_USER_ERROR);
  53. }
  54. if(isset($input['probeid'])) {
  55. $str_parts[]= 'probeid'.$input['probeid'];
  56. }
  57. if(isset($input['tstampF'])) {
  58. $str_parts[]= 'tstampF'.$input['tstampF'];
  59. }
  60. if(isset($input['tstampT'])) {
  61. $str_parts[]= 'tstampT'.$input['tstampT'];
  62. }
  63. if(isset($input['bucketsz'])) {
  64. $str_parts[]= 'bucketsz'.$input['bucketsz'];
  65. }
  66. if(isset($input['maxy'])) {
  67. $str_parts[]= 'maxy'.$input['maxy'];
  68. }
  69. $generated_string = implode('_',$str_parts);
  70. $name = $prefix . "__" . $generated_string .".$suffix";
  71. return $name;
  72. }
  73. ######
  74. # Creating the graph elements
  75. function create_graph_usemap01($width=700, $height=160, $fontsize=7)
  76. {
  77. $Canvas =& Image_Canvas::factory('png',
  78. array('width' => $width,
  79. 'height' => $height,
  80. 'usemap' => true));
  81. // This is how you get the ImageMap object,
  82. // fx. to save map to file (using toHtml())
  83. $Imagemap = $Canvas->getImageMap();
  84. // Create the graph
  85. //$Graph =& Image_Graph::factory('graph', array(600, 140));
  86. $Graph =& Image_Graph::factory('graph', $Canvas);
  87. // add a TrueType font
  88. //$myfont = '/usr/share/fonts/truetype/freefont/FreeSans.ttf';
  89. $myfont = '/usr/share/fonts/truetype/freefont/FreeSerif.ttf';
  90. $Font =& $Graph->addNew('font', $myfont);
  91. //$Font =& $Graph->addNew('font', 'Verdana');
  92. //$Font =& $Graph->addNew('font', 'Helvetica');
  93. // set the font size
  94. $Font->setSize($fontsize);
  95. $Graph->setFont($Font);
  96. #return array(&$Graph, &$Font);
  97. return $Graph;
  98. }
  99. function create_plotarea_with_title01(&$Graph, &$Font, $title)
  100. {
  101. /* How element are connected is a bit hard to understand, please read:
  102. http://pear.veggerby.dk/wiki/
  103. image_graph:getting_started_guide#creating_the_building_blocks
  104. */
  105. /*
  106. $Graph->add(
  107. Image_Graph::vertical(
  108. Image_Graph::factory('title', array("$title", 10)),
  109. Image_Graph::vertical(
  110. $Plotarea = Image_Graph::factory('plotarea'),
  111. $Legend = Image_Graph::factory('legend'),
  112. 90
  113. ),
  114. 5
  115. )
  116. );
  117. $Legend->setPlotarea($Plotarea);
  118. */
  119. /* Here we start with the Title and add the Plotarea to the Title,
  120. its might seem a little odd, but it works...
  121. */
  122. $Title = $Graph->addNew('title', array("$title", 10));
  123. $Plotarea = $Title->addNew('plotarea');
  124. $Plotarea->setFont($Font);
  125. return $Plotarea;
  126. }
  127. function create_plotarea_with_title02(&$Graph, &$Font, $title)
  128. {
  129. $Title = $Graph->addNew('title', array("$title", 10));
  130. /* Notice the Axis settings on plotarea */
  131. $Plotarea = $Title->addNew('plotarea', array('axis', 'axis'));
  132. $Plotarea->setFont($Font);
  133. return $Plotarea;
  134. }
  135. function data_addPoints01(& $Dataset, & $data, $droptype, $max_y_value, $urldata)
  136. {
  137. //Data comes from: $data = probe_data_query($probeid);
  138. $cnt=0;
  139. # LOOP: addPoints
  140. foreach ($data as $row) {
  141. $cnt++;
  142. #$date = $row['datoen'];
  143. $date = $row['timestamp'];
  144. $skips = $row['skips'];
  145. $drops = $row['drops'];
  146. $value = $row["$droptype"];
  147. $timemin = $row['timemin'];
  148. $timemax = $row['timemax'];
  149. $period = $timemax - $timemin;
  150. $records = $row['records'];
  151. # "title" info in html map
  152. $title = "$droptype:$value";
  153. $title .= " hour:" . date("H:i:s", $date);
  154. $title .= " (day:" . date("j.M", $date) . ")";
  155. $title .= " period:{$period}s";
  156. if (isset($records)) {
  157. $title .= " records:$records";
  158. }
  159. //$title = "drops:$drops skips:$skips";
  160. //echo "TEST: value:$value $title<br>\n";
  161. # Color code if data value exceed max allowed data value
  162. $colorid = 'DROPS';
  163. if ($value > $max_y_value) {
  164. $value = $max_y_value;
  165. $title .= " EXCESSIVE";
  166. $colorid = 'EXCESS';
  167. }
  168. # Create an URL
  169. $bucketsz = $urldata['bucketsz'];
  170. $probeid = $urldata['probeid'];
  171. $channel = $urldata['channel'];
  172. #echo "bucketsz:$bucketsz ";
  173. #echo "timemin:$timemin ";
  174. #echo "timemax:$timemax ";
  175. # For the link adjust $timemin/max, e.g. sub/add bucketsz period
  176. if (is_numeric($bucketsz)) {
  177. $timemin -= floor($bucketsz / 2);
  178. $timemax += floor($bucketsz / 2);
  179. }
  180. #echo "timemin2:$timemin ";
  181. #echo "timemax2:$timemax<br>\n";
  182. # Calc new bucketsz, by saying how many elements I want on the graph
  183. $wanted_elements = 50;
  184. $zoom_period = $timemax - $timemin;
  185. $newsz = floor($zoom_period / $wanted_elements);
  186. if ($newsz < 10) {
  187. $newsz = 10;
  188. }
  189. $URL = "?tstampF=$timemin&tstampT=$timemax"
  190. . "&probeid=$probeid"
  191. . "&bucketsz=" . $newsz;
  192. if (isset($channel)) {
  193. $URL .= "&channel=$channel";
  194. }
  195. # Add value points to the dataset
  196. $Dataset->addPoint($date, $value,
  197. array(
  198. 'id' => "$colorid",
  199. 'url' => "$URL",
  200. 'alt' => $value,
  201. 'target' => '_self',
  202. 'htmltags' => array('title' => $title)
  203. )
  204. );
  205. }
  206. return $cnt;
  207. }
  208. function data_addPoints_fake(& $Dataset, $bucketsz,
  209. $tstampF, $tstampT, $urldata=NULL)
  210. {
  211. $cnt=0;
  212. # LOOP: addPoints
  213. for ($i = $tstampF; $i < $tstampT; $i = $i + $bucketsz) {
  214. $cnt++;
  215. $date = $i;
  216. $value = 1;
  217. # "title" info in html map
  218. $title = "no drops";
  219. $title .= " hour:" . date("H:i:s", $date);
  220. $title .= " (day:" . date("j.M", $date) . ")";
  221. # Color code
  222. $colorid = 'NODROPS';
  223. # Create an URL
  224. $bucketsz = $urldata['bucketsz'];
  225. $probeid = $urldata['probeid'];
  226. $URL = "?tstampF=$tstampF&tstampT=$tstampT"
  227. . "&probeid=$probeid&bucketsz="
  228. . $bucketsz;
  229. # Add value points to the dataset
  230. $Dataset->addPoint($date, $value,
  231. array(
  232. 'id' => "$colorid",
  233. 'url' => "$URL",
  234. 'alt' => $value,
  235. 'target' => '_self',
  236. 'htmltags' => array('title' => $title)
  237. )
  238. );
  239. }
  240. return $cnt;
  241. }
  242. ?>