PageRenderTime 67ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/pear/Image/Graph/Dataset/Trivial.php

https://bitbucket.org/blackriver/openx
PHP | 260 lines | 100 code | 21 blank | 139 comment | 17 complexity | d1e8fab3e76667ab30803e277c48478f MD5 | raw file
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Image_Graph - PEAR PHP OO Graph Rendering Utility.
  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 Dataset
  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: Trivial.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/Dataset.php
  30. */
  31. require_once 'Image/Graph/Dataset.php';
  32. /**
  33. * Trivial data set, simply add points (x, y) 1 by 1
  34. *
  35. * @category Images
  36. * @package Image_Graph
  37. * @subpackage Dataset
  38. * @author Jesper Veggerby <pear.nosey@veggerby.dk>
  39. * @copyright Copyright (C) 2003, 2004 Jesper Veggerby Hansen
  40. * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
  41. * @version Release: 0.7.2
  42. * @link http://pear.php.net/package/Image_Graph
  43. */
  44. class Image_Graph_Dataset_Trivial extends Image_Graph_Dataset
  45. {
  46. /**
  47. * Data storage
  48. * @var array
  49. * @access private
  50. */
  51. var $_data;
  52. /**
  53. * Image_Graph_Dataset_Trivial [Constructor]
  54. *
  55. * Pass an associated array ($data[$x] = $y) to the constructor for easy
  56. * data addition. Alternatively (if multiple entries with same x value is
  57. * required) pass an array with (x, y) values: $data[$id] = array('x' => $x,
  58. * 'y' => $y);
  59. *
  60. * NB! If passing the 1st type array at this point, the x-values will also
  61. * be used for ID tags, i.e. when using {@link Image_Graph_Fill_Array}. In
  62. * the 2nd type the key/index of the "outermost" array will be the id tag
  63. * (i.e. $id in the example)
  64. *
  65. * @param array $dataArray An associated array with values to the dataset
  66. */
  67. function Image_Graph_Dataset_Trivial($dataArray = false)
  68. {
  69. parent::__construct();
  70. $this->_data = array ();
  71. if (is_array($dataArray)) {
  72. reset($dataArray);
  73. $keys = array_keys($dataArray);
  74. foreach ($keys as $x) {
  75. $y =& $dataArray[$x];
  76. if ((is_array($y)) && (isset($y['x'])) && (isset($y['y']))) {
  77. $this->addPoint($y['x'], $y['y'], (isset($y['id']) ? $y['id'] : $x));
  78. } else {
  79. $this->addPoint($x, $y, $x);
  80. }
  81. }
  82. }
  83. }
  84. /**
  85. * Add a point to the dataset
  86. *
  87. * $ID can contain either the ID of the point, i.e. 'DK', 123, 'George', etc. or it can contain
  88. * values used for creation of the HTML image map. This is achieved using is an an associated array
  89. * with the following values:
  90. *
  91. * 'url' The URL to create the link to
  92. *
  93. * 'alt' [optional] The alt text on the link
  94. *
  95. * 'target' [optional] The target of the link
  96. *
  97. * 'htmltags' [optional] An associated array with html tags (tag as key), fx. 'onMouseOver' => 'history.go(-1);', 'id' => 'thelink'
  98. *
  99. * @param int $x The X value to add
  100. * @param int $y The Y value to add, can be omited
  101. * @param var $ID The ID of the point
  102. */
  103. function addPoint($x, $y = false, $ID = false)
  104. {
  105. parent::addPoint($x, $y, $ID);
  106. if (is_array($ID)) {
  107. $data = $ID;
  108. $ID = (isset($data['id']) ? $data['id'] : false);
  109. } else {
  110. $data = false;
  111. }
  112. $this->_data[] = array ('X' => $x, 'Y' => $y, 'ID' => $ID, 'data' => $data);
  113. if (!is_numeric($x)) {
  114. $this->_maximumX = count($this->_data);
  115. }
  116. }
  117. /**
  118. * The first point
  119. *
  120. * @return array The last point
  121. */
  122. function first()
  123. {
  124. reset($this->_data);
  125. return current($this->_data);
  126. }
  127. /**
  128. * The last point
  129. *
  130. * @return array The first point
  131. */
  132. function last()
  133. {
  134. return end($this->_data);
  135. }
  136. /**
  137. * Gets a X point from the dataset
  138. *
  139. * @param var $x The variable to return an X value from, fx in a
  140. * vector function data set
  141. * @return var The X value of the variable
  142. * @access private
  143. */
  144. function _getPointX($x)
  145. {
  146. if (isset($this->_data[$x])) {
  147. return $this->_data[$x]['X'];
  148. } else {
  149. return false;
  150. }
  151. }
  152. /**
  153. * Gets a Y point from the dataset
  154. *
  155. * @param var $x The variable to return an Y value from, fx in a
  156. * vector function data set
  157. * @return var The Y value of the variable
  158. * @access private
  159. */
  160. function _getPointY($x)
  161. {
  162. if (isset($this->_data[$x])) {
  163. return $this->_data[$x]['Y'];
  164. } else {
  165. return false;
  166. }
  167. }
  168. /**
  169. * Gets a ID from the dataset
  170. *
  171. * @param var $x The variable to return an Y value from, fx in a
  172. * vector function data set
  173. * @return var The ID value of the variable
  174. * @access private
  175. */
  176. function _getPointID($x)
  177. {
  178. if (isset($this->_data[$x])) {
  179. return $this->_data[$x]['ID'];
  180. } else {
  181. return false;
  182. }
  183. }
  184. /**
  185. * Gets point data from the dataset
  186. *
  187. * @param var $x The variable to return an Y value from, fx in a vector
  188. * function data set
  189. * @return array The data for the point
  190. * @access private
  191. */
  192. function _getPointData($x)
  193. {
  194. if (isset($this->_data[$x])) {
  195. return $this->_data[$x]['data'];
  196. } else {
  197. return false;
  198. }
  199. }
  200. /**
  201. * The number of values in the dataset
  202. *
  203. * @return int The number of values in the dataset
  204. */
  205. function count()
  206. {
  207. return count($this->_data);
  208. }
  209. /**
  210. * Reset the intertal dataset pointer
  211. *
  212. * @return var The first X value
  213. * @access private
  214. */
  215. function _reset()
  216. {
  217. $this->_posX = 0;
  218. return $this->_posX;
  219. }
  220. /**
  221. * Get the next point the internal pointer refers to and advance the pointer
  222. *
  223. * @return array The next point
  224. * @access private
  225. */
  226. function _next()
  227. {
  228. if ($this->_posX >= $this->count()) {
  229. return false;
  230. }
  231. $x = $this->_getPointX($this->_posX);
  232. $y = $this->_getPointY($this->_posX);
  233. $ID = $this->_getPointID($this->_posX);
  234. $data = $this->_getPointData($this->_posX);
  235. $this->_posX += $this->_stepX();
  236. return array('X' => $x, 'Y' => $y, 'ID' => $ID, 'data' => $data);
  237. }
  238. }
  239. ?>