PageRenderTime 59ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/zeta/Graph/src/renderer/axis_label_centered.php

https://gitlab.com/OnBlox/OnBlox-Template
PHP | 282 lines | 176 code | 19 blank | 87 comment | 12 complexity | cf73d4a83519615f4d4a4904083fe3b9 MD5 | raw file
  1. <?php
  2. /**
  3. * File containing the ezcGraphAxisCenteredLabelRenderer class
  4. *
  5. * Licensed to the Apache Software Foundation (ASF) under one
  6. * or more contributor license agreements. See the NOTICE file
  7. * distributed with this work for additional information
  8. * regarding copyright ownership. The ASF licenses this file
  9. * to you under the Apache License, Version 2.0 (the
  10. * "License"); you may not use this file except in compliance
  11. * with the License. You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing,
  16. * software distributed under the License is distributed on an
  17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18. * KIND, either express or implied. See the License for the
  19. * specific language governing permissions and limitations
  20. * under the License.
  21. *
  22. * @package Graph
  23. * @version //autogentag//
  24. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  25. */
  26. /**
  27. * Renders axis labels centered below the axis steps.
  28. *
  29. * <code>
  30. * $chart->xAxis->axisLabelRenderer = new ezcGraphAxisCenteredLabelRenderer();
  31. * </code>
  32. *
  33. * @property bool $showZeroValue
  34. * Show the value at the zero point of an axis. This value might be
  35. * crossed by the other axis which would result in an unreadable
  36. * label.
  37. *
  38. * @version //autogentag//
  39. * @package Graph
  40. * @mainclass
  41. */
  42. class ezcGraphAxisCenteredLabelRenderer extends ezcGraphAxisLabelRenderer
  43. {
  44. /**
  45. * Constructor
  46. *
  47. * @param array $options Default option array
  48. * @return void
  49. * @ignore
  50. */
  51. public function __construct( array $options = array() )
  52. {
  53. $this->properties['showZeroValue'] = false;
  54. parent::__construct( $options );
  55. }
  56. /**
  57. * __set
  58. *
  59. * @param mixed $propertyName
  60. * @param mixed $propertyValue
  61. * @throws ezcBaseValueException
  62. * If a submitted parameter was out of range or type.
  63. * @throws ezcBasePropertyNotFoundException
  64. * If a the value for the property options is not an instance of
  65. * @return void
  66. * @ignore
  67. */
  68. public function __set( $propertyName, $propertyValue )
  69. {
  70. switch ( $propertyName )
  71. {
  72. case 'showZeroValue':
  73. if ( !is_bool( $propertyValue ) )
  74. {
  75. throw new ezcBaseValueException( $propertyName, $propertyValue, 'bool' );
  76. }
  77. $this->properties['showZeroValue'] = (bool) $propertyValue;
  78. break;
  79. default:
  80. return parent::__set( $propertyName, $propertyValue );
  81. }
  82. }
  83. /**
  84. * Render Axis labels
  85. *
  86. * Render labels for an axis.
  87. *
  88. * @param ezcGraphRenderer $renderer Renderer used to draw the chart
  89. * @param ezcGraphBoundings $boundings Boundings of the axis
  90. * @param ezcGraphCoordinate $start Axis starting point
  91. * @param ezcGraphCoordinate $end Axis ending point
  92. * @param ezcGraphChartElementAxis $axis Axis instance
  93. * @return void
  94. */
  95. public function renderLabels(
  96. ezcGraphRenderer $renderer,
  97. ezcGraphBoundings $boundings,
  98. ezcGraphCoordinate $start,
  99. ezcGraphCoordinate $end,
  100. ezcGraphChartElementAxis $axis,
  101. ezcGraphBoundings $innerBoundings = null )
  102. {
  103. // receive rendering parameters from axis
  104. $steps = $axis->getSteps();
  105. $axisBoundings = new ezcGraphBoundings(
  106. $start->x, $start->y,
  107. $end->x, $end->y
  108. );
  109. // Determine normalized axis direction
  110. $direction = new ezcGraphVector(
  111. $end->x - $start->x,
  112. $end->y - $start->y
  113. );
  114. $direction->unify();
  115. // Get axis space
  116. $gridBoundings = null;
  117. list( $xSpace, $ySpace ) = $this->getAxisSpace( $renderer, $boundings, $axis, $innerBoundings, $gridBoundings );
  118. // Draw steps and grid
  119. foreach ( $steps as $nr => $step )
  120. {
  121. $position = new ezcGraphCoordinate(
  122. $start->x + ( $end->x - $start->x ) * $step->position,
  123. $start->y + ( $end->y - $start->y ) * $step->position
  124. );
  125. $stepSize = new ezcGraphCoordinate(
  126. $axisBoundings->width * $step->width,
  127. $axisBoundings->height * $step->width
  128. );
  129. if ( ! $step->isZero )
  130. {
  131. // major grid
  132. if ( $axis->majorGrid )
  133. {
  134. $this->drawGrid(
  135. $renderer,
  136. $gridBoundings,
  137. $position,
  138. $stepSize,
  139. $axis->majorGrid
  140. );
  141. }
  142. // major step
  143. $this->drawStep(
  144. $renderer,
  145. $position,
  146. $direction,
  147. $axis->position,
  148. $this->majorStepSize,
  149. $axis->border
  150. );
  151. }
  152. // draw label
  153. if ( $this->showLabels && ( $this->showZeroValue || ! $step->isZero ) )
  154. {
  155. // Calculate label boundings
  156. if ( abs( $direction->x ) > abs( $direction->y ) )
  157. {
  158. // Horizontal labels
  159. switch ( true )
  160. {
  161. case ( $nr === 0 ):
  162. // First label
  163. $labelSize = min(
  164. $xSpace * 2,
  165. $step->width * $axisBoundings->width
  166. );
  167. break;
  168. case ( $step->isLast ):
  169. // Last label
  170. $labelSize = min(
  171. $xSpace * 2,
  172. $steps[$nr - 1]->width * $axisBoundings->width
  173. );
  174. break;
  175. default:
  176. $labelSize = min(
  177. $step->width * $axisBoundings->width,
  178. $steps[$nr - 1]->width * $axisBoundings->width
  179. );
  180. break;
  181. }
  182. $labelBoundings = new ezcGraphBoundings(
  183. $position->x - $labelSize / 2 + $this->labelPadding,
  184. $position->y + $this->labelPadding,
  185. $position->x + $labelSize / 2 - $this->labelPadding,
  186. $position->y + $ySpace - $this->labelPadding
  187. );
  188. $alignement = ezcGraph::CENTER | ezcGraph::TOP;
  189. }
  190. else
  191. {
  192. // Vertical labels
  193. switch ( true )
  194. {
  195. case ( $nr === 0 ):
  196. // First label
  197. $labelSize = min(
  198. $ySpace * 2,
  199. $step->width * $axisBoundings->height
  200. );
  201. break;
  202. case ( $step->isLast ):
  203. // Last label
  204. $labelSize = min(
  205. $ySpace * 2,
  206. $steps[$nr - 1]->width * $axisBoundings->height
  207. );
  208. break;
  209. default:
  210. $labelSize = min(
  211. $step->width * $axisBoundings->height,
  212. $steps[$nr - 1]->width * $axisBoundings->height
  213. );
  214. break;
  215. }
  216. $labelBoundings = new ezcGraphBoundings(
  217. $position->x - $xSpace + $this->labelPadding,
  218. $position->y - $labelSize / 2 + $this->labelPadding,
  219. $position->x - $this->labelPadding,
  220. $position->y + $labelSize / 2 - $this->labelPadding
  221. );
  222. $alignement = ezcGraph::MIDDLE | ezcGraph::RIGHT;
  223. }
  224. $renderer->drawText( $labelBoundings, $step->label, $alignement );
  225. }
  226. // Iterate over minor steps
  227. if ( !$step->isLast )
  228. {
  229. foreach ( $step->childs as $minorStep )
  230. {
  231. $minorStepPosition = new ezcGraphCoordinate(
  232. $start->x + ( $end->x - $start->x ) * $minorStep->position,
  233. $start->y + ( $end->y - $start->y ) * $minorStep->position
  234. );
  235. $minorStepSize = new ezcGraphCoordinate(
  236. $axisBoundings->width * $minorStep->width,
  237. $axisBoundings->height * $minorStep->width
  238. );
  239. if ( $axis->minorGrid )
  240. {
  241. $this->drawGrid(
  242. $renderer,
  243. $gridBoundings,
  244. $minorStepPosition,
  245. $minorStepSize,
  246. $axis->minorGrid
  247. );
  248. }
  249. // major step
  250. $this->drawStep(
  251. $renderer,
  252. $minorStepPosition,
  253. $direction,
  254. $axis->position,
  255. $this->minorStepSize,
  256. $axis->border
  257. );
  258. }
  259. }
  260. }
  261. }
  262. }
  263. ?>