PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/pgfouine-1.2/include/reporting/artichow/php4/Graph.class.php

#
PHP | 373 lines | 171 code | 96 blank | 106 comment | 17 complexity | 51531f78768a4aa3c9dd6414f1197d3e MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0
  1. <?php
  2. /*
  3. * This work is hereby released into the Public Domain.
  4. * To view a copy of the public domain dedication,
  5. * visit http://creativecommons.org/licenses/publicdomain/ or send a letter to
  6. * Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
  7. *
  8. */
  9. // Artichow configuration
  10. // Some useful files
  11. require_once ARTICHOW."/common.php";
  12. require_once ARTICHOW."/Component.class.php";
  13. require_once ARTICHOW."/Image.class.php";
  14. require_once ARTICHOW."/inc/Grid.class.php";
  15. require_once ARTICHOW."/inc/Tools.class.php";
  16. require_once ARTICHOW."/inc/Drawer.class.php";
  17. require_once ARTICHOW."/inc/Math.class.php";
  18. require_once ARTICHOW."/inc/Tick.class.php";
  19. require_once ARTICHOW."/inc/Axis.class.php";
  20. require_once ARTICHOW."/inc/Legend.class.php";
  21. require_once ARTICHOW."/inc/Mark.class.php";
  22. require_once ARTICHOW."/inc/Label.class.php";
  23. require_once ARTICHOW."/inc/Text.class.php";
  24. require_once ARTICHOW."/inc/Color.class.php";
  25. require_once ARTICHOW."/inc/Font.class.php";
  26. require_once ARTICHOW."/inc/Gradient.class.php";
  27. // Catch all errors
  28. ob_start();
  29. /**
  30. * A graph
  31. *
  32. * @package Artichow
  33. */
  34. class awGraph extends awImage {
  35. /**
  36. * Graph name
  37. *
  38. * @var string
  39. */
  40. var $name;
  41. /**
  42. * Cache timeout
  43. *
  44. * @var int
  45. */
  46. var $timeout = 0;
  47. /**
  48. * Graph timing ?
  49. *
  50. * @var bool
  51. */
  52. var $timing;
  53. /**
  54. * Components
  55. *
  56. * @var array
  57. */
  58. var $components = array();
  59. /**
  60. * Some labels to add to the component
  61. *
  62. * @var array
  63. */
  64. var $labels = array();
  65. /**
  66. * Graph title
  67. *
  68. * @var Label
  69. */
  70. var $title;
  71. /**
  72. * Construct a new graph
  73. *
  74. * @param int $width Graph width
  75. * @param int $height Graph height
  76. * @param string $name Graph name for the cache (must be unique). Let it null to not use the cache.
  77. * @param int $timeout Cache timeout (unix timestamp)
  78. */
  79. function awGraph($width = NULL, $height = NULL, $name = NULL, $timeout = 0) {
  80. parent::awImage();
  81. $this->setSize($width, $height);
  82. $this->name = $name;
  83. $this->timeout = $timeout;
  84. // Clean sometimes all the cache
  85. if(mt_rand(0, 5000) === 0) {
  86. awGraph::cleanCache();
  87. }
  88. if($this->name !== NULL) {
  89. $file = ARTICHOW."/cache/".$this->name."-time";
  90. if(is_file($file)) {
  91. $type = awGraph::cleanGraphCache($file);
  92. if($type === NULL) {
  93. awGraph::deleteFromCache($this->name);
  94. } else {
  95. header("Content-Type: image/".$type);
  96. readfile(ARTICHOW."/cache/".$this->name."");
  97. exit;
  98. }
  99. }
  100. }
  101. $this->title = new awLabel(
  102. NULL,
  103. new awDejaVuSans(16),
  104. NULL,
  105. 0
  106. );
  107. $this->title->setAlign(LABEL_CENTER, LABEL_BOTTOM);
  108. }
  109. /**
  110. * Delete a graph from the cache
  111. *
  112. * @param string $name Graph name
  113. * @return bool TRUE on success, FALSE on failure
  114. */
  115. function deleteFromCache($name) {
  116. if(is_file(ARTICHOW."/cache/".$name."-time")) {
  117. unlink(ARTICHOW."/cache/".$name."");
  118. unlink(ARTICHOW."/cache/".$name."-time");
  119. }
  120. }
  121. /**
  122. * Delete all graphs from the cache
  123. */
  124. function deleteAllCache() {
  125. $dp = opendir(ARTICHOW."/cache");
  126. while($file = readdir($dp)) {
  127. if($file !== '.' and $file != '..') {
  128. unlink(ARTICHOW."/cache/".$file);
  129. }
  130. }
  131. }
  132. /**
  133. * Clean cache
  134. */
  135. function cleanCache() {
  136. $glob = glob(ARTICHOW."/cache/*-time");
  137. foreach($glob as $file) {
  138. $type = awGraph::cleanGraphCache($file);
  139. if($type === NULL) {
  140. $name = ereg_replace(".*/(.*)\-time", "\\1", $file);
  141. awGraph::deleteFromCache($name);
  142. }
  143. }
  144. }
  145. /**
  146. * Enable/Disable Graph timing
  147. *
  148. * @param bool $timing
  149. */
  150. function setTiming($timing) {
  151. $this->timing = (bool)$timing;
  152. }
  153. /**
  154. * Add a component to the graph
  155. *
  156. * @param &$component
  157. */
  158. function add(&$component) {
  159. $this->components[] = $component;
  160. }
  161. /**
  162. * Add a label to the component
  163. *
  164. * @param &$label
  165. * @param int $x Position on X axis of the center of the text
  166. * @param int $y Position on Y axis of the center of the text
  167. */
  168. function addLabel(&$label, $x, $y) {
  169. $this->labels[] = array(
  170. $label, $x, $y
  171. );
  172. }
  173. /**
  174. * Add a label to the component with aboslute position
  175. *
  176. * @param &$label
  177. * @param $point Text position
  178. */
  179. function addAbsLabel(&$label, $point) {
  180. $this->labels[] = array(
  181. $label, $point
  182. );
  183. }
  184. /**
  185. * Build the graph and draw component on it
  186. * Image is sent to the user browser
  187. *
  188. * @param string $file Save the image in the specified file. Let it null to print image to screen.
  189. */
  190. function draw($file = NULL) {
  191. if($this->timing) {
  192. $time = microtimeFloat();
  193. }
  194. $this->create();
  195. foreach($this->components as $component) {
  196. $this->drawComponent($component);
  197. }
  198. $this->drawTitle();
  199. $this->drawShadow();
  200. $this->drawLabels();
  201. if($this->timing) {
  202. $this->drawTiming(microtimeFloat() - $time);
  203. }
  204. $this->send($file);
  205. if($file === NULL) {
  206. $data = ob_get_contents();
  207. if($this->name !== NULL) {
  208. if(is_writable(ARTICHOW."/cache") === FALSE) {
  209. trigger_error("Cache directory is not writable");
  210. }
  211. $file = ARTICHOW."/cache/".$this->name."";
  212. file_put_contents($file, $data);
  213. $file .= "-time";
  214. file_put_contents($file, $this->timeout."\n".$this->getFormat());
  215. }
  216. }
  217. }
  218. function drawLabels() {
  219. $drawer = $this->getDrawer();
  220. foreach($this->labels as $array) {
  221. if(count($array) === 3) {
  222. // Text in relative position
  223. list($label, $x, $y) = $array;
  224. $point = new awPoint(
  225. $x * $this->width,
  226. $y * $this->height
  227. );
  228. } else {
  229. // Text in absolute position
  230. list($label, $point) = $array;
  231. }
  232. $label->draw($drawer, $point);
  233. }
  234. }
  235. function drawTitle() {
  236. $drawer = $this->getDrawer();
  237. $point = new awPoint(
  238. $this->width / 2,
  239. 10
  240. );
  241. $this->title->draw($drawer, $point);
  242. }
  243. function drawTiming($time) {
  244. $drawer = $this->getDrawer();
  245. $label = new awLabel;
  246. $label->set("(".sprintf("%.3f", $time)." s)");
  247. $label->setAlign(LABEL_LEFT, LABEL_TOP);
  248. $label->border->show();
  249. $label->setPadding(1, 0, 0, 0);
  250. $label->setBackgroundColor(new awColor(230, 230, 230, 25));
  251. $label->draw($drawer, new awPoint(5, $drawer->height - 5));
  252. }
  253. function cleanGraphCache($file) {
  254. list(
  255. $time,
  256. $type
  257. ) = explode("\n", file_get_contents($file));
  258. $time = (int)$time;
  259. if($time !== 0 and $time < time()) {
  260. return NULL;
  261. } else {
  262. return $type;
  263. }
  264. }
  265. }
  266. registerClass('Graph');
  267. /*
  268. * To preserve PHP 4 compatibility
  269. */
  270. function microtimeFloat() {
  271. list($usec, $sec) = explode(" ", microtime());
  272. return (float)$usec + (float)$sec;
  273. }
  274. ?>