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