PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/tiendasikah/core/vendors/libchart/classes/view/plot/Plot.php

http://tiendasikah.googlecode.com/
PHP | 415 lines | 168 code | 52 blank | 195 comment | 6 complexity | e10382cab277d26546c6538d977635df MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause
  1. <?php
  2. /* Libchart - PHP chart library
  3. * Copyright (C) 2005-2008 Jean-Marc Trémeaux (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. * The plot holds graphical attributes, and is responsible for computing the layout of the graph.
  21. * The layout is quite simple right now, with 4 areas laid out like that:
  22. * (of course this is subject to change in the future).
  23. *
  24. * output area------------------------------------------------|
  25. * | (outer padding) |
  26. * | image area--------------------------------------------| |
  27. * | | (title padding) | |
  28. * | | title area----------------------------------------| | |
  29. * | | |-------------------------------------------------| | |
  30. * | | | |
  31. * | | (graph padding) (caption padding) | |
  32. * | | graph area----------------| caption area---------| | |
  33. * | | | | | | | |
  34. * | | | | | | | |
  35. * | | | | | | | |
  36. * | | | | | | | |
  37. * | | | | | | | |
  38. * | | |-------------------------| |--------------------| | |
  39. * | | | |
  40. * | |-----------------------------------------------------| |
  41. * | |
  42. * |----------------------------------------------------------|
  43. *
  44. * All area dimensions are known in advance , and the optional logo is drawn in absolute coordinates.
  45. *
  46. * @author Jean-Marc Trémeaux (jm.tremeaux at gmail.com)
  47. * Created on 27 july 2007
  48. */
  49. class Plot {
  50. // Style properties
  51. protected $title;
  52. protected $logoFileName;
  53. // Outer area, whose dimension is the same as the PNG returned
  54. protected $outputArea;
  55. // Outer padding surrounding the whole image, everything outside is blank
  56. protected $outerPadding;
  57. // Coordinates of the area inside the outer padding
  58. protected $imageArea;
  59. // Fixed title height in pixels
  60. protected $titleHeight;
  61. // Padding of the title area
  62. protected $titlePadding;
  63. // Coordinates of the title area
  64. protected $titleArea;
  65. // True if the plot has a caption
  66. protected $hasCaption;
  67. // Ratio of graph/caption in width
  68. protected $graphCaptionRatio;
  69. // Padding of the graph area
  70. protected $graphPadding;
  71. // Coordinates of the graph area
  72. protected $graphArea;
  73. // Padding of the caption area
  74. protected $captionPadding;
  75. // Coordinates of the caption area
  76. protected $captionArea;
  77. /**
  78. * Text writer.
  79. */
  80. protected $text;
  81. /**
  82. * Color palette.
  83. */
  84. protected $palette;
  85. /**
  86. * GD image
  87. */
  88. protected $img;
  89. /**
  90. * Drawing primitives
  91. */
  92. protected $primitive;
  93. protected $backGroundColor;
  94. protected $textColor;
  95. /**
  96. * Constructor of Plot.
  97. *
  98. * @param integer width of the image
  99. * @param integer height of the image
  100. */
  101. public function Plot($width, $height) {
  102. $this->width = $width;
  103. $this->height = $height;
  104. $this->text = new Text();
  105. $this->palette = new Palette();
  106. // Default layout
  107. $this->outputArea = new Rectangle(0, 0, $width - 1, $height - 1);
  108. $this->outerPadding = new Padding(5);
  109. $this->titleHeight = 26;
  110. $this->titlePadding = new Padding(5);
  111. $this->hasCaption = false;
  112. $this->graphCaptionRatio = 0.50;
  113. $this->graphPadding = new Padding(50);
  114. $this->captionPadding = new Padding(15);
  115. }
  116. /**
  117. * Compute the area inside the outer padding (outside is white).
  118. */
  119. private function computeImageArea() {
  120. $this->imageArea = $this->outputArea->getPaddedRectangle($this->outerPadding);
  121. }
  122. /**
  123. * Compute the title area.
  124. */
  125. private function computeTitleArea() {
  126. $titleUnpaddedBottom = $this->imageArea->y1 + $this->titleHeight + $this->titlePadding->top + $this->titlePadding->bottom;
  127. $titleArea = new Rectangle(
  128. $this->imageArea->x1,
  129. $this->imageArea->y1,
  130. $this->imageArea->x2,
  131. $titleUnpaddedBottom - 1
  132. );
  133. $this->titleArea = $titleArea->getPaddedRectangle($this->titlePadding);
  134. }
  135. /**
  136. * Compute the graph area.
  137. */
  138. private function computeGraphArea() {
  139. $titleUnpaddedBottom = $this->imageArea->y1 + $this->titleHeight + $this->titlePadding->top + $this->titlePadding->bottom;
  140. $graphArea = null;
  141. if ($this->hasCaption) {
  142. $graphUnpaddedRight = $this->imageArea->x1 + ($this->imageArea->x2 - $this->imageArea->x1) * $this->graphCaptionRatio
  143. + $this->graphPadding->left + $this->graphPadding->right;
  144. $graphArea = new Rectangle(
  145. $this->imageArea->x1,
  146. $titleUnpaddedBottom,
  147. $graphUnpaddedRight - 1,
  148. $this->imageArea->y2
  149. );
  150. } else {
  151. $graphArea = new Rectangle(
  152. $this->imageArea->x1,
  153. $titleUnpaddedBottom,
  154. $this->imageArea->x2,
  155. $this->imageArea->y2
  156. );
  157. }
  158. $this->graphArea = $graphArea->getPaddedRectangle($this->graphPadding);
  159. }
  160. /**
  161. * Compute the caption area.
  162. */
  163. private function computeCaptionArea() {
  164. $graphUnpaddedRight = $this->imageArea->x1 + ($this->imageArea->x2 - $this->imageArea->x1) * $this->graphCaptionRatio
  165. + $this->graphPadding->left + $this->graphPadding->right;
  166. $titleUnpaddedBottom = $this->imageArea->y1 + $this->titleHeight + $this->titlePadding->top + $this->titlePadding->bottom;
  167. $captionArea = new Rectangle(
  168. $graphUnpaddedRight,
  169. $titleUnpaddedBottom,
  170. $this->imageArea->x2,
  171. $this->imageArea->y2
  172. );
  173. $this->captionArea = $captionArea->getPaddedRectangle($this->captionPadding);
  174. }
  175. /**
  176. * Compute the layout of all areas of the graph.
  177. */
  178. public function computeLayout() {
  179. $this->computeImageArea();
  180. $this->computeTitleArea();
  181. $this->computeGraphArea();
  182. if ($this->hasCaption) {
  183. $this->computeCaptionArea();
  184. }
  185. }
  186. /**
  187. * Creates and initialize the image.
  188. */
  189. public function createImage() {
  190. $this->img = imagecreatetruecolor($this->width, $this->height);
  191. $this->primitive = new Primitive($this->img);
  192. $this->backGroundColor = new Color(255, 255, 255);
  193. $this->textColor = new Color(0, 0, 0);
  194. // White background
  195. imagefilledrectangle($this->img, 0, 0, $this->width - 1, $this->height - 1, $this->backGroundColor->getColor($this->img));
  196. //imagerectangle($this->img, $this->imageArea->x1, $this->imageArea->y1, $this->imageArea->x2, $this->imageArea->y2, $this->palette->red->getColor($this->img));
  197. }
  198. /**
  199. * Print the title to the image.
  200. */
  201. public function printTitle() {
  202. $yCenter = $this->titleArea->y1 + ($this->titleArea->y2 - $this->titleArea->y1) / 2;
  203. $this->text->printCentered($this->img, $yCenter, $this->textColor, $this->title, $this->text->fontCondensedBold);
  204. }
  205. /**
  206. * Print the logo image to the image.
  207. */
  208. public function printLogo() {
  209. @$logoImage = imageCreateFromPNG($this->logoFileName);
  210. if ($logoImage) {
  211. imagecopymerge($this->img, $logoImage, 2 * $this->outerPadding->left, $this->outerPadding->top, 0, 0, imagesx($logoImage), imagesy($logoImage), 100);
  212. }
  213. }
  214. /**
  215. * Renders to a file or to standard output.
  216. *
  217. * @param fileName File name (optional)
  218. */
  219. public function render($fileName) {
  220. if (isset($fileName)) {
  221. imagepng($this->img, $fileName);
  222. } else {
  223. imagepng($this->img);
  224. }
  225. }
  226. /**
  227. * Sets the title.
  228. *
  229. * @param string New title
  230. */
  231. public function setTitle($title) {
  232. $this->title = $title;
  233. }
  234. /**
  235. * Sets the logo image file name.
  236. *
  237. * @param string New logo image file name
  238. */
  239. public function setLogoFileName($logoFileName) {
  240. $this->logoFileName = $logoFileName;
  241. }
  242. /**
  243. * Return the GD image.
  244. *
  245. * @return GD Image
  246. */
  247. public function getImg() {
  248. return $this->img;
  249. }
  250. /**
  251. * Return the palette.
  252. *
  253. * @return palette
  254. */
  255. public function getPalette() {
  256. return $this->palette;
  257. }
  258. /**
  259. * Return the text.
  260. *
  261. * @return text
  262. */
  263. public function getText() {
  264. return $this->text;
  265. }
  266. /**
  267. * Return the primitive.
  268. *
  269. * @return primitive
  270. */
  271. public function getPrimitive() {
  272. return $this->primitive;
  273. }
  274. /**
  275. * Return the outer padding.
  276. *
  277. * @param integer Outer padding value in pixels
  278. */
  279. public function getOuterPadding() {
  280. return $outerPadding;
  281. }
  282. /**
  283. * Set the outer padding.
  284. *
  285. * @param integer Outer padding value in pixels
  286. */
  287. public function setOuterPadding($outerPadding) {
  288. $this->outerPadding = $outerPadding;
  289. }
  290. /**
  291. * Return the title height.
  292. *
  293. * @param integer title height
  294. */
  295. public function setTitleHeight($titleHeight) {
  296. $this->titleHeight = $titleHeight;
  297. }
  298. /**
  299. * Return the title padding.
  300. *
  301. * @param integer title padding
  302. */
  303. public function setTitlePadding($titlePadding) {
  304. $this->titlePadding = $titlePadding;
  305. }
  306. /**
  307. * Return the graph padding.
  308. *
  309. * @param integer graph padding
  310. */
  311. public function setGraphPadding($graphPadding) {
  312. $this->graphPadding = $graphPadding;
  313. }
  314. /**
  315. * Set if the graph has a caption.
  316. *
  317. * @param boolean graph has a caption
  318. */
  319. public function setHasCaption($hasCaption) {
  320. $this->hasCaption = $hasCaption;
  321. }
  322. /**
  323. * Set the caption padding.
  324. *
  325. * @param integer caption padding
  326. */
  327. public function setCaptionPadding($captionPadding) {
  328. $this->captionPadding = $captionPadding;
  329. }
  330. /**
  331. * Set the graph/caption ratio.
  332. *
  333. * @param integer caption padding
  334. */
  335. public function setGraphCaptionRatio($graphCaptionRatio) {
  336. $this->graphCaptionRatio = $graphCaptionRatio;
  337. }
  338. /**
  339. * Return the graph area.
  340. *
  341. * @return graph area
  342. */
  343. public function getGraphArea() {
  344. return $this->graphArea;
  345. }
  346. /**
  347. * Return the caption area.
  348. *
  349. * @return caption area
  350. */
  351. public function getCaptionArea() {
  352. return $this->captionArea;
  353. }
  354. /**
  355. * Return the text color.
  356. *
  357. * @return text color
  358. */
  359. public function getTextColor() {
  360. return $this->textColor;
  361. }
  362. }
  363. ?>