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

/lib/pear/Image/Graph/Axis/Category.php

https://bitbucket.org/blackriver/openx
PHP | 437 lines | 195 code | 40 blank | 202 comment | 41 complexity | 770fee70de5629ed2efe49b17429de1c MD5 | raw file
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Class for axis handling.
  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 Axis
  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: Category.php 47481 2009-12-15 20:29:37Z chris.nutting $
  26. * @link http://pear.php.net/package/Image_Graph
  27. */
  28. /**
  29. * Include file Image/Graph/Axis.php
  30. */
  31. require_once 'Image/Graph/Axis.php';
  32. /**
  33. * A normal axis thats displays labels with a 'interval' of 1.
  34. * This is basically a normal axis where the range is
  35. * the number of labels defined, that is the range is explicitly defined
  36. * when constructing the axis.
  37. *
  38. * @category Images
  39. * @package Image_Graph
  40. * @subpackage Axis
  41. * @author Jesper Veggerby <pear.nosey@veggerby.dk>
  42. * @copyright Copyright (C) 2003, 2004 Jesper Veggerby Hansen
  43. * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
  44. * @version Release: 0.7.2
  45. * @link http://pear.php.net/package/Image_Graph
  46. */
  47. class Image_Graph_Axis_Category extends Image_Graph_Axis
  48. {
  49. /**
  50. * The labels shown on the axis
  51. * @var array
  52. * @access private
  53. */
  54. var $_labels = false;
  55. /**
  56. * Image_Graph_Axis_Category [Constructor].
  57. *
  58. * @param int $type The type (direction) of the Axis
  59. */
  60. function Image_Graph_Axis_Category($type = IMAGE_GRAPH_AXIS_X)
  61. {
  62. parent::__construct($type);
  63. $this->_labels = array();
  64. $this->setlabelInterval(1);
  65. }
  66. /**
  67. * Gets the minimum value the axis will show.
  68. *
  69. * This is always 0
  70. *
  71. * @return double The minumum value
  72. * @access private
  73. */
  74. function _getMinimum()
  75. {
  76. return 0;
  77. }
  78. /**
  79. * Gets the maximum value the axis will show.
  80. *
  81. * This is always the number of labels passed to the constructor.
  82. *
  83. * @return double The maximum value
  84. * @access private
  85. */
  86. function _getMaximum()
  87. {
  88. return count($this->_labels) - 1;
  89. }
  90. /**
  91. * Sets the minimum value the axis will show.
  92. *
  93. * A minimum cannot be set on a SequentialAxis, it is always 0.
  94. *
  95. * @param double Minimum The minumum value to use on the axis
  96. * @access private
  97. */
  98. function _setMinimum($minimum)
  99. {
  100. }
  101. /**
  102. * Sets the maximum value the axis will show
  103. *
  104. * A maximum cannot be set on a SequentialAxis, it is always the number
  105. * of labels passed to the constructor.
  106. *
  107. * @param double Maximum The maximum value to use on the axis
  108. * @access private
  109. */
  110. function _setMaximum($maximum)
  111. {
  112. }
  113. /**
  114. * Forces the minimum value of the axis
  115. *
  116. * <b>A minimum cannot be set on this type of axis</b>
  117. *
  118. * To modify the labels which are displayed on the axis, instead use
  119. * setLabelInterval($labels) where $labels is an array containing the
  120. * values/labels the axis should display. <b>Note!</b> Only values in
  121. * this array will then be displayed on the graph!
  122. *
  123. * @param double $minimum A minimum cannot be set on this type of axis
  124. */
  125. function forceMinimum($minimum, $userEnforce = true)
  126. {
  127. }
  128. /**
  129. * Forces the maximum value of the axis
  130. *
  131. * <b>A maximum cannot be set on this type of axis</b>
  132. *
  133. * To modify the labels which are displayed on the axis, instead use
  134. * setLabelInterval($labels) where $labels is an array containing the
  135. * values/labels the axis should display. <b>Note!</b> Only values in
  136. * this array will then be displayed on the graph!
  137. *
  138. * @param double $maximum A maximum cannot be set on this type of axis
  139. */
  140. function forceMaximum($maximum, $userEnforce = true)
  141. {
  142. }
  143. /**
  144. * Sets an interval for where labels are shown on the axis.
  145. *
  146. * The label interval is rounded to nearest integer value.
  147. *
  148. * @param double $labelInterval The interval with which labels are shown
  149. */
  150. function setLabelInterval($labelInterval = 'auto', $level = 1)
  151. {
  152. if (is_array($labelInterval)) {
  153. parent::setLabelInterval($labelInterval);
  154. } elseif ($labelInterval == 'auto') {
  155. parent::setLabelInterval(1);
  156. } else {
  157. parent::setLabelInterval(round($labelInterval));
  158. }
  159. }
  160. /**
  161. * Preprocessor for values, ie for using logarithmic axis
  162. *
  163. * @param double $value The value to preprocess
  164. * @return double The preprocessed value
  165. * @access private
  166. */
  167. function _value($value)
  168. {
  169. // $the_value = array_search($value, $this->_labels);
  170. if (isset($this->_labels[$value])) {
  171. $the_value = $this->_labels[$value];
  172. if ($the_value !== false) {
  173. return $the_value + ($this->_pushValues ? 0.5 : 0);
  174. } else {
  175. return 0;
  176. }
  177. }
  178. }
  179. /**
  180. * Get the minor label interval with which axis label ticks are drawn.
  181. *
  182. * For a sequential axis this is always disabled (i.e false)
  183. *
  184. * @return double The minor label interval, always false
  185. * @access private
  186. */
  187. function _minorLabelInterval()
  188. {
  189. return false;
  190. }
  191. /**
  192. * Get the size in pixels of the axis.
  193. *
  194. * For an x-axis this is the width of the axis including labels, and for an
  195. * y-axis it is the corrresponding height
  196. *
  197. * @return int The size of the axis
  198. * @access private
  199. */
  200. function _size()
  201. {
  202. if (!$this->_visible) {
  203. return 0;
  204. }
  205. $this->_canvas->setFont($this->_getFont());
  206. $maxSize = 0;
  207. foreach($this->_labels as $label => $id) {
  208. $labelPosition = $this->_point($label);
  209. if (is_object($this->_dataPreProcessor)) {
  210. $labelText = $this->_dataPreProcessor->_process($label);
  211. } else {
  212. $labelText = $label;
  213. }
  214. if ((($this->_type == IMAGE_GRAPH_AXIS_X) && (!$this->_transpose)) ||
  215. (($this->_type != IMAGE_GRAPH_AXIS_X) && ($this->_transpose)))
  216. {
  217. $maxSize = max($maxSize, $this->_canvas->textHeight($labelText));
  218. } else {
  219. $maxSize = max($maxSize, $this->_canvas->textWidth($labelText));
  220. }
  221. }
  222. if ($this->_title) {
  223. $this->_canvas->setFont($this->_getTitleFont());
  224. if ((($this->_type == IMAGE_GRAPH_AXIS_X) && (!$this->_transpose)) ||
  225. (($this->_type != IMAGE_GRAPH_AXIS_X) && ($this->_transpose)))
  226. {
  227. $maxSize += $this->_canvas->textHeight($this->_title);
  228. } else {
  229. $maxSize += $this->_canvas->textWidth($this->_title);
  230. }
  231. $maxSize += 10;
  232. }
  233. return $maxSize +3;
  234. }
  235. /**
  236. * Apply the dataset to the axis.
  237. *
  238. * This calculates the order of the categories, which is very important
  239. * for fx. line plots, so that the line does not "go backwards", consider
  240. * these X-sets:<p>
  241. * 1: (1, 2, 3, 4, 5, 6)<br>
  242. * 2: (0, 1, 2, 3, 4, 5, 6, 7)<p>
  243. * If they are not ordered, but simply appended, the categories on the axis
  244. * would be:<p>
  245. * X: (1, 2, 3, 4, 5, 6, 0, 7)<p>
  246. * Which would render the a line for the second plot to show incorrectly.
  247. * Instead this algorithm, uses and 'value- is- before' method to see that
  248. * the 0 is before a 1 in the second set, and that it should also be before
  249. * a 1 in the X set. Hence:<p>
  250. * X: (0, 1, 2, 3, 4, 5, 6, 7)
  251. *
  252. * @param Image_Graph_Dataset $dataset The dataset
  253. * @access private
  254. */
  255. function _applyDataset(&$dataset)
  256. {
  257. $newLabels = array();
  258. $allLabels = array();
  259. $dataset->_reset();
  260. $count = 0;
  261. $count_new = 0;
  262. while ($point = $dataset->_next()) {
  263. if ($this->_type == IMAGE_GRAPH_AXIS_X) {
  264. $data = $point['X'];
  265. } else {
  266. $data = $point['Y'];
  267. }
  268. if (!isset($this->_labels[$data])) {
  269. $newLabels[$data] = $count_new++;
  270. //$this->_labels[] = $data;
  271. }
  272. $allLabels[$data] = $count++;
  273. }
  274. if (count($this->_labels) == 0) {
  275. $this->_labels = $newLabels;
  276. } elseif ((is_array($newLabels)) && (count($newLabels) > 0)) {
  277. // get all intersecting labels
  278. $intersect = array_intersect(array_keys($allLabels), array_keys($this->_labels));
  279. // traverse all new and find their relative position withing the
  280. // intersec, fx value X0 is before X1 in the intersection, which
  281. // means that X0 should be placed before X1 in the label array
  282. foreach($newLabels as $newLabel => $id) {
  283. $key = $allLabels[$newLabel];
  284. reset($intersect);
  285. $this_value = false;
  286. // intersect indexes are the same as in allLabels!
  287. $first = true;
  288. while ((list($id, $value) = each($intersect)) &&
  289. ($this_value === false))
  290. {
  291. if (($first) && ($id > $key)) {
  292. $this_value = $value;
  293. } elseif ($id >= $key) {
  294. $this_value = $value;
  295. }
  296. $first = false;
  297. }
  298. if ($this_value === false) {
  299. // the new label was not found before anything in the
  300. // intersection -> append it
  301. $this->_labels[$newLabel] = count($this->_labels);
  302. } else {
  303. // the new label was found before $this_value in the
  304. // intersection, insert the label before this position in
  305. // the label array
  306. // $key = $this->_labels[$this_value];
  307. $keys = array_keys($this->_labels);
  308. $key = array_search($this_value, $keys);
  309. $pre = array_slice($keys, 0, $key);
  310. $pre[] = $newLabel;
  311. $post = array_slice($keys, $key);
  312. $this->_labels = array_flip(array_merge($pre, $post));
  313. }
  314. }
  315. unset($keys);
  316. }
  317. $labels = array_keys($this->_labels);
  318. $i = 0;
  319. foreach ($labels as $label) {
  320. $this->_labels[$label] = $i++;
  321. }
  322. // $this->_labels = array_values(array_unique($this->_labels));
  323. $this->_calcLabelInterval();
  324. }
  325. /**
  326. * Return the label distance.
  327. *
  328. * @return int The distance between 2 adjacent labels
  329. * @access private
  330. */
  331. function _labelDistance($level = 1)
  332. {
  333. reset($this->_labels);
  334. list($l1) = each($this->_labels);
  335. list($l2) = each($this->_labels);
  336. return abs($this->_point($l2) - $this->_point($l1));
  337. }
  338. /**
  339. * Get next label point
  340. *
  341. * @param doubt $point The current point, if omitted or false, the first is
  342. * returned
  343. * @return double The next label point
  344. * @access private
  345. */
  346. function _getNextLabel($currentLabel = false, $level = 1)
  347. {
  348. if ($currentLabel === false) {
  349. reset($this->_labels);
  350. }
  351. $result = false;
  352. $count = ($currentLabel === false ? $this->_labelInterval() - 1 : 0);
  353. while ($count < $this->_labelInterval()) {
  354. $result = (list($label) = each($this->_labels));
  355. $count++;
  356. }
  357. if ($result) {
  358. return $label;
  359. } else {
  360. return false;
  361. }
  362. }
  363. /**
  364. * Is the axis numeric or not?
  365. *
  366. * @return bool True if numeric, false if not
  367. * @access private
  368. */
  369. function _isNumeric()
  370. {
  371. return false;
  372. }
  373. /**
  374. * Output the axis
  375. *
  376. * @return bool Was the output 'good' (true) or 'bad' (false).
  377. * @access private
  378. */
  379. function _done()
  380. {
  381. $result = true;
  382. if (Image_Graph_Element::_done() === false) {
  383. $result = false;
  384. }
  385. $this->_canvas->startGroup(get_class($this));
  386. $this->_drawAxisLines();
  387. $this->_canvas->startGroup(get_class($this) . '_ticks');
  388. $label = false;
  389. while (($label = $this->_getNextLabel($label)) !== false) {
  390. $this->_drawTick($label);
  391. }
  392. $this->_canvas->endGroup();
  393. $this->_canvas->endGroup();
  394. return $result;
  395. }
  396. }
  397. ?>