PageRenderTime 64ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/MantisBT/library/ezc/Graph/src/charts/odometer.php

https://bitbucket.org/crypticrod/sr_wp_code
PHP | 296 lines | 124 code | 24 blank | 148 comment | 7 complexity | d48b1230f2965349c510d42b05c14f3a 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 ezcGraphOdometerChart 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 for odometer charts. Can only use one dataset which will be dispalyed
  12. * as a odometer chart.
  13. *
  14. * <code>
  15. * $graph = new ezcGraphOdometerChart();
  16. * $graph->title = 'Custom odometer';
  17. *
  18. * $graph->data['data'] = new ezcGraphArrayDataSet(
  19. * array( 87 )
  20. * );
  21. *
  22. * // Set the marker color
  23. * $graph->data['data']->color[0] = '#A0000055';
  24. *
  25. * // Set colors for the background gradient
  26. * $graph->options->startColor = '#2E3436';
  27. * $graph->options->endColor = '#EEEEEC';
  28. *
  29. * // Define a border for the odometer
  30. * $graph->options->borderWidth = 2;
  31. * $graph->options->borderColor = '#BABDB6';
  32. *
  33. * // Set marker width
  34. * $graph->options->markerWidth = 5;
  35. *
  36. * // Set space, which the odometer may consume
  37. * $graph->options->odometerHeight = .7;
  38. *
  39. * // Set axis span and label
  40. * $graph->axis->min = 0;
  41. * $graph->axis->max = 100;
  42. * $graph->axis->label = 'Coverage ';
  43. *
  44. * $graph->render( 400, 150, 'custom_odometer_chart.svg' );
  45. * </code>
  46. *
  47. * Each chart consists of several chart elements which represents logical parts
  48. * of the chart and can be formatted independently. The odometer chart consists
  49. * of:
  50. * - title ( {@link ezcGraphChartElementText} )
  51. * - background ( {@link ezcGraphChartElementBackground} )
  52. *
  53. * All elements can be configured by accessing them as properties of the chart:
  54. *
  55. * <code>
  56. * $chart->title->position = ezcGraph::BOTTOM;
  57. * </code>
  58. *
  59. * The chart itself also offers several options to configure the appearance.
  60. * The extended configure options are available in
  61. * {@link ezcGraphOdometerChartOptions} extending the {@link
  62. * ezcGraphChartOptions}.
  63. *
  64. * @property ezcGraphOdometerChartOptions $options
  65. * Chart options class
  66. *
  67. * @version 1.5
  68. * @package Graph
  69. * @mainclass
  70. */
  71. class ezcGraphOdometerChart extends ezcGraphChart
  72. {
  73. /**
  74. * Constructor
  75. *
  76. * @param array $options Default option array
  77. * @return void
  78. * @ignore
  79. */
  80. public function __construct( array $options = array() )
  81. {
  82. $this->options = new ezcGraphOdometerChartOptions( $options );
  83. parent::__construct( $options );
  84. $this->data = new ezcGraphChartSingleDataContainer( $this );
  85. $this->addElement( 'axis', new ezcGraphChartElementNumericAxis());
  86. $this->elements['axis']->axisLabelRenderer = new ezcGraphAxisCenteredLabelRenderer();
  87. $this->elements['axis']->axisLabelRenderer->showZeroValue = true;
  88. $this->elements['axis']->position = ezcGraph::LEFT;
  89. $this->elements['axis']->axisSpace = .05;
  90. }
  91. /**
  92. * Property write access
  93. *
  94. * @throws ezcBasePropertyNotFoundException
  95. * If Option could not be found
  96. * @throws ezcBaseValueException
  97. * If value is out of range
  98. * @param string $propertyName Option name
  99. * @param mixed $propertyValue Option value;
  100. * @return void
  101. * @ignore
  102. */
  103. public function __set( $propertyName, $propertyValue )
  104. {
  105. switch ( $propertyName ) {
  106. case 'axis':
  107. if ( $propertyValue instanceof ezcGraphChartElementAxis )
  108. {
  109. $this->addElement( 'axis', $propertyValue );
  110. $this->elements['axis']->axisLabelRenderer = new ezcGraphAxisCenteredLabelRenderer();
  111. $this->elements['axis']->axisLabelRenderer->showZeroValue = true;
  112. $this->elements['axis']->position = ezcGraph::LEFT;
  113. $this->elements['axis']->axisSpace = .05;
  114. }
  115. else
  116. {
  117. throw new ezcBaseValueException( $propertyName, $propertyValue, 'ezcGraphChartElementAxis' );
  118. }
  119. break;
  120. case 'renderer':
  121. if ( $propertyValue instanceof ezcGraphOdometerRenderer )
  122. {
  123. parent::__set( $propertyName, $propertyValue );
  124. }
  125. else
  126. {
  127. throw new ezcBaseValueException( $propertyName, $propertyValue, 'ezcGraphOdometerRenderer' );
  128. }
  129. break;
  130. default:
  131. parent::__set( $propertyName, $propertyValue );
  132. }
  133. }
  134. /**
  135. * Render the assigned data
  136. *
  137. * Will renderer all charts data in the remaining boundings after drawing
  138. * all other chart elements. The data will be rendered depending on the
  139. * settings in the dataset.
  140. *
  141. * @param ezcGraphRenderer $renderer Renderer
  142. * @param ezcGraphBoundings $boundings Remaining boundings
  143. * @return void
  144. */
  145. protected function renderData( ezcGraphRenderer $renderer, ezcGraphBoundings $boundings )
  146. {
  147. // Draw the odometer data
  148. $dataset = $this->data->rewind();
  149. foreach ( $dataset as $key => $value )
  150. {
  151. $renderer->drawOdometerMarker(
  152. $boundings,
  153. $this->elements['axis']->axisLabelRenderer->modifyChartDataPosition(
  154. new ezcGraphCoordinate(
  155. $this->elements['axis']->getCoordinate( $value ),
  156. 0
  157. )
  158. ),
  159. $dataset->symbol[$key],
  160. $dataset->color[$key],
  161. $this->options->markerWidth
  162. );
  163. }
  164. }
  165. /**
  166. * Returns the default display type of the current chart type.
  167. *
  168. * @return int Display type
  169. */
  170. public function getDefaultDisplayType()
  171. {
  172. return ezcGraph::ODOMETER;
  173. }
  174. /**
  175. * Renders the basic elements of this chart type
  176. *
  177. * @param int $width
  178. * @param int $height
  179. * @return void
  180. */
  181. protected function renderElements( $width, $height )
  182. {
  183. if ( !count( $this->data ) )
  184. {
  185. throw new ezcGraphNoDataException();
  186. }
  187. // Set image properties in driver
  188. $this->driver->options->width = $width;
  189. $this->driver->options->height = $height;
  190. // no legend
  191. $this->renderElement['legend'] = false;
  192. // Get boundings from parameters
  193. $this->options->width = $width;
  194. $this->options->height = $height;
  195. $boundings = new ezcGraphBoundings();
  196. $boundings->x1 = $this->options->width;
  197. $boundings->y1 = $this->options->height;
  198. // Get values out the single used dataset to calculate axis boundings
  199. $values = array();
  200. foreach ( $this->data->rewind() as $value )
  201. {
  202. $values[] = $value;
  203. }
  204. // Set values for Axis
  205. $this->elements['axis']->addData( $values );
  206. $this->elements['axis']->nullPosition = 0.5 + $this->options->odometerHeight / 2;
  207. $this->elements['axis']->calculateAxisBoundings();
  208. // Render subelements exept axis, which will be drawn together with the
  209. // odometer bar
  210. foreach ( $this->elements as $name => $element )
  211. {
  212. // Skip element, if it should not get rendered
  213. if ( $this->renderElement[$name] === false ||
  214. $name === 'axis' )
  215. {
  216. continue;
  217. }
  218. $this->driver->options->font = $element->font;
  219. $boundings = $element->render( $this->renderer, $boundings );
  220. }
  221. // Draw basic odometer
  222. $this->driver->options->font = $this->elements['axis']->font;
  223. $boundings = $this->renderer->drawOdometer(
  224. $boundings,
  225. $this->elements['axis'],
  226. $this->options
  227. );
  228. // Render graph
  229. $this->renderData( $this->renderer, $boundings );
  230. }
  231. /**
  232. * Render the pie chart
  233. *
  234. * Renders the chart into a file or stream. The width and height are
  235. * needed to specify the dimensions of the resulting image. For direct
  236. * output use 'php://stdout' as output file.
  237. *
  238. * @param int $width Image width
  239. * @param int $height Image height
  240. * @param string $file Output file
  241. * @apichange
  242. * @return void
  243. */
  244. public function render( $width, $height, $file = null )
  245. {
  246. $this->renderElements( $width, $height );
  247. if ( !empty( $file ) )
  248. {
  249. $this->renderer->render( $file );
  250. }
  251. $this->renderedFile = $file;
  252. }
  253. /**
  254. * Renders this chart to direct output
  255. *
  256. * Does the same as ezcGraphChart::render(), but renders directly to
  257. * output and not into a file.
  258. *
  259. * @param int $width
  260. * @param int $height
  261. * @apichange
  262. * @return void
  263. */
  264. public function renderToOutput( $width, $height )
  265. {
  266. // @TODO: merge this function with render an deprecate ommit of third
  267. // argument in render() when API break is possible
  268. $this->renderElements( $width, $height );
  269. $this->renderer->render( null );
  270. }
  271. }
  272. ?>