PageRenderTime 56ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/core/graph.inc.php

https://github.com/axxtel/agilebill
PHP | 405 lines | 249 code | 78 blank | 78 comment | 32 complexity | e4f13aacd56e13c66494dfcf7ca0487d MD5 | raw file
  1. <?php
  2. /**
  3. * AgileBill - Open Billing Software
  4. *
  5. * This body of work is free software; you can redistribute it and/or
  6. * modify it under the terms of the Open AgileBill License
  7. * License as published at http://www.agileco.com/agilebill/license1-4.txt
  8. *
  9. * For questions, help, comments, discussion, etc., please join the
  10. * Agileco community forums at http://forum.agileco.com/
  11. *
  12. * @link http://www.agileco.com/
  13. * @copyright 2004-2008 Agileco, LLC.
  14. * @license http://www.agileco.com/agilebill/license1-4.txt
  15. * @author Tony Landis <tony@agileco.com>
  16. * @package AgileBill
  17. * @version 1.4.93
  18. */
  19. class CORE_graph
  20. {
  21. ########################################################################
  22. ### Create a Bar Graph
  23. ########################################################################
  24. function PIE_graph($module, $method, $type, $start, $extra_fields)
  25. {
  26. global $C_translate, $C_auth;
  27. include_once(PATH_CORE . 'validate.inc.php');
  28. $dt = new CORE_validate;
  29. include (PATH_GRAPH."jpgraph.php");
  30. ####################################################################
  31. ### Check if 'search' is authorized for this account
  32. ####################################################################
  33. # check the validation for this function
  34. if($C_auth->auth_method_by_name($module,'search'))
  35. {
  36. # validate this file exists, and include it.
  37. if (file_exists(PATH_MODULES . '/' . $module . '/' . $module . '.inc.php'))
  38. {
  39. include_once(PATH_MODULES . '/' . $module . '/' . $module . '.inc.php');
  40. }
  41. else
  42. {
  43. ### Not exist!
  44. $error = $C_translate->translate('module_non_existant','','');
  45. }
  46. }
  47. else
  48. {
  49. ### Not auth
  50. $error = $C_translate->translate('module_non_auth','','');
  51. }
  52. if(isset($error))
  53. {
  54. include (PATH_GRAPH."jpgraph_canvas.php");
  55. // Create the graph.
  56. $graph = new CanvasGraph(460,55,"auto");
  57. $t1 = new Text($error);
  58. $t1->Pos(0.2,0.5);
  59. $t1->SetOrientation("h");
  60. $t1->SetBox("white","black",'gray');
  61. $t1->SetFont(FF_FONT1,FS_NORMAL);
  62. $t1->SetColor("black");
  63. $graph->AddText($t1);
  64. $graph->Stroke();
  65. exit;
  66. }
  67. # initialize the module, if it has not already been initialized
  68. $eval = '$' . $module . ' = new ' . $module . '; ';
  69. $eval .= '$this_Obj = $' . $module . ';';
  70. eval ($eval);
  71. # run the function
  72. $array = call_user_func (array($module, $method), $start_str, $end_str, $constraint_array, $default_array, $extra_fields);
  73. include (PATH_GRAPH."jpgraph_pie.php");
  74. include (PATH_GRAPH."jpgraph_pie3d.php");
  75. $data = $array['data'];
  76. $legends = $array['legends'];
  77. // Create the Pie Graph.
  78. $graph = new PieGraph(500,250,"auto");
  79. $graph->SetScale("textlin");
  80. $graph->SetMarginColor('#F9F9F9');
  81. $graph->SetFrame(true,'#FFFFFF',0);
  82. $graph->SetColor('#F9F9F9');
  83. // Create pie plot
  84. $p1 = new PiePlot3d($data);
  85. $p1->SetTheme("water");
  86. $p1->SetCenter(0.4);
  87. $p1->SetAngle(30);
  88. $p1->value->SetFont(FF_FONT1,FS_NORMAL,8);
  89. $p1->SetLegends($legends);
  90. // Explode the larges slice:
  91. $largest = 0;
  92. for($i=0; $i<count($data); $i++)
  93. {
  94. if ($data[$i] > $largest)
  95. {
  96. $largest = $data[$i];
  97. $explode = $i;
  98. }
  99. }
  100. if($explode)
  101. $p1->ExplodeSlice($explode);
  102. $graph->Add($p1);
  103. $graph->Stroke();
  104. }
  105. ########################################################################
  106. ### Create a Bar Graph
  107. ########################################################################
  108. function BAR_graph($module, $type, $start, $extra_fields)
  109. {
  110. global $C_translate, $C_auth;
  111. include_once(PATH_CORE . 'validate.inc.php');
  112. $dt = new CORE_validate;
  113. include (PATH_GRAPH."jpgraph.php");
  114. ####################################################################
  115. ### Check if 'search' is authorized for this account
  116. ####################################################################
  117. # check the validation for this function
  118. if($C_auth->auth_method_by_name($module,'search'))
  119. {
  120. # validate this file exists, and include it.
  121. if (file_exists(PATH_MODULES . '/' . $module . '/' . $module . '.inc.php'))
  122. {
  123. include_once(PATH_MODULES . '/' . $module . '/' . $module . '.inc.php');
  124. }
  125. else
  126. {
  127. ### Not exist!
  128. $error = $C_translate->translate('module_non_existant','','');
  129. }
  130. }
  131. else
  132. {
  133. ### Not auth
  134. $error = $C_translate->translate('module_non_auth','','');
  135. }
  136. if(isset($error))
  137. {
  138. include (PATH_GRAPH."jpgraph_canvas.php");
  139. // Create the graph.
  140. $graph = new CanvasGraph(460,55,"auto");
  141. $t1 = new Text($error);
  142. $t1->Pos(0.2,0.5);
  143. $t1->SetOrientation("h");
  144. $t1->SetBox("white","black",'gray');
  145. $t1->SetFont(FF_FONT1,FS_NORMAL);
  146. $t1->SetColor("black");
  147. $graph->AddText($t1);
  148. $graph->Stroke();
  149. exit;
  150. }
  151. ####################################################################
  152. ### BY WEEK
  153. ####################################################################
  154. if ($type == 'week')
  155. {
  156. $FONT_SIZE= 7;
  157. $AbsWidth = 12;
  158. $interval = 4;
  159. $type = $C_translate->translate('week','','');
  160. if($start == "" || $start <= 12)
  161. {
  162. ## Get the beginning/end of this week
  163. $start_str = mktime (24,0,0,12, 31, date("Y")-1);
  164. $start = date(UNIX_DATE_FORMAT, $start_str);
  165. $end_str = mktime (24,0,0,12, 30, date("Y",$start_str));
  166. $end = date(UNIX_DATE_FORMAT, $end_str);
  167. }
  168. else
  169. {
  170. ## Get the beginning/end of the specified week
  171. $start_str = mktime (24,0,0,12, 31, date("$start")-1);
  172. $start = date(UNIX_DATE_FORMAT, $start_str);
  173. $end_str = mktime (24,0,0,12, 30, date("Y",$start_str));
  174. $end = date(UNIX_DATE_FORMAT, $end_str);
  175. }
  176. ### Set the constraint array:
  177. $curr_str = $start_str;
  178. while ( $curr_str <= $end_str )
  179. {
  180. $new_curr_str = mktime (0,0,0,date("m",$curr_str), date("d",$curr_str)+7, date("Y",$curr_str));
  181. $constraint_array[] = Array('start' => $curr_str, 'end' => $new_curr_str);
  182. $curr_str = $new_curr_str;
  183. $default_array[] = 0;
  184. }
  185. }
  186. ####################################################################
  187. ### MONTH
  188. ####################################################################
  189. else if ($type == 'month')
  190. {
  191. $FONT_SIZE = 10;
  192. $AbsWidth = 12;
  193. $TickLables = $gDateLocale->GetShortMonth();
  194. $interval =1;
  195. $type = $C_translate->translate('month','','');
  196. if($start == "" || $start < 12)
  197. {
  198. ## Get the beginning/end of this week
  199. $start_str = mktime (24,0,0,12, 31, date("Y")-1);
  200. $start = date(UNIX_DATE_FORMAT, $start_str);
  201. $end_str = mktime (24,0,0,12, 30, date("Y",$start_str));
  202. $end = date(UNIX_DATE_FORMAT, $end_str);
  203. }
  204. else
  205. {
  206. ## Get the beginning/end of the specified week
  207. ## Get the beginning/end of this week
  208. $start_str = mktime (24,0,0,12, 31, date("$start")-1);
  209. $start = date(UNIX_DATE_FORMAT, $start_str);
  210. $end_str = mktime (24,0,0,12, 30, date("Y",$start_str));
  211. $end = date(UNIX_DATE_FORMAT, $end_str);
  212. }
  213. ### Set the constraint array:
  214. $curr_str = $start_str;
  215. while ( $curr_str <= $end_str )
  216. {
  217. $new_curr_str = mktime (0,0,0,date("m",$curr_str)+1, date("d",$curr_str), date("Y",$curr_str));
  218. $constraint_array[] = Array('start' => $curr_str, 'end' => $new_curr_str);
  219. $curr_str = $new_curr_str;
  220. $default_array[] = 0;
  221. }
  222. }
  223. ####################################################################
  224. ### BY YEAR
  225. ####################################################################
  226. else if ($type == 'year')
  227. {
  228. $FONT_SIZE = 10;
  229. $interval =1;
  230. $AbsWidth = 13;
  231. $type = $C_translate->translate('year','','');
  232. ## Get the beginning/end of this year - 10
  233. $start_str = mktime (0,0,0,1,1, date("Y")-9);
  234. $start = date(UNIX_DATE_FORMAT, $start_str);
  235. $end_str = mktime (0,0,0,12,30, date("Y",$start_str)+9);
  236. $end = date(UNIX_DATE_FORMAT, $end_str);
  237. ### Set the constraint array:
  238. $curr_str = $start_str;
  239. while ( $curr_str <= $end_str )
  240. {
  241. $new_curr_str = mktime (0,0,0,date("m",$curr_str), date("d",$curr_str), date("Y",$curr_str)+1);
  242. $constraint_array[] = Array('start' => $curr_str, 'end' => $new_curr_str);
  243. $TickLables[] = date("Y",$curr_str);
  244. $curr_str = $new_curr_str;
  245. $default_array[] = 0;
  246. }
  247. }
  248. ####################################################################
  249. ### BY DAY
  250. ####################################################################
  251. else
  252. {
  253. $FONT_SIZE= 8;
  254. $interval = 3;
  255. $AbsWidth = 4;
  256. $type = $C_translate->translate('day','','');
  257. if($start == "" || $start > 12 || $start < 1 )
  258. {
  259. ## Get the beginning/end of this week
  260. $start_str = mktime (0,0,0,date("m"), 1, date("Y"));
  261. $start = date(UNIX_DATE_FORMAT, $start_str);
  262. $end_str = mktime (0,0,0,date("m",$start_str)+1, 1, date("Y",$start_str));
  263. $end = date(UNIX_DATE_FORMAT, $end_str);
  264. }
  265. else
  266. {
  267. ## Get the beginning/end of the specified week
  268. $start_str = mktime (0,0,0,date("$start"), 1, date("Y"));
  269. $start = date(UNIX_DATE_FORMAT, $start_str);
  270. $end_str = mktime (0,0,0,date("m",$start_str)+1, 1, date("Y",$start_str));
  271. $end = date(UNIX_DATE_FORMAT, $end_str);
  272. }
  273. ### Set the constraint array:
  274. $curr_str = $start_str;
  275. while ( $curr_str < $end_str )
  276. {
  277. $new_curr_str = mktime (0,0,0,date("m",$curr_str), date("d",$curr_str)+1, date("Y",$curr_str));
  278. $constraint_array[] = Array('start' => $curr_str, 'end' => $new_curr_str);
  279. $TickLables[] = date("j",$curr_str);
  280. $curr_str = $new_curr_str;
  281. $default_array[] = 0;
  282. }
  283. }
  284. # initialize the module, if it has not already been initialized
  285. $eval = '$' . $module . ' = new ' . $module . '; ';
  286. $eval .= '$this_Obj = $' . $module . ';';
  287. eval ($eval);
  288. # run the function
  289. $array = call_user_func (array($module, "graph"), $start_str, $end_str, $constraint_array, $default_array, $extra_fields);
  290. include (PATH_GRAPH."jpgraph_bar.php");
  291. $datay=$array['results'];
  292. // Create the graph. These two calls are always required
  293. $graph = new Graph(550,200,"auto");
  294. $graph->SetScale("textlin");
  295. $graph->yaxis->scale->SetGrace(25);
  296. $graph->SetMarginColor('#F9F9F9');
  297. $graph->SetFrame(true,'darkgreen',0);
  298. $graph->SetColor('#FFFFFF');
  299. #$graph->SetFrame(false);
  300. // Adjust the margin a bit to make more room for titles
  301. $graph->img->SetMargin(45,10,15,25);
  302. // Create a bar pot
  303. $bplot = new BarPlot($datay);
  304. // Set the X
  305. if(isset($TickLables))
  306. $graph->xaxis->SetTickLabels($TickLables);
  307. $graph->xaxis->SetTextLabelInterval($interval);
  308. $graph->xaxis->SetColor("navy");
  309. $graph->yaxis->SetColor("navy");
  310. // Adjust fill color
  311. $bplot->SetFillColor('#506DC7');
  312. $bplot->value->Show();
  313. $bplot->value->SetFont(FF_FONT1,FS_NORMAL,$FONT_SIZE);
  314. $bplot->value->SetAngle(90);
  315. $bplot->value->SetFormat('%0.0f');
  316. $bplot->value->SetColor("darkblue");
  317. $graph->Add($bplot);
  318. // Setup the titles
  319. $title = $array['title'];
  320. $graph->title->Set($title . " $start - $end");
  321. $graph->title->SetFont(FF_FONT1,FS_BOLD);
  322. $graph->title->SetColor("black");
  323. // Display the graph
  324. $graph->Stroke();
  325. }
  326. }
  327. ?>