PageRenderTime 58ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/crypticrod/sr_wp_code
PHP | 292 lines | 155 code | 24 blank | 113 comment | 10 complexity | 1d53371b1dcfe14a197bae8d50419872 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 ezcGraphBarChart 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 bar charts. Can make use of an unlimited amount of datasets and
  12. * will display them as bars by default.
  13. * X axis:
  14. * - Labeled axis
  15. * - Boxed axis label renderer
  16. * Y axis:
  17. * - Numeric axis
  18. * - Exact axis label renderer
  19. *
  20. * <code>
  21. * // Create a new horizontal bar chart
  22. * $chart = new ezcGraphHorizontalBarChart();
  23. *
  24. * // Add data to line chart
  25. * $chart->data['sample dataset'] = new ezcGraphArrayDataSet(
  26. * array(
  27. * '100' => 1.2,
  28. * '200' => 43.2,
  29. * '300' => -34.14,
  30. * '350' => 65,
  31. * '400' => 123,
  32. * )
  33. * );
  34. *
  35. * // Render chart with the special designated renderer and default SVG driver
  36. * $chart->renderer = new ezcGraphHorizontalRenderer();
  37. * $chart->render( 500, 200, 'bar_chart.svg' );
  38. * </code>
  39. *
  40. * Each chart consists of several chart elements which represents logical
  41. * parts of the chart and can be formatted independently. The bar chart
  42. * consists of:
  43. * - title ( {@link ezcGraphChartElementText} )
  44. * - legend ( {@link ezcGraphChartElementLegend} )
  45. * - background ( {@link ezcGraphChartElementBackground} )
  46. * - xAxis ( {@link ezcGraphChartElementLabeledAxis} )
  47. * - yAxis ( {@link ezcGraphChartElementNumericAxis} )
  48. *
  49. * The type of the axis may be changed and all elements can be configured by
  50. * accessing them as properties of the chart:
  51. *
  52. * <code>
  53. * $chart->legend->position = ezcGraph::RIGHT;
  54. * </code>
  55. *
  56. * The chart itself also offers several options to configure the appearance. As
  57. * bar charts extend line charts the the extended configure options are
  58. * available in {@link ezcGraphLineChartOptions} extending the
  59. * {@link ezcGraphChartOptions}.
  60. *
  61. * @property ezcGraphLineChartOptions $options
  62. * Chart options class
  63. *
  64. * @version 1.5
  65. * @package Graph
  66. * @mainclass
  67. */
  68. class ezcGraphHorizontalBarChart extends ezcGraphBarChart
  69. {
  70. /**
  71. * Constructor
  72. *
  73. * @param array $options Default option array
  74. * @return void
  75. * @ignore
  76. */
  77. public function __construct( array $options = array() )
  78. {
  79. parent::__construct();
  80. $this->addElement( 'xAxis', new ezcGraphChartElementNumericAxis() );
  81. $this->elements['xAxis']->axisLabelRenderer = new ezcGraphAxisCenteredLabelRenderer();
  82. $this->elements['xAxis']->position = ezcGraph::LEFT;
  83. $this->addElement( 'yAxis', new ezcGraphChartElementLabeledAxis() );
  84. $this->elements['yAxis']->axisLabelRenderer = new ezcGraphAxisBoxedLabelRenderer();
  85. $this->elements['yAxis']->position = ezcGraph::BOTTOM;
  86. $this->renderer = new ezcGraphHorizontalRenderer();
  87. }
  88. /**
  89. * Render the assigned data
  90. *
  91. * Will renderer all charts data in the remaining boundings after drawing
  92. * all other chart elements. The data will be rendered depending on the
  93. * settings in the dataset.
  94. *
  95. * @param ezcGraphRenderer $renderer Renderer
  96. * @param ezcGraphBoundings $boundings Remaining boundings
  97. * @return void
  98. */
  99. protected function renderData( ezcGraphRenderer $renderer, ezcGraphBoundings $boundings, ezcGraphBoundings $innerBoundings )
  100. {
  101. // Use inner boundings for drawning chart data
  102. $boundings = $innerBoundings;
  103. $yAxisNullPosition = $this->elements['xAxis']->getCoordinate( false );
  104. // Initialize counters
  105. $nr = array();
  106. $count = array();
  107. foreach ( $this->data as $data )
  108. {
  109. if ( !isset( $nr[$data->displayType->default] ) )
  110. {
  111. $nr[$data->displayType->default] = 0;
  112. $count[$data->displayType->default] = 0;
  113. }
  114. $nr[$data->displayType->default]++;
  115. $count[$data->displayType->default]++;
  116. }
  117. $checkedRegularSteps = false;
  118. // Display data
  119. foreach ( $this->data as $datasetName => $data )
  120. {
  121. --$nr[$data->displayType->default];
  122. // Check which axis should be used
  123. $xAxis = ( $data->xAxis->default ? $data->xAxis->default: $this->elements['xAxis'] );
  124. $yAxis = ( $data->yAxis->default ? $data->yAxis->default: $this->elements['yAxis'] );
  125. // Determine fill color for dataset
  126. if ( $this->options->fillLines !== false )
  127. {
  128. $fillColor = clone $data->color->default;
  129. $fillColor->alpha = (int) round( ( 255 - $fillColor->alpha ) * ( $this->options->fillLines / 255 ) );
  130. }
  131. else
  132. {
  133. $fillColor = null;
  134. }
  135. // Ensure regular steps on axis when used with bar charts and
  136. // precalculate some values use to render bar charts
  137. //
  138. // Called only once and only when bars should be rendered
  139. if ( ( $checkedRegularSteps === false ) &&
  140. ( $data->displayType->default === ezcGraph::BAR ) )
  141. {
  142. $height = $this->calculateStepWidth( $yAxis, $xAxis, $boundings->height )->y;
  143. }
  144. // Draw lines for dataset
  145. $lastPoint = false;
  146. foreach ( $data as $key => $value )
  147. {
  148. // Calculate point in chart
  149. $point = $xAxis->axisLabelRenderer->modifyChartDataPosition(
  150. $yAxis->axisLabelRenderer->modifyChartDataPosition(
  151. new ezcGraphCoordinate(
  152. $xAxis->getCoordinate( $value ),
  153. $yAxis->getCoordinate( $key )
  154. )
  155. )
  156. );
  157. // Render depending on display type of dataset
  158. switch ( true )
  159. {
  160. case $data->displayType->default === ezcGraph::BAR:
  161. $renderer->drawHorizontalBar(
  162. $boundings,
  163. new ezcGraphContext( $datasetName, $key, $data->url[$key] ),
  164. $data->color[$key],
  165. $point,
  166. $height,
  167. $nr[$data->displayType->default],
  168. $count[$data->displayType->default],
  169. $data->symbol[$key],
  170. $yAxisNullPosition
  171. );
  172. // Render highlight string if requested
  173. if ( $data->highlight[$key] )
  174. {
  175. $renderer->drawDataHighlightText(
  176. $boundings,
  177. new ezcGraphContext( $datasetName, $key, $data->url[$key] ),
  178. $point,
  179. $yAxisNullPosition,
  180. $nr[$data->displayType->default],
  181. $count[$data->displayType->default],
  182. $this->options->highlightFont,
  183. ( $data->highlightValue[$key] ? $data->highlightValue[$key] : $value ),
  184. $this->options->highlightSize + $this->options->highlightFont->padding * 2,
  185. ( $this->options->highlightLines ? $data->color[$key] : null ),
  186. ( $this->options->highlightXOffset ? $this->options->highlightXOffset : 0 ),
  187. ( $this->options->highlightYOffset ? $this->options->highlightYOffset : 0 ),
  188. $height,
  189. $data->displayType->default
  190. );
  191. }
  192. break;
  193. default:
  194. throw new ezcGraphInvalidDisplayTypeException( $data->displayType->default );
  195. break;
  196. }
  197. // Store last point, used to connect lines in line chart.
  198. $lastPoint = $point;
  199. }
  200. }
  201. }
  202. /**
  203. * Aggregate and calculate value boundings on axis.
  204. *
  205. * This function is nearly the same as in ezcGraphLineChart, but reverses
  206. * the usage of keys and values for the axis.
  207. *
  208. * @return void
  209. */
  210. protected function setAxisValues()
  211. {
  212. // Virtual data set build for agrregated values sums for bar charts
  213. $virtualBarSumDataSet = array( array(), array() );
  214. // Calculate axis scaling and labeling
  215. foreach ( $this->data as $dataset )
  216. {
  217. $nr = 0;
  218. $labels = array();
  219. $values = array();
  220. foreach ( $dataset as $label => $value )
  221. {
  222. $labels[] = $label;
  223. $values[] = $value;
  224. // Build sum of all bars
  225. if ( $this->options->stackBars &&
  226. ( $dataset->displayType->default === ezcGraph::BAR ) )
  227. {
  228. if ( !isset( $virtualBarSumDataSet[(int) $value >= 0][$nr] ) )
  229. {
  230. $virtualBarSumDataSet[(int) $value >= 0][$nr++] = $value;
  231. }
  232. else
  233. {
  234. $virtualBarSumDataSet[(int) $value >= 0][$nr++] += $value;
  235. }
  236. }
  237. }
  238. // Check if data has been associated with another custom axis, use
  239. // default axis otherwise.
  240. if ( $dataset->xAxis->default )
  241. {
  242. $dataset->xAxis->default->addData( $values );
  243. }
  244. else
  245. {
  246. $this->elements['xAxis']->addData( $values );
  247. }
  248. if ( $dataset->yAxis->default )
  249. {
  250. $dataset->yAxis->default->addData( array_reverse( $labels ) );
  251. }
  252. else
  253. {
  254. $this->elements['yAxis']->addData( array_reverse( $labels ) );
  255. }
  256. }
  257. // There should always be something assigned to the main x and y axis.
  258. if ( !$this->elements['xAxis']->initialized ||
  259. !$this->elements['yAxis']->initialized )
  260. {
  261. throw new ezcGraphNoDataException();
  262. }
  263. // Calculate boundings from assigned data
  264. $this->elements['xAxis']->calculateAxisBoundings();
  265. $this->elements['yAxis']->calculateAxisBoundings();
  266. }
  267. }
  268. ?>