/lib/ezc/Graph/src/renderer/axis_label_exact.php

https://github.com/Proudio-Interactive/phpUnderControl · PHP · 304 lines · 213 code · 20 blank · 71 comment · 13 complexity · e3efdd5d453f3aec0999a05785d38c60 MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the ezcGraphAxisExactLabelRenderer class
  4. *
  5. * @package Graph
  6. * @version 1.4.3
  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. * Renders axis labels like known from charts drawn in analysis
  12. *
  13. * <code>
  14. * $chart->xAxis->axisLabelRenderer = new ezcGraphAxisExactLabelRenderer();
  15. * </code>
  16. *
  17. * @property bool $showLastValue
  18. * Show the last value on the axis, which will be aligned different
  19. * than all other values, to not interfere with the arrow head of
  20. * the axis.
  21. * @property bool $renderLastOutside
  22. * Render the last label outside of the normal axis label boundings
  23. * next to the chart boundings. This may interfere with axis labels
  24. * or cause small font size with a low axisSpace.
  25. *
  26. * @version 1.4.3
  27. * @package Graph
  28. * @mainclass
  29. */
  30. class ezcGraphAxisExactLabelRenderer extends ezcGraphAxisLabelRenderer
  31. {
  32. /**
  33. * Constructor
  34. *
  35. * @param array $options Default option array
  36. * @return void
  37. * @ignore
  38. */
  39. public function __construct( array $options = array() )
  40. {
  41. $this->properties['showLastValue'] = true;
  42. $this->properties['renderLastOutside'] = false;
  43. parent::__construct( $options );
  44. }
  45. /**
  46. * __set
  47. *
  48. * @param mixed $propertyName
  49. * @param mixed $propertyValue
  50. * @throws ezcBaseValueException
  51. * If a submitted parameter was out of range or type.
  52. * @throws ezcBasePropertyNotFoundException
  53. * If a the value for the property options is not an instance of
  54. * @return void
  55. * @ignore
  56. */
  57. public function __set( $propertyName, $propertyValue )
  58. {
  59. switch ( $propertyName )
  60. {
  61. case 'showLastValue':
  62. case 'renderLastOutside':
  63. if ( !is_bool( $propertyValue ) )
  64. {
  65. throw new ezcBaseValueException( $propertyName, $propertyValue, 'bool' );
  66. }
  67. $this->properties[$propertyName] = (bool) $propertyValue;
  68. break;
  69. default:
  70. return parent::__set( $propertyName, $propertyValue );
  71. }
  72. }
  73. /**
  74. * Render Axis labels
  75. *
  76. * Render labels for an axis.
  77. *
  78. * @param ezcGraphRenderer $renderer Renderer used to draw the chart
  79. * @param ezcGraphBoundings $boundings Boundings of the axis
  80. * @param ezcGraphCoordinate $start Axis starting point
  81. * @param ezcGraphCoordinate $end Axis ending point
  82. * @param ezcGraphChartElementAxis $axis Axis instance
  83. * @return void
  84. */
  85. public function renderLabels(
  86. ezcGraphRenderer $renderer,
  87. ezcGraphBoundings $boundings,
  88. ezcGraphCoordinate $start,
  89. ezcGraphCoordinate $end,
  90. ezcGraphChartElementAxis $axis,
  91. ezcGraphBoundings $innerBoundings = null )
  92. {
  93. // receive rendering parameters from axis
  94. $steps = $axis->getSteps();
  95. $axisBoundings = new ezcGraphBoundings(
  96. $start->x, $start->y,
  97. $end->x, $end->y
  98. );
  99. // Determine normalized axis direction
  100. $direction = new ezcGraphVector(
  101. $end->x - $start->x,
  102. $end->y - $start->y
  103. );
  104. $direction->unify();
  105. // Get axis space
  106. $gridBoundings = null;
  107. list( $xSpace, $ySpace ) = $this->getAxisSpace( $renderer, $boundings, $axis, $innerBoundings, $gridBoundings );
  108. // Draw steps and grid
  109. foreach ( $steps as $nr => $step )
  110. {
  111. $position = new ezcGraphCoordinate(
  112. $start->x + ( $end->x - $start->x ) * $step->position,
  113. $start->y + ( $end->y - $start->y ) * $step->position
  114. );
  115. $stepSize = new ezcGraphCoordinate(
  116. $axisBoundings->width * $step->width,
  117. $axisBoundings->height * $step->width
  118. );
  119. if ( ! $step->isZero )
  120. {
  121. // major grid
  122. if ( $axis->majorGrid )
  123. {
  124. $this->drawGrid(
  125. $renderer,
  126. $gridBoundings,
  127. $position,
  128. $stepSize,
  129. $axis->majorGrid
  130. );
  131. }
  132. // major step
  133. $this->drawStep(
  134. $renderer,
  135. $position,
  136. $direction,
  137. $axis->position,
  138. $this->majorStepSize,
  139. $axis->border
  140. );
  141. }
  142. if ( $this->showLabels )
  143. {
  144. switch ( $axis->position )
  145. {
  146. case ezcGraph::RIGHT:
  147. case ezcGraph::LEFT:
  148. $labelWidth = $axisBoundings->width *
  149. $steps[$nr - $step->isLast]->width /
  150. ( $this->showLastValue + 1 );
  151. $labelHeight = $ySpace;
  152. if ( ( $this->renderLastOutside === true ) &&
  153. ( $step->isLast === true ) )
  154. {
  155. $labelWidth = ( $boundings->width - $axisBoundings->width ) / 2;
  156. }
  157. break;
  158. case ezcGraph::BOTTOM:
  159. case ezcGraph::TOP:
  160. $labelWidth = $xSpace;
  161. $labelHeight = $axisBoundings->height *
  162. $steps[$nr - $step->isLast]->width /
  163. ( $this->showLastValue + 1 );
  164. if ( ( $this->renderLastOutside === true ) &&
  165. ( $step->isLast === true ) )
  166. {
  167. $labelHeight = ( $boundings->height - $axisBoundings->height ) / 2;
  168. }
  169. break;
  170. }
  171. $showLabel = true;
  172. switch ( true )
  173. {
  174. case ( !$this->showLastValue && $step->isLast ):
  175. // Skip last step if showLastValue is false
  176. $showLabel = false;
  177. break;
  178. // Draw label at top left of step
  179. case ( ( $axis->position === ezcGraph::BOTTOM ) &&
  180. ( !$step->isLast ) ) ||
  181. ( ( $axis->position === ezcGraph::BOTTOM ) &&
  182. ( $step->isLast ) &&
  183. ( $this->renderLastOutside ) ) ||
  184. ( ( $axis->position === ezcGraph::TOP ) &&
  185. ( $step->isLast ) &&
  186. ( !$this->renderLastOutside ) ):
  187. $labelBoundings = new ezcGraphBoundings(
  188. $position->x - $labelWidth + $this->labelPadding,
  189. $position->y - $labelHeight + $this->labelPadding,
  190. $position->x - $this->labelPadding,
  191. $position->y - $this->labelPadding
  192. );
  193. $alignement = ezcGraph::RIGHT | ezcGraph::BOTTOM;
  194. break;
  195. // Draw label at bottom right of step
  196. case ( ( $axis->position === ezcGraph::LEFT ) &&
  197. ( !$step->isLast ) ) ||
  198. ( ( $axis->position === ezcGraph::LEFT ) &&
  199. ( $step->isLast ) &&
  200. ( $this->renderLastOutside ) ) ||
  201. ( ( $axis->position === ezcGraph::RIGHT ) &&
  202. ( $step->isLast ) &&
  203. ( !$this->renderLastOutside ) ):
  204. $labelBoundings = new ezcGraphBoundings(
  205. $position->x + $this->labelPadding,
  206. $position->y + $this->labelPadding,
  207. $position->x + $labelWidth - $this->labelPadding,
  208. $position->y + $labelHeight - $this->labelPadding
  209. );
  210. $alignement = ezcGraph::LEFT | ezcGraph::TOP;
  211. break;
  212. // Draw label at bottom left of step
  213. case ( ( $axis->position === ezcGraph::TOP ) &&
  214. ( !$step->isLast ) ) ||
  215. ( ( $axis->position === ezcGraph::TOP ) &&
  216. ( $step->isLast ) &&
  217. ( $this->renderLastOutside ) ) ||
  218. ( ( $axis->position === ezcGraph::RIGHT ) &&
  219. ( !$step->isLast ) ) ||
  220. ( ( $axis->position === ezcGraph::RIGHT ) &&
  221. ( $step->isLast ) &&
  222. ( $this->renderLastOutside ) ) ||
  223. ( ( $axis->position === ezcGraph::BOTTOM ) &&
  224. ( $step->isLast ) &&
  225. ( !$this->renderLastOutside ) ) ||
  226. ( ( $axis->position === ezcGraph::LEFT ) &&
  227. ( $step->isLast ) &&
  228. ( !$this->renderLastOutside ) ):
  229. $labelBoundings = new ezcGraphBoundings(
  230. $position->x - $labelWidth + $this->labelPadding,
  231. $position->y + $this->labelPadding,
  232. $position->x - $this->labelPadding,
  233. $position->y + $labelHeight - $this->labelPadding
  234. );
  235. $alignement = ezcGraph::RIGHT | ezcGraph::TOP;
  236. break;
  237. }
  238. if ( $showLabel )
  239. {
  240. $renderer->drawText(
  241. $labelBoundings,
  242. $step->label,
  243. $alignement
  244. );
  245. }
  246. }
  247. if ( !$step->isLast )
  248. {
  249. // Iterate over minor steps
  250. foreach ( $step->childs as $minorStep )
  251. {
  252. $minorStepPosition = new ezcGraphCoordinate(
  253. $start->x + ( $end->x - $start->x ) * $minorStep->position,
  254. $start->y + ( $end->y - $start->y ) * $minorStep->position
  255. );
  256. $minorStepSize = new ezcGraphCoordinate(
  257. $axisBoundings->width * $minorStep->width,
  258. $axisBoundings->height * $minorStep->width
  259. );
  260. if ( $axis->minorGrid )
  261. {
  262. $this->drawGrid(
  263. $renderer,
  264. $gridBoundings,
  265. $minorStepPosition,
  266. $minorStepSize,
  267. $axis->minorGrid
  268. );
  269. }
  270. // major step
  271. $this->drawStep(
  272. $renderer,
  273. $minorStepPosition,
  274. $direction,
  275. $axis->position,
  276. $this->minorStepSize,
  277. $axis->border
  278. );
  279. }
  280. }
  281. }
  282. }
  283. }
  284. ?>