PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/pear/Image/Graph/Legend.php

https://github.com/axxtel/agilebill
PHP | 385 lines | 224 code | 38 blank | 123 comment | 37 complexity | 4c6e764112fbe90f0adbc85763cb91d3 MD5 | raw file
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Image_Graph - Main class for the graph creation.
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * LICENSE: This library is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation; either version 2.1 of the License, or (at your
  11. * option) any later version. This library is distributed in the hope that it
  12. * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  13. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
  14. * General Public License for more details. You should have received a copy of
  15. * the GNU Lesser General Public License along with this library; if not, write
  16. * to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  17. * 02111-1307 USA
  18. *
  19. * @category Images
  20. * @package Image_Graph
  21. * @subpackage Legend
  22. * @author Jesper Veggerby <pear.nosey@veggerby.dk>
  23. * @copyright Copyright (C) 2003, 2004 Jesper Veggerby Hansen
  24. * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
  25. * @version CVS: $Id: Legend.php,v 1.16 2006/02/28 22:48:07 nosey Exp $
  26. * @link http://pear.php.net/package/Image_Graph
  27. */
  28. /**
  29. * Include file Image/Graph/Layout.php
  30. */
  31. require_once 'Image/Graph/Layout.php';
  32. /**
  33. * Displays a legend for a plotarea.
  34. *
  35. * A legend can be displayed in two ways:
  36. *
  37. * 1 As an overlayed box within the plotarea
  38. *
  39. * 2 Layout'ed on the canvas smewhere next to the plotarea.
  40. *
  41. * @category Images
  42. * @package Image_Graph
  43. * @subpackage Legend
  44. * @author Jesper Veggerby <pear.nosey@veggerby.dk>
  45. * @copyright Copyright (C) 2003, 2004 Jesper Veggerby Hansen
  46. * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
  47. * @version Release: @package_version@
  48. * @link http://pear.php.net/package/Image_Graph
  49. */
  50. class Image_Graph_Legend extends Image_Graph_Layout
  51. {
  52. /**
  53. * Alignment of the text
  54. * @var int
  55. * @access private
  56. */
  57. var $_alignment = false;
  58. /**
  59. * The plotarea(s) to show the legend for
  60. * @var array
  61. * @access private
  62. */
  63. var $_plotareas = array();
  64. /**
  65. * Should markers be shown or not on this legend
  66. * @var bool
  67. * @access private
  68. */
  69. var $_showMarker = false;
  70. /**
  71. * Image_Graph_Legend [Constructor]
  72. */
  73. function Image_Graph_Legend()
  74. {
  75. parent::Image_Graph_Layout();
  76. $this->_padding = array('left' => 5, 'top' => 5, 'right' => 5, 'bottom' => 5);
  77. }
  78. /**
  79. * The number of actual plots in the plot area
  80. *
  81. * @return int The number of plotes
  82. * @access private
  83. */
  84. function _plotCount()
  85. {
  86. $count = 0;
  87. $keys = array_keys($this->_plotareas);
  88. foreach($keys as $key) {
  89. $plotarea =& $this->_plotareas[$key];
  90. if (is_a($plotarea, 'Image_Graph_Plotarea')) {
  91. $keys2 = array_keys($plotarea->_elements);
  92. foreach ($keys2 as $key) {
  93. $element =& $plotarea->_elements[$key];
  94. if (is_a($element, 'Image_Graph_Plot')) {
  95. $count ++;
  96. }
  97. }
  98. unset($keys2);
  99. }
  100. }
  101. unset($keys);
  102. return $count;
  103. }
  104. /**
  105. * Get a default parameter array for legendSamples
  106. * @param bool $simulate Whether the array should be used for simulation or
  107. * not
  108. * @return array Default parameter array
  109. * @access private
  110. */
  111. function _parameterArray($simulate = false)
  112. {
  113. $param['left'] = $this->_left + $this->_padding['left'];
  114. $param['top'] = $this->_top + $this->_padding['top'];
  115. $param['right'] = $this->_right - $this->_padding['right'];
  116. $param['bottom'] = $this->_bottom - $this->_padding['bottom'];
  117. $param['align'] = $this->_alignment;
  118. $param['x'] = $this->_left + $this->_padding['left'];
  119. $param['y'] = $this->_top + $this->_padding['top'];
  120. $param['width'] = 16;
  121. $param['height'] = 16;
  122. $param['show_marker'] = $this->_showMarker;
  123. $param['maxwidth'] = 0;
  124. $param['font'] = $this->_getFont();
  125. if ($simulate) {
  126. $param['simulate'] = true;
  127. }
  128. return $param;
  129. }
  130. /**
  131. * The height of the element on the canvas
  132. *
  133. * @return int Number of pixels representing the height of the element
  134. * @access private
  135. */
  136. function _height()
  137. {
  138. $parent = (is_object($this->_parent) ? get_class($this->_parent) : $this->_parent);
  139. if (strtolower($parent) == 'image_graph_plotarea') {
  140. $param = $this->_parameterArray(true);
  141. $param['align'] = IMAGE_GRAPH_ALIGN_VERTICAL;
  142. $param0 = $param;
  143. $keys = array_keys($this->_plotareas);
  144. foreach($keys as $key) {
  145. $plotarea =& $this->_plotareas[$key];
  146. $keys2 = array_keys($plotarea->_elements);
  147. foreach($keys2 as $key) {
  148. $element =& $plotarea->_elements[$key];
  149. if (is_a($element, 'Image_Graph_Plot')) {
  150. $element->_legendSample($param);
  151. }
  152. }
  153. unset($keys2);
  154. }
  155. unset($keys);
  156. return abs($param['y'] - $param0['y']) + $this->_padding['top'] + $this->_padding['bottom'];
  157. } else {
  158. return parent::height();
  159. }
  160. }
  161. /**
  162. * The width of the element on the canvas
  163. *
  164. * @return int Number of pixels representing the width of the element
  165. * @access private
  166. */
  167. function _width()
  168. {
  169. $parent = (is_object($this->_parent) ? get_class($this->_parent) : $this->_parent);
  170. if (strtolower($parent) == 'image_graph_plotarea') {
  171. $param = $this->_parameterArray(true);
  172. $param['align'] = IMAGE_GRAPH_ALIGN_VERTICAL;
  173. $keys = array_keys($this->_plotareas);
  174. foreach($keys as $key) {
  175. $plotarea =& $this->_plotareas[$key];
  176. $keys2 = array_keys($plotarea->_elements);
  177. foreach($keys2 as $key) {
  178. $element =& $plotarea->_elements[$key];
  179. if (is_a($element, 'Image_Graph_Plot')) {
  180. $element->_legendSample($param);
  181. }
  182. }
  183. unset($keys2);
  184. }
  185. unset($keys);
  186. return $param['maxwidth'];
  187. } else {
  188. return parent::width();
  189. }
  190. }
  191. /**
  192. * Set the alignment of the legend
  193. *
  194. * @param int $alignment The alignment
  195. */
  196. function setAlignment($alignment)
  197. {
  198. $this->_alignment = $alignment;
  199. }
  200. /**
  201. * Update coordinates
  202. *
  203. * @access private
  204. */
  205. function _updateCoords()
  206. {
  207. parent::_updateCoords();
  208. $parent = (is_object($this->_parent) ? get_class($this->_parent) : $this->_parent);
  209. if (strtolower($parent) == 'image_graph_plotarea') {
  210. $w = $this->_width();
  211. $h = $this->_height();
  212. if ($this->_alignment === false) {
  213. $this->_alignment = IMAGE_GRAPH_ALIGN_TOP + IMAGE_GRAPH_ALIGN_RIGHT;
  214. }
  215. if (($this->_alignment & IMAGE_GRAPH_ALIGN_BOTTOM) != 0) {
  216. $y = $this->_parent->_fillBottom() - $h - $this->_padding['bottom'];
  217. } else {
  218. $y = $this->_parent->_fillTop() + $this->_padding['top'];
  219. }
  220. if (($this->_alignment & IMAGE_GRAPH_ALIGN_LEFT) != 0) {
  221. $x = $this->_parent->_fillLeft() + $this->_padding['left'];
  222. } else {
  223. $x = $this->_parent->_fillRight() - $w - $this->_padding['right'];
  224. }
  225. $this->_setCoords($x, $y, $x + $w, $y + $h);
  226. }
  227. }
  228. /**
  229. * Sets Plotarea
  230. *
  231. * @param Image_Graph_Plotarea $plotarea The plotarea
  232. */
  233. function setPlotarea(& $plotarea)
  234. {
  235. if (is_a($plotarea, 'Image_Graph_Plotarea')) {
  236. $this->_plotareas[] =& $plotarea;
  237. }
  238. }
  239. /**
  240. * Sets the parent. The parent chain should ultimately be a GraPHP object
  241. *
  242. * @see Image_Graph
  243. * @param Image_Graph_Common $parent The parent
  244. * @access private
  245. */
  246. function _setParent(& $parent)
  247. {
  248. parent::_setParent($parent);
  249. if (count($this->_plotareas) == 0) {
  250. $this->setPlotarea($parent);
  251. }
  252. }
  253. /**
  254. * Set if this legends should show markers
  255. *
  256. * @param bool $showMarker True if markers are to be shown, false is not
  257. */
  258. function setShowMarker($showMarker)
  259. {
  260. $this->_showMarker = $showMarker;
  261. }
  262. /**
  263. * Output the plot
  264. *
  265. * @return bool Was the output 'good' (true) or 'bad' (false).
  266. * @access private
  267. */
  268. function _done()
  269. {
  270. if (Image_Graph_Element::_done() === false) {
  271. return false;
  272. }
  273. $this->_canvas->startGroup(get_class($this));
  274. $param = $this->_parameterArray();
  275. $parent = (is_object($this->_parent) ?
  276. get_class($this->_parent) :
  277. $this->_parent
  278. );
  279. if (strtolower($parent) == 'image_graph_plotarea') {
  280. $this->_getFillStyle();
  281. $this->_getLineStyle();
  282. $this->_canvas->rectangle(
  283. array(
  284. 'x0' => $this->_left,
  285. 'y0' => $this->_top,
  286. 'x1' => $this->_right,
  287. 'y1' => $this->_bottom
  288. )
  289. );
  290. $param = $this->_parameterArray();
  291. $keys = array_keys($this->_plotareas);
  292. foreach($keys as $key) {
  293. $plotarea =& $this->_plotareas[$key];
  294. $keys2 = array_keys($plotarea->_elements);
  295. foreach($keys2 as $key) {
  296. $element =& $plotarea->_elements[$key];
  297. if (is_a($element, 'Image_Graph_Plot')) {
  298. $element->_legendSample($param);
  299. }
  300. }
  301. unset($keys2);
  302. }
  303. unset($keys);
  304. } else {
  305. $param0 = $param;
  306. $param0['simulate'] = true;
  307. $keys = array_keys($this->_plotareas);
  308. foreach($keys as $key) {
  309. $plotarea =& $this->_plotareas[$key];
  310. $keys2 = array_keys($plotarea->_elements);
  311. foreach($keys2 as $key) {
  312. $element =& $plotarea->_elements[$key];
  313. if (is_a($element, 'Image_Graph_Plot')) {
  314. $element->_legendSample($param0);
  315. }
  316. }
  317. unset($keys2);
  318. }
  319. unset($keys);
  320. if (($this->_alignment & IMAGE_GRAPH_ALIGN_VERTICAL) != 0) {
  321. if ($param0['x'] == $param['x']) {
  322. $param['y'] = $param['y'] + ($this->_height() - ($param0['y'] - $param['y']))/2;
  323. }
  324. } else {
  325. if ($param0['y'] == $param['y']) {
  326. $param['x'] = $param['x'] + ($this->_width() - ($param0['x'] - $param['x']))/2;
  327. }
  328. }
  329. $keys = array_keys($this->_plotareas);
  330. foreach($keys as $key) {
  331. $plotarea =& $this->_plotareas[$key];
  332. $keys2 = array_keys($plotarea->_elements);
  333. foreach($keys2 as $key) {
  334. $element =& $plotarea->_elements[$key];
  335. if (is_a($element, 'Image_Graph_Plot')) {
  336. $element->_legendSample($param);
  337. }
  338. }
  339. unset($keys2);
  340. }
  341. unset($keys);
  342. }
  343. $this->_canvas->endGroup();
  344. return true;
  345. }
  346. }
  347. ?>