PageRenderTime 89ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/graph-engine/graph.bar.php

https://gitlab.com/ElvisAns/tiki
PHP | 338 lines | 278 code | 55 blank | 5 comment | 26 complexity | fa4fa69a0d8bbaa69403010e16a1b735 MD5 | raw file
  1. <?php
  2. // (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
  3. //
  4. // All Rights Reserved. See copyright.txt for details and a complete list of authors.
  5. // Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
  6. // $Id$
  7. require_once 'lib/graph-engine/abstract.gridbased.php';
  8. class BarBasedGraphic extends GridBasedGraphic // {{{1
  9. {
  10. public $columns;
  11. public $styleMap;
  12. public $columnMap;
  13. public function __construct() // {{{2
  14. {
  15. parent::__construct();
  16. $this->columns = [];
  17. $this->styleMap = [];
  18. $this->columnMap = [];
  19. }
  20. public function getRequiredSeries() // {{{2
  21. {
  22. return [
  23. 'label' => false,
  24. 'color' => false,
  25. 'style' => false,
  26. 'x' => true,
  27. 'y0' => true
  28. ];
  29. }
  30. public function _getMinValue($type) // {{{2
  31. {
  32. switch ($type) {
  33. case 'dependant':
  34. $extremes = [];
  35. foreach ($this->columns as $line) {
  36. $extremes[] = min($line);
  37. }
  38. $min = min($extremes);
  39. break;
  40. case 'independant':
  41. $min = min(array_keys($this->columns));
  42. }
  43. if ($min > 0) {
  44. $min = 0;
  45. }
  46. return $min;
  47. }
  48. public function _getMaxValue($type) // {{{2
  49. {
  50. switch ($type) {
  51. case 'dependant':
  52. $extremes = [];
  53. foreach ($this->columns as $line) {
  54. $extremes[] = max($line);
  55. }
  56. return max($extremes);
  57. case 'independant':
  58. return max(array_keys($this->columns));
  59. }
  60. }
  61. public function _getLabels($type) // {{{2
  62. {
  63. switch ($type) {
  64. case 'dependant':
  65. return [];
  66. case 'independant':
  67. return array_keys($this->columns);
  68. }
  69. }
  70. public function _handleData($data) // {{{2
  71. {
  72. $columns = [];
  73. for ($i = 0; isset($data['y' . $i]); ++$i) {
  74. $columns[] = $data['y' . $i];
  75. }
  76. $count = count($columns);
  77. if (! isset($data['color'])) {
  78. $data['color'] = [];
  79. for ($i = 0; $count > $i; ++$i) {
  80. $data['color'][] = $this->_getColor();
  81. }
  82. }
  83. if (! isset($data['style'])) {
  84. for ($i = 0; $count > $i; ++$i) {
  85. $data['style'][] = 'FillStroke-' . $data['color'][$i];
  86. }
  87. }
  88. if (isset($data['label'])) {
  89. foreach ($data['label'] as $key => $label) {
  90. $this->addLegend(
  91. $data['color'][$key],
  92. $label,
  93. (isset($data['link']) && isset($data['link'][$key])) ? $data['link'][$key] : 0
  94. );
  95. }
  96. }
  97. foreach ($columns as $key => $line) {
  98. $style = $data['style'][$key];
  99. $this->styleMap[$style] = "y$key";
  100. foreach ($line as $key => $value) {
  101. $x = $data['x'][$key];
  102. $this->columnMap[$x] = $key;
  103. if (! isset($this->columns[$x])) {
  104. $this->columns[$x] = [];
  105. }
  106. if (! empty($value)) {
  107. $this->columns[$x][$style] = $value;
  108. } else {
  109. $this->columns[$x][$style] = 0;
  110. }
  111. }
  112. }
  113. return true;
  114. }
  115. public function _drawGridContent(&$renderer) // {{{2
  116. {
  117. $layout = $this->_layout();
  118. $zero = $this->dependant->getLocation(0);
  119. foreach ($this->columns as $label => $values) {
  120. $range = $this->independant->getRange($label);
  121. switch ($this->independant->orientation) {
  122. case 'vertical':
  123. $ren = new Fake_GRenderer($renderer, 0, $range[0], 1, $range[1]);
  124. break;
  125. case 'horizontal':
  126. $ren = new Fake_GRenderer($renderer, $range[0], 0, $range[1], 1);
  127. break;
  128. }
  129. $positions = $this->_drawColumn($ren, $values, $zero);
  130. if (is_array($positions)) {
  131. $index = $this->columnMap[$label];
  132. foreach ($positions as $style => $positionData) {
  133. $series = $this->styleMap[$style];
  134. $this->_notify($ren, $positionData, $series, $index);
  135. }
  136. }
  137. }
  138. }
  139. public function _drawColumn(&$renderer, $values, $zero)
  140. {
  141. die("Abstract Function Call");
  142. }
  143. public function _drawBox(&$renderer, $left, $top, $right, $bottom, $style)
  144. {
  145. $style = $renderer->getStyle($style);
  146. switch ($this->independant->orientation) {
  147. case 'vertical':
  148. $renderer->drawRectangle($bottom, $left, $top, $right, $style);
  149. break;
  150. case 'horizontal':
  151. $renderer->drawRectangle($left, $top, $right, $bottom, $style);
  152. break;
  153. }
  154. }
  155. public function _drawLegendBox(&$renderer, $color) // {{{2
  156. {
  157. $renderer->drawRectangle(0, 1, 1, 0, $renderer->getStyle("FillStroke-$color"));
  158. }
  159. public function _default() // {{{2
  160. {
  161. return array_merge(
  162. parent::_default(),
  163. [
  164. 'grid-independant-scale' => 'static',
  165. 'grid-independant-major-guide' => 'Thin-LineStroke-Black'
  166. ]
  167. );
  168. }
  169. }
  170. class BarStackGraphic extends BarBasedGraphic // {{{1
  171. {
  172. public function __construct() // {{{2
  173. {
  174. parent::__construct();
  175. }
  176. public function _getMinValue($type) // {{{2
  177. {
  178. switch ($type) {
  179. case 'dependant':
  180. $extremes = [];
  181. foreach ($this->columns as $line) {
  182. $extremes[] = array_sum($line);
  183. }
  184. $min = min($extremes);
  185. break;
  186. case 'independant':
  187. $min = min(array_keys($this->columns));
  188. break;
  189. }
  190. if ($min > 0) {
  191. $min = 0;
  192. }
  193. return $min;
  194. }
  195. public function _getMaxValue($type) // {{{2
  196. {
  197. switch ($type) {
  198. case 'dependant':
  199. $extremes = [];
  200. foreach ($this->columns as $line) {
  201. $extremes[] = array_sum($line);
  202. }
  203. return max($extremes);
  204. case 'independant':
  205. return max(array_keys($this->columns));
  206. }
  207. }
  208. public function _drawColumn(&$renderer, $values, $zero) // {{{2
  209. {
  210. $layout = $this->_layout();
  211. $begin = ( 1 - $layout['stack-column-width'] ) / 2;
  212. $end = $begin + $layout['stack-column-width'];
  213. $positive = 0;
  214. $negative = 0;
  215. foreach ($values as $style => $value) {
  216. if ($value == 0) {
  217. continue;
  218. }
  219. if ($value > 0) {
  220. $bottom = $positive;
  221. $positive += $value;
  222. $top = $positive;
  223. } else {
  224. $top = $negative;
  225. $negative += $value;
  226. $bottom = $negative;
  227. }
  228. $this->_drawBox(
  229. $renderer,
  230. $begin,
  231. $this->dependant->getLocation($top),
  232. $end,
  233. $this->dependant->getLocation($bottom),
  234. $style
  235. );
  236. }
  237. }
  238. public function _default() // {{{2
  239. {
  240. return array_merge(
  241. parent::_default(),
  242. ['stack-column-width' => 0.6]
  243. );
  244. }
  245. }
  246. class MultibarGraphic extends BarBasedGraphic // {{{1
  247. {
  248. public function __construct()
  249. {
  250. parent::__construct();
  251. }
  252. public function _drawColumn(&$renderer, $values, $zero)
  253. {
  254. $layout = $this->_layout();
  255. $count = count($values);
  256. $width = $layout['multi-columns-width'] / $count;
  257. $pad = ( 1 - $layout['multi-columns-width'] ) / 2;
  258. $positions = [];
  259. $i = 0;
  260. foreach ($values as $style => $value) {
  261. $base = $pad + $width * $i++;
  262. if ($value == 0) {
  263. continue;
  264. }
  265. $bottom = $this->dependant->getLocation($value);
  266. $this->_drawBox($renderer, $base, $zero, $base + $width, $bottom, $style);
  267. $positions[$style] = [
  268. 'left' => $base,
  269. 'top' => $zero,
  270. 'right' => $base + $width,
  271. 'bottom' => $bottom
  272. ];
  273. }
  274. return $positions;
  275. }
  276. public function _default()
  277. {
  278. return array_merge(
  279. parent::_default(),
  280. ['multi-columns-width' => 0.8]
  281. );
  282. }
  283. }