PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/ezc/Graph/src/interfaces/chart.php

https://github.com/Proudio-Interactive/phpUnderControl
PHP | 296 lines | 142 code | 27 blank | 127 comment | 11 complexity | 845ae5c0923bd38860ddab0c373dd8df MD5 | raw file
  1. <?php
  2. /**
  3. * File containing the abstract ezcGraphChart 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. * Class to represent a complete chart.
  12. *
  13. * @property ezcGraphRenderer $renderer
  14. * Renderer used to render chart
  15. * @property ezcGraphDriver $driver
  16. * Output driver used for chart
  17. * @property ezcGraphPalette $palette
  18. * Palette used for colorization of chart
  19. * @property-read mixed $renderedFile
  20. * Contains the filename of the rendered file, if rendered.
  21. *
  22. * @package Graph
  23. * @version 1.4.3
  24. */
  25. abstract class ezcGraphChart
  26. {
  27. /**
  28. * Contains all general chart options
  29. *
  30. * @var ezcGraphChartConfig
  31. */
  32. protected $options;
  33. /**
  34. * Contains subelelemnts of the chart like legend and axes
  35. *
  36. * @var array(ezcGraphChartElement)
  37. */
  38. protected $elements = array();
  39. /**
  40. * Contains the data of the chart
  41. *
  42. * @var ezcGraphChartDataContainer
  43. */
  44. protected $data;
  45. /**
  46. * Array containing chart properties
  47. *
  48. * @var array
  49. */
  50. protected $properties;
  51. /**
  52. * Contains the status wheather an element should be rendered
  53. *
  54. * @var array
  55. */
  56. protected $renderElement;
  57. /**
  58. * Constructor
  59. *
  60. * @param array $options Default option array
  61. * @return void
  62. * @ignore
  63. */
  64. public function __construct( array $options = array() )
  65. {
  66. $this->palette = new ezcGraphPaletteTango();
  67. $this->data = new ezcGraphChartDataContainer( $this );
  68. // Add standard elements
  69. $this->addElement( 'background', new ezcGraphChartElementBackground() );
  70. $this->elements['background']->position = ezcGraph::CENTER | ezcGraph::MIDDLE;
  71. $this->addElement( 'title', new ezcGraphChartElementText() );
  72. $this->elements['title']->position = ezcGraph::TOP;
  73. $this->renderElement['title'] = false;
  74. $this->addElement( 'subtitle', new ezcGraphChartElementText() );
  75. $this->elements['subtitle']->position = ezcGraph::TOP;
  76. $this->renderElement['subtitle'] = false;
  77. $this->addElement( 'legend', new ezcGraphChartElementLegend() );
  78. $this->elements['legend']->position = ezcGraph::LEFT;
  79. // Define standard renderer and driver
  80. $this->properties['driver'] = new ezcGraphSvgDriver();
  81. $this->properties['renderer'] = new ezcGraphRenderer2d();
  82. $this->properties['renderer']->setDriver( $this->driver );
  83. // Initialize other properties
  84. $this->properties['renderedFile'] = null;
  85. }
  86. /**
  87. * Add element to chart
  88. *
  89. * Add a chart element to the chart and perform the required configuration
  90. * tasks for the chart element.
  91. *
  92. * @param string $name Element name
  93. * @param ezcGraphChartElement $element Chart element
  94. * @return void
  95. */
  96. protected function addElement( $name, ezcGraphChartElement $element )
  97. {
  98. $this->elements[$name] = $element;
  99. $this->elements[$name]->font = $this->options->font;
  100. $this->elements[$name]->setFromPalette( $this->palette );
  101. // Render element by default
  102. $this->renderElement[$name] = true;
  103. }
  104. /**
  105. * Options write access
  106. *
  107. * @throws ezcBasePropertyNotFoundException
  108. * If Option could not be found
  109. * @throws ezcBaseValueException
  110. * If value is out of range
  111. * @param mixed $propertyName Option name
  112. * @param mixed $propertyValue Option value;
  113. * @return void
  114. * @ignore
  115. */
  116. public function __set( $propertyName, $propertyValue )
  117. {
  118. switch ( $propertyName ) {
  119. case 'title':
  120. case 'subtitle':
  121. $this->elements[$propertyName]->title = $propertyValue;
  122. $this->renderElement[$propertyName] = true;
  123. break;
  124. case 'background':
  125. $this->elements[$propertyName]->color = $propertyValue;
  126. break;
  127. case 'legend':
  128. if ( !is_bool( $propertyValue ) )
  129. {
  130. throw new ezcBaseValueException( $propertyName, $propertyValue, 'boolean' );
  131. }
  132. $this->renderElement['legend'] = (bool) $propertyValue;
  133. break;
  134. case 'renderer':
  135. if ( $propertyValue instanceof ezcGraphRenderer )
  136. {
  137. $this->properties['renderer'] = $propertyValue;
  138. $this->properties['renderer']->setDriver( $this->driver );
  139. return $this->properties['renderer'];
  140. }
  141. else
  142. {
  143. throw new ezcBaseValueException( $propertyName, $propertyValue, 'ezcGraphRenderer' );
  144. }
  145. break;
  146. case 'driver':
  147. if ( $propertyValue instanceof ezcGraphDriver )
  148. {
  149. $this->properties['driver'] = $propertyValue;
  150. $this->properties['renderer']->setDriver( $this->driver );
  151. return $this->properties['driver'];
  152. }
  153. else
  154. {
  155. throw new ezcBaseValueException( $propertyName, $propertyValue, 'ezcGraphDriver' );
  156. }
  157. break;
  158. case 'palette':
  159. if ( $propertyValue instanceof ezcGraphPalette )
  160. {
  161. $this->properties['palette'] = $propertyValue;
  162. $this->setFromPalette( $this->palette );
  163. }
  164. else
  165. {
  166. throw new ezcBaseValueException( "palette", $propertyValue, "instanceof ezcGraphPalette" );
  167. }
  168. break;
  169. case 'renderedFile':
  170. $this->properties['renderedFile'] = (string) $propertyValue;
  171. break;
  172. case 'options':
  173. if ( $propertyValue instanceof ezcGraphChartOptions )
  174. {
  175. $this->options = $propertyValue;
  176. }
  177. else
  178. {
  179. throw new ezcBaseValueException( "options", $propertyValue, "instanceof ezcGraphOptions" );
  180. }
  181. default:
  182. throw new ezcBasePropertyNotFoundException( $propertyName );
  183. break;
  184. }
  185. }
  186. /**
  187. * Set colors and border fro this element
  188. *
  189. * @param ezcGraphPalette $palette Palette
  190. * @return void
  191. */
  192. public function setFromPalette( ezcGraphPalette $palette )
  193. {
  194. $this->options->font->name = $palette->fontName;
  195. $this->options->font->color = $palette->fontColor;
  196. foreach ( $this->elements as $element )
  197. {
  198. $element->setFromPalette( $palette );
  199. }
  200. }
  201. /**
  202. * __get
  203. *
  204. * @param mixed $propertyName
  205. * @throws ezcBasePropertyNotFoundException
  206. * If a the value for the property options is not an instance of
  207. * @return mixed
  208. * @ignore
  209. */
  210. public function __get( $propertyName )
  211. {
  212. if ( array_key_exists( $propertyName, $this->properties ) )
  213. {
  214. return $this->properties[$propertyName];
  215. }
  216. if ( isset( $this->elements[$propertyName] ) )
  217. {
  218. return $this->elements[$propertyName];
  219. }
  220. if ( ( $propertyName === 'options' ) ||
  221. ( $propertyName === 'data' ) )
  222. {
  223. return $this->$propertyName;
  224. }
  225. else
  226. {
  227. throw new ezcGraphNoSuchElementException( $propertyName );
  228. }
  229. }
  230. /**
  231. * Returns the default display type of the current chart type.
  232. *
  233. * @return int Display type
  234. */
  235. abstract public function getDefaultDisplayType();
  236. /**
  237. * Return filename of rendered file, and false if no file was yet rendered.
  238. *
  239. * @return mixed
  240. */
  241. public function getRenderedFile()
  242. {
  243. return ( $this->renderedFile !== null ? $this->renderedFile : false );
  244. }
  245. /**
  246. * Renders this chart
  247. *
  248. * Creates basic visual chart elements from the chart to be processed by
  249. * the renderer.
  250. *
  251. * @param int $width
  252. * @param int $height
  253. * @param string $file
  254. * @return void
  255. */
  256. abstract public function render( $width, $height, $file = null );
  257. /**
  258. * Renders this chart to direct output
  259. *
  260. * Does the same as ezcGraphChart::render(), but renders directly to
  261. * output and not into a file.
  262. *
  263. * @param int $width
  264. * @param int $height
  265. * @return void
  266. */
  267. abstract public function renderToOutput( $width, $height );
  268. }
  269. ?>