PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/MantisBT/library/ezc/Graph/src/axis/numeric.php

https://bitbucket.org/crypticrod/sr_wp_code
PHP | 505 lines | 251 code | 40 blank | 214 comment | 30 complexity | f94fb34c7d2d0af52145b382b9bb3dda MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1, GPL-3.0, LGPL-2.0, AGPL-3.0
  1. <?php
  2. /**
  3. * File containing the abstract ezcGraphChartElementNumericAxis class
  4. *
  5. * @package Graph
  6. * @version 1.5
  7. * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
  8. * @license http://ez.no/licenses/new_bsd New BSD License
  9. */
  10. /**
  11. * Class to represent a numeric axis.
  12. *
  13. * Axis elements represent the axis in a bar, line or radar chart. They are
  14. * chart elements (ezcGraphChartElement) extending from
  15. * ezcGraphChartElementAxis, where additional formatting options can be found.
  16. * You should generally use the axis, which matches your input data best, so
  17. * that the automatic chart layouting works best. Aavailable axis types are:
  18. *
  19. * - ezcGraphChartElementDateAxis
  20. * - ezcGraphChartElementLabeledAxis
  21. * - ezcGraphChartElementLogarithmicalAxis
  22. * - ezcGraphChartElementNumericAxis
  23. *
  24. * The axis tries to calculate "nice" start and end values for the axis scale.
  25. * The used interval is considered as nice, if it is equal to [1,2,5] * 10^x
  26. * with x in [.., -1, 0, 1, ..].
  27. *
  28. * The start and end value are the next bigger / smaller multiple of the
  29. * intervall compared to the maximum / minimum axis value.
  30. *
  31. * You may specify custom step sizes using the properties $majorStep and
  32. * $minorStep. The minimum and maximum values for the axis labels can be
  33. * defined using the $min and $max properties. You should be able to set any
  34. * subset of these values, and all values not explicitely set will be
  35. * calculated automatically.
  36. *
  37. * This axis should be used for all numeric values except dates. If your data
  38. * does span very big number intervals you might want to consider using the
  39. * logrithmic axis instead.
  40. *
  41. * The numeric axis may be used like:
  42. *
  43. * <code>
  44. * $graph = new ezcGraphLineChart();
  45. * $graph->title = 'Some random data';
  46. * $graph->legend = false;
  47. *
  48. * $graph->xAxis = new ezcGraphChartElementNumericAxis();
  49. * // The y axis is numeric by default.
  50. *
  51. * $graph->xAxis->min = -15;
  52. * $graph->xAxis->max = 15;
  53. * $graph->xAxis->majorStep = 5;
  54. *
  55. * $data = array(
  56. * array(),
  57. * array()
  58. * );
  59. * for ( $i = -10; $i <= 10; $i++ )
  60. * {
  61. * $data[0][$i] = mt_rand( -23, 59 );
  62. * $data[1][$i] = mt_rand( -23, 59 );
  63. * }
  64. *
  65. * // Add data
  66. * $graph->data['random blue'] = new ezcGraphArrayDataSet( $data[0] );
  67. * $graph->data['random green'] = new ezcGraphArrayDataSet( $data[1] );
  68. *
  69. * $graph->render( 400, 150, 'tutorial_axis_numeric.svg' );
  70. * </code>
  71. *
  72. * @property float $min
  73. * Minimum value of displayed scale on axis.
  74. * @property float $max
  75. * Maximum value of displayed scale on axis.
  76. * @property mixed $majorStep
  77. * Labeled major steps displayed on the axis.
  78. * @property mixed $minorStep
  79. * Non labeled minor steps on the axis.
  80. * @property-read float $minValue
  81. * Minimum Value to display on this axis.
  82. * @property-read float $maxValue
  83. * Maximum value to display on this axis.
  84. *
  85. * @version 1.5
  86. * @package Graph
  87. * @mainclass
  88. */
  89. class ezcGraphChartElementNumericAxis extends ezcGraphChartElementAxis
  90. {
  91. /**
  92. * Constant used for calculation of automatic definition of major scaling
  93. * steps
  94. */
  95. const MIN_MAJOR_COUNT = 5;
  96. /**
  97. * Constant used for automatic calculation of minor steps from given major
  98. * steps
  99. */
  100. const MIN_MINOR_COUNT = 8;
  101. /**
  102. * Constructor
  103. *
  104. * @param array $options Default option array
  105. * @return void
  106. * @ignore
  107. */
  108. public function __construct( array $options = array() )
  109. {
  110. $this->properties['min'] = null;
  111. $this->properties['max'] = null;
  112. $this->properties['minValue'] = null;
  113. $this->properties['maxValue'] = null;
  114. parent::__construct( $options );
  115. }
  116. /**
  117. * __set
  118. *
  119. * @param mixed $propertyName
  120. * @param mixed $propertyValue
  121. * @throws ezcBaseValueException
  122. * If a submitted parameter was out of range or type.
  123. * @throws ezcBasePropertyNotFoundException
  124. * If a the value for the property options is not an instance of
  125. * @return void
  126. * @ignore
  127. */
  128. public function __set( $propertyName, $propertyValue )
  129. {
  130. switch ( $propertyName )
  131. {
  132. case 'min':
  133. if ( !is_numeric( $propertyValue ) )
  134. {
  135. throw new ezcBaseValueException( $propertyName, $propertyValue, 'float' );
  136. }
  137. $this->properties['min'] = (float) $propertyValue;
  138. $this->properties['initialized'] = true;
  139. break;
  140. case 'max':
  141. if ( !is_numeric( $propertyValue ) )
  142. {
  143. throw new ezcBaseValueException( $propertyName, $propertyValue, 'float' );
  144. }
  145. $this->properties['max'] = (float) $propertyValue;
  146. $this->properties['initialized'] = true;
  147. break;
  148. default:
  149. parent::__set( $propertyName, $propertyValue );
  150. break;
  151. }
  152. }
  153. /**
  154. * Returns a "nice" number for a given floating point number.
  155. *
  156. * Nice numbers are steps on a scale which are easily recognized by humans
  157. * like 0.5, 25, 1000 etc.
  158. *
  159. * @param float $float Number to be altered
  160. * @return float Nice number
  161. */
  162. protected function getNiceNumber( $float )
  163. {
  164. // Get absolute value and save sign
  165. $abs = abs( $float );
  166. $sign = $float / $abs;
  167. // Normalize number to a range between 1 and 10
  168. $log = (int) round( log10( $abs ), 0 );
  169. $abs /= pow( 10, $log );
  170. // find next nice number
  171. if ( $abs > 5 )
  172. {
  173. $abs = 10.;
  174. }
  175. elseif ( $abs > 2.5 )
  176. {
  177. $abs = 5.;
  178. }
  179. elseif ( $abs > 1 )
  180. {
  181. $abs = 2.5;
  182. }
  183. else
  184. {
  185. $abs = 1;
  186. }
  187. // unnormalize number to original values
  188. return $abs * pow( 10, $log ) * $sign;
  189. }
  190. /**
  191. * Calculate minimum value for displayed axe basing on real minimum and
  192. * major step size
  193. *
  194. * @param float $min Real data minimum
  195. * @param float $max Real data maximum
  196. * @return void
  197. */
  198. protected function calculateMinimum( $min, $max )
  199. {
  200. if ( $this->properties['max'] === null )
  201. {
  202. $this->properties['min'] = floor( $min / $this->properties['majorStep'] ) * $this->properties['majorStep'];
  203. }
  204. else
  205. {
  206. $calculatedMin = $this->properties['max'];
  207. do {
  208. $calculatedMin -= $this->properties['majorStep'];
  209. } while ( $calculatedMin > $min );
  210. $this->properties['min'] = $calculatedMin;
  211. }
  212. }
  213. /**
  214. * Calculate maximum value for displayed axe basing on real maximum and
  215. * major step size
  216. *
  217. * @param float $min Real data minimum
  218. * @param float $max Real data maximum
  219. * @return void
  220. */
  221. protected function calculateMaximum( $min, $max )
  222. {
  223. $calculatedMax = $this->properties['min'];
  224. do {
  225. $calculatedMax += $this->properties['majorStep'];
  226. } while ( $calculatedMax < $max );
  227. $this->properties['max'] = $calculatedMax;
  228. }
  229. /**
  230. * Calculate size of minor steps based on the size of the major step size
  231. *
  232. * @param float $min Real data minimum
  233. * @param float $max Real data maximum
  234. * @return void
  235. */
  236. protected function calculateMinorStep( $min, $max )
  237. {
  238. $stepSize = $this->properties['majorStep'] / self::MIN_MINOR_COUNT;
  239. $this->properties['minorStep'] = $this->getNiceNumber( $stepSize );
  240. }
  241. /**
  242. * Calculate size of major step based on the span to be displayed and the
  243. * defined MIN_MAJOR_COUNT constant.
  244. *
  245. * @param float $min Real data minimum
  246. * @param float $max Real data maximum
  247. * @return void
  248. */
  249. protected function calculateMajorStep( $min, $max )
  250. {
  251. $span = $max - $min;
  252. $stepSize = $span / self::MIN_MAJOR_COUNT;
  253. $this->properties['majorStep'] = $this->getNiceNumber( $stepSize );
  254. }
  255. /**
  256. * Add data for this axis
  257. *
  258. * @param array $values Value which will be displayed on this axis
  259. * @return void
  260. */
  261. public function addData( array $values )
  262. {
  263. foreach ( $values as $value )
  264. {
  265. if ( $this->properties['minValue'] === null ||
  266. $value < $this->properties['minValue'] )
  267. {
  268. $this->properties['minValue'] = $value;
  269. }
  270. if ( $this->properties['maxValue'] === null ||
  271. $value > $this->properties['maxValue'] )
  272. {
  273. $this->properties['maxValue'] = $value;
  274. }
  275. }
  276. $this->properties['initialized'] = true;
  277. }
  278. /**
  279. * Calculate axis bounding values on base of the assigned values
  280. *
  281. * @abstract
  282. * @access public
  283. * @return void
  284. */
  285. public function calculateAxisBoundings()
  286. {
  287. // Prevent division by zero, when min == max
  288. if ( $this->properties['minValue'] == $this->properties['maxValue'] )
  289. {
  290. if ( $this->properties['minValue'] == 0 )
  291. {
  292. $this->properties['maxValue'] = 1;
  293. }
  294. else
  295. {
  296. if ( $this->properties['majorStep'] !== null )
  297. {
  298. $this->properties['minValue'] -= $this->properties['majorStep'];
  299. $this->properties['maxValue'] += $this->properties['majorStep'];
  300. }
  301. else
  302. {
  303. $this->properties['minValue'] -= ( $this->properties['minValue'] * .1 );
  304. $this->properties['maxValue'] += ( $this->properties['maxValue'] * .1 );
  305. }
  306. }
  307. }
  308. // Use custom minimum and maximum if available
  309. if ( $this->properties['min'] !== null )
  310. {
  311. $this->properties['minValue'] = $this->properties['min'];
  312. }
  313. if ( $this->properties['max'] !== null )
  314. {
  315. $this->properties['maxValue'] = $this->properties['max'];
  316. }
  317. // If min and max values are forced, we may not be able to find a
  318. // "nice" number for the steps. Try to find such a nice step size, or
  319. // fall back to a step size, which is just the span divided by 5.
  320. if ( ( $this->properties['min'] !== null ) &&
  321. ( $this->properties['max'] !== null ) &&
  322. ( $this->properties['majorStep'] === null ) )
  323. {
  324. $diff = $this->properties['max'] - $this->properties['min'];
  325. $this->calculateMajorStep( $this->properties['minValue'], $this->properties['maxValue'] );
  326. $stepInvariance = $diff / $this->properties['majorStep'];
  327. if ( ( $stepInvariance - floor( $stepInvariance ) ) > .0000001 )
  328. {
  329. // For too big step invariances calculate the step size just
  330. // from the given difference between min and max value.
  331. $this->properties['majorStep'] = ( $this->properties['max'] - $this->properties['min'] ) / self::MIN_MAJOR_COUNT;
  332. $this->properties['minorStep'] = $this->properties['majorStep'] / self::MIN_MAJOR_COUNT;
  333. }
  334. }
  335. // Calculate "nice" values for scaling parameters
  336. if ( $this->properties['majorStep'] === null )
  337. {
  338. $this->calculateMajorStep( $this->properties['minValue'], $this->properties['maxValue'] );
  339. }
  340. if ( $this->properties['minorStep'] === null )
  341. {
  342. $this->calculateMinorStep( $this->properties['minValue'], $this->properties['maxValue'] );
  343. }
  344. if ( $this->properties['min'] === null )
  345. {
  346. $this->calculateMinimum( $this->properties['minValue'], $this->properties['maxValue'] );
  347. }
  348. if ( $this->properties['max'] === null )
  349. {
  350. $this->calculateMaximum( $this->properties['minValue'], $this->properties['maxValue'] );
  351. }
  352. // Check that the major step size matches up with the min and max
  353. // values on the axis.
  354. $quotient = ( $this->properties['max'] - $this->properties['min'] ) / $this->properties['majorStep'];
  355. $quotient = abs( $quotient - floor( $quotient ) );
  356. if ( ( $quotient >= .00001 ) &&
  357. ( abs( $quotient - 1 ) >= .00001 ) )
  358. {
  359. throw new ezcGraphInvalidStepSizeException( "The difference between minimum and maximum value is not a multiplier of the major step size." );
  360. }
  361. // Check that the minor step size matches up with major step size on
  362. // the axis.
  363. $quotient = $this->properties['majorStep'] / $this->properties['minorStep'];
  364. $quotient = abs( $quotient - floor( $quotient ) );
  365. if ( ( $quotient >= .00001 ) &&
  366. ( abs( $quotient - 1 ) >= .00001 ) )
  367. {
  368. throw new ezcGraphInvalidStepSizeException( "The major step size value is not a multiplier of the minor step size." );
  369. }
  370. }
  371. /**
  372. * Get coordinate for a dedicated value on the chart
  373. *
  374. * @param float $value Value to determine position for
  375. * @return float Position on chart
  376. */
  377. public function getCoordinate( $value )
  378. {
  379. // Force typecast, because ( false < -100 ) results in (bool) true
  380. $floatValue = (float) $value;
  381. if ( ( $value === false ) &&
  382. ( ( $floatValue < $this->properties['min'] ) || ( $floatValue > $this->properties['max'] ) ) )
  383. {
  384. switch ( $this->position )
  385. {
  386. case ezcGraph::LEFT:
  387. case ezcGraph::TOP:
  388. return 0.;
  389. case ezcGraph::RIGHT:
  390. case ezcGraph::BOTTOM:
  391. return 1.;
  392. }
  393. }
  394. else
  395. {
  396. switch ( $this->position )
  397. {
  398. case ezcGraph::LEFT:
  399. case ezcGraph::TOP:
  400. return ( $value - $this->properties['min'] ) / ( $this->properties['max'] - $this->properties['min'] );
  401. case ezcGraph::RIGHT:
  402. case ezcGraph::BOTTOM:
  403. return 1 - ( $value - $this->properties['min'] ) / ( $this->properties['max'] - $this->properties['min'] );
  404. }
  405. }
  406. }
  407. /**
  408. * Return count of minor steps
  409. *
  410. * @return integer Count of minor steps
  411. */
  412. public function getMinorStepCount()
  413. {
  414. return (int) ( ( $this->properties['max'] - $this->properties['min'] ) / $this->properties['minorStep'] );
  415. }
  416. /**
  417. * Return count of major steps
  418. *
  419. * @return integer Count of major steps
  420. */
  421. public function getMajorStepCount()
  422. {
  423. return (int) ( ( $this->properties['max'] - $this->properties['min'] ) / $this->properties['majorStep'] );
  424. }
  425. /**
  426. * Get label for a dedicated step on the axis
  427. *
  428. * @param integer $step Number of step
  429. * @return string label
  430. */
  431. public function getLabel( $step )
  432. {
  433. if ( $this->properties['labelCallback'] !== null )
  434. {
  435. return call_user_func_array(
  436. $this->properties['labelCallback'],
  437. array(
  438. $this->properties['min'] + ( $step * $this->properties['majorStep'] ),
  439. $step,
  440. )
  441. );
  442. }
  443. elseif ( $this->properties['formatString'] !== null )
  444. {
  445. return sprintf( $this->properties['formatString'], $this->properties['min'] + ( $step * $this->properties['majorStep'] ) );
  446. }
  447. else
  448. {
  449. return $this->properties['min'] + ( $step * $this->properties['majorStep'] );
  450. }
  451. }
  452. /**
  453. * Is zero step
  454. *
  455. * Returns true if the given step is the one on the initial axis position
  456. *
  457. * @param int $step Number of step
  458. * @return bool Status If given step is initial axis position
  459. */
  460. public function isZeroStep( $step )
  461. {
  462. return ( $this->getLabel( $step ) == 0 );
  463. }
  464. }
  465. ?>