PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/src/old/solution2012/libchart/libchart/classes/view/chart/PieChart.php

https://bitbucket.org/vees/symulacja-rynku-lotniczego
PHP | 266 lines | 133 code | 47 blank | 86 comment | 9 complexity | 8e0c24fc09830d2532d6233f6f235b97 MD5 | raw file
  1. <?php
  2. /* Libchart - PHP chart library
  3. * Copyright (C) 2005-2011 Jean-Marc Tr?eaux (jm.tremeaux at gmail.com)
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. /**
  20. * Pie chart.
  21. *
  22. * @author Jean-Marc Tr?eaux (jm.tremeaux at gmail.com)
  23. */
  24. class PieChart extends Chart {
  25. protected $pieCenterX;
  26. protected $pieCenterY;
  27. /**
  28. * Constructor of a pie chart.
  29. *
  30. * @param integer width of the image
  31. * @param integer height of the image
  32. */
  33. public function PieChart($width = 600, $height = 250) {
  34. parent::Chart($width, $height);
  35. $this->plot->setGraphPadding(new Padding(15, 10, 30, 30));
  36. }
  37. /**
  38. * Computes the layout.
  39. */
  40. protected function computeLayout() {
  41. $this->plot->setHasCaption(true);
  42. $this->plot->computeLayout();
  43. // Get the graph area
  44. $graphArea = $this->plot->getGraphArea();
  45. // Compute the coordinates of the pie
  46. $this->pieCenterX = $graphArea->x1 + ($graphArea->x2 - $graphArea->x1) / 2;
  47. $this->pieCenterY = $graphArea->y1 + ($graphArea->y2 - $graphArea->y1) / 2;
  48. $this->pieWidth = round(($graphArea->x2 - $graphArea->x1) * 4 / 5);
  49. $this->pieHeight = round(($graphArea->y2 - $graphArea->y1) * 3.7 / 5);
  50. $this->pieDepth = round($this->pieWidth * 0.05);
  51. }
  52. /**
  53. * Compare two sampling point values, order from biggest to lowest value.
  54. *
  55. * @param double first value
  56. * @param double second value
  57. * @return integer result of the comparison
  58. */
  59. protected function sortPie($v1, $v2) {
  60. return $v1[0] == $v2[0] ? 0 :
  61. $v1[0] > $v2[0] ? -1 :
  62. 1;
  63. }
  64. /**
  65. * Compute pie values in percentage and sort them.
  66. */
  67. protected function computePercent() {
  68. $this->total = 0;
  69. $this->percent = array();
  70. $pointList = $this->dataSet->getPointList();
  71. foreach ($pointList as $point) {
  72. $this->total += $point->getY();
  73. }
  74. foreach ($pointList as $point) {
  75. $percent = $this->total == 0 ? 0 : 100 * $point->getY() / $this->total;
  76. array_push($this->percent, array($percent, $point));
  77. }
  78. // Sort data points
  79. if ($this->config->getSortDataPoint()) {
  80. //usort($this->percent, array("PieChart", "sortPie"));
  81. }
  82. }
  83. /**
  84. * Creates the pie chart image.
  85. */
  86. protected function createImage() {
  87. parent::createImage();
  88. // Get graphical obects
  89. $img = $this->plot->getImg();
  90. $palette = $this->plot->getPalette();
  91. $primitive = $this->plot->getPrimitive();
  92. // Get the graph area
  93. $graphArea = $this->plot->getGraphArea();
  94. // Legend box
  95. $primitive->outlinedBox($graphArea->x1, $graphArea->y1, $graphArea->x2, $graphArea->y2, $palette->axisColor[0], $palette->axisColor[1]);
  96. // Aqua-like background
  97. for ($i = $graphArea->y1 + 2; $i < $graphArea->y2 - 1; $i++) {
  98. $color = $palette->backgroundColor[($i + 3) % 4];
  99. $primitive->line($graphArea->x1 + 2, $i, $graphArea->x2 - 2, $i, $color);
  100. }
  101. }
  102. /**
  103. * Renders the caption.
  104. */
  105. protected function printCaption() {
  106. // Create a list of labels
  107. $labelList = array();
  108. foreach($this->percent as $percent) {
  109. list($percent, $point) = $percent;
  110. $label = $point->getX();
  111. array_push($labelList, $label);
  112. }
  113. // Create the caption
  114. $caption = new Caption();
  115. $caption->setPlot($this->plot);
  116. $caption->setLabelList($labelList);
  117. $palette = $this->plot->getPalette();
  118. $pieColorSet = $palette->pieColorSet;
  119. $caption->setColorSet($pieColorSet);
  120. // Render the caption
  121. $caption->render();
  122. }
  123. /**
  124. * Draw a 2D disc.
  125. *
  126. * @param integer Center coordinate (y)
  127. * @param array Colors for each portion
  128. * @param bitfield Drawing mode
  129. */
  130. protected function drawDisc($cy, $colorArray, $mode) {
  131. // Get graphical obects
  132. $img = $this->plot->getImg();
  133. $i = 0;
  134. $oldAngle = 0;
  135. $percentTotal = 0;
  136. foreach ($this->percent as $a) {
  137. list ($percent, $point) = $a;
  138. // If value is null, don't draw this arc
  139. if ($percent <= 0) {
  140. continue;
  141. }
  142. $color = $colorArray[$i % count($colorArray)];
  143. $percentTotal += $percent;
  144. $newAngle = $percentTotal * 360 / 100;
  145. // imagefilledarc doesn't like null values (#1)
  146. if ($newAngle - $oldAngle >= 1) {
  147. imagefilledarc($img, $this->pieCenterX, $cy, $this->pieWidth, $this->pieHeight, $oldAngle, $newAngle, $color->getColor($img), $mode);
  148. }
  149. $oldAngle = $newAngle;
  150. $i++;
  151. }
  152. }
  153. /**
  154. * Print the percentage text.
  155. */
  156. protected function drawPercent() {
  157. // Get graphical obects
  158. $img = $this->plot->getImg();
  159. $palette = $this->plot->getPalette();
  160. $text = $this->plot->getText();
  161. $primitive = $this->plot->getPrimitive();
  162. $angle1 = 0;
  163. $percentTotal = 0;
  164. foreach ($this->percent as $a) {
  165. list ($percent, $point) = $a;
  166. // If value is null, the arc isn't drawn, no need to display percent
  167. if ($percent <= 0) {
  168. continue;
  169. }
  170. $percentTotal += $percent;
  171. $angle2 = $percentTotal * 2 * M_PI / 100;
  172. $angle = $angle1 + ($angle2 - $angle1) / 2;
  173. $label = number_format($percent) . "%";
  174. $x = cos($angle) * ($this->pieWidth + 35) / 2 + $this->pieCenterX;
  175. $y = sin($angle) * ($this->pieHeight + 35) / 2 + $this->pieCenterY;
  176. $text->printText($img, $x, $y, $this->plot->getTextColor(), $label, $text->fontCondensed, $text->HORIZONTAL_CENTER_ALIGN | $text->VERTICAL_CENTER_ALIGN);
  177. $angle1 = $angle2;
  178. }
  179. }
  180. /**
  181. * Print the pie chart.
  182. */
  183. protected function printPie() {
  184. // Get graphical obects
  185. $img = $this->plot->getImg();
  186. $palette = $this->plot->getPalette();
  187. $text = $this->plot->getText();
  188. $primitive = $this->plot->getPrimitive();
  189. // Get the pie color set
  190. $pieColorSet = $palette->pieColorSet;
  191. $pieColorSet->reset();
  192. // Silhouette
  193. for ($cy = $this->pieCenterY + $this->pieDepth / 2; $cy >= $this->pieCenterY - $this->pieDepth / 2; $cy--) {
  194. $this->drawDisc($cy, $palette->pieColorSet->shadowColorList, IMG_ARC_EDGED);
  195. }
  196. // Top
  197. $this->drawDisc($this->pieCenterY - $this->pieDepth / 2, $palette->pieColorSet->colorList, IMG_ARC_PIE);
  198. // Top Outline
  199. if ($this->config->getShowPointCaption()) {
  200. $this->drawPercent();
  201. }
  202. }
  203. /**
  204. * Render the chart image.
  205. *
  206. * @param string name of the file to render the image to (optional)
  207. */
  208. public function render($fileName = null) {
  209. $this->computePercent();
  210. $this->computeLayout();
  211. $this->createImage();
  212. $this->plot->printLogo();
  213. $this->plot->printTitle();
  214. $this->printPie();
  215. $this->printCaption();
  216. $this->plot->render($fileName);
  217. }
  218. }
  219. ?>