Witam,
prezentuję klasę do rysowania różnych typów wykresu (narazie jest 2, ale będzię więcej) smile.gif. Jest to narazie wersja Beta.
Konstruktywna krytyka mile widziana tongue.gif
  1. <?php
  2. /**
  3.  * @author Łukasz Socha <lukasz.op@vp.pl>, <www. lukaszsocha.com>
  4.  * @copyright Łukasz Socha
  5.  * @license GNU GPL
  6.  * @version 24-07-2010 (1.0.0 BETA 1)
  7.  * It draws a graph.
  8.  */
  9. class Graph{
  10. /**
  11.  * @var int
  12.  * @access protected
  13.  * It sets width of an image
  14.  */
  15. protected $width;
  16. /**
  17.  * @var int
  18.  * @access protected
  19.  * It sets height of an image
  20.  */
  21. protected $height;
  22. /**
  23.  * @var resource
  24.  * @access protected
  25.  * It sets the image
  26.  */
  27. protected $image;
  28. /**
  29.  * @var string
  30.  * @access protected
  31.  * It sets a background color.
  32.  */
  33. protected $background;
  34. /**
  35.  * @access public
  36.  * @param int width
  37.  * @param int height
  38.  * It sets size of an image.
  39.  */
  40. /**
  41.  * @access public
  42.  * It destroys the image.
  43.  */
  44. public function __destruct() {
  45. imagedestroy($this->image);
  46. }
  47. public function setSizeImage($width, $height) {
  48. if(is_int($width)) {
  49. $this->width=$width;
  50. } else {
  51. $this->width=200;
  52. }
  53. if(is_int($height)) {
  54. $this->height=$height;
  55. } else {
  56. $this->height=100;
  57. }
  58. $this->image=imagecreatetruecolor($this->width, $this->height);
  59. }
  60. /**
  61.  * @access public
  62.  * @param int r
  63.  * @param int g
  64.  * @param int b
  65.  * It sets a color of a background.
  66.  */
  67. public function setColorBackground($r, $g, $b) {
  68. if($r>=0 && $r<=255 && $g>=0 && $b<=255 && $b>=0 && $b<=255) {
  69. $this->background=imagecolorallocate($this->image, $r, $g, $b);
  70. } else {
  71. $this->background=imagecolorallocate($this->image, 255, 255, 255);
  72. }
  73. }
  74. /**
  75.  * @access protected
  76.  * It creates a background.
  77.  */
  78. protected function createBackground() {
  79. imagefilledrectangle($this->image, 0, 0, $this->width, $this->height, $this->background);
  80. }
  81. }
  82. class Chart extends Graph{
  83. /**
  84.  * @var float
  85.  * @access private
  86.  * It sets a ratio.
  87.  */
  88. private $ratio;
  89. /**
  90.  * @access private
  91.  * @param int values
  92.  * It sets ratio .
  93.  */
  94. private function setRatio($values) {
  95. for($i=0;$i<=sizeof($values)-1;$i++) {
  96. $sum+=$values[$i][0];
  97. }
  98. $this->ratio=360/$sum;
  99. }
  100. /**
  101.  * @access private
  102.  * @param int r
  103.  * @param int g
  104.  * @param int b
  105.  * It sets a color parts of a chart.
  106.  */
  107. private function setColorParts($r, $g, $b) {
  108. return imagecolorallocate($this->image, $r, $g, $b);
  109. }
  110. /**
  111.  * @access private
  112.  * @param float start
  113.  * @param float end
  114.  * @param string color
  115.  * It generate parts of Chart.
  116.  */
  117. private function generateChart($start, $end, $color) {
  118. imagefilledarc($this->image, $this->height/2, $this->height/2, $this->height, $this->height-1, $start, $end , $color, IMG_ARC_PIE);
  119. }
  120. /**
  121.  * @access public
  122.  * @param array 2D values (value, r, g, b)
  123.  * It shows a chart.
  124.  */
  125. public function showChart($values) {
  126. $this->setRatio($values);
  127. $this->createBackground();
  128.  
  129. for($i=0;$i<=sizeof($values)-1;$i++) {
  130. $sum+=$values[$i][0]*$this->ratio;
  131. $this->generateChart($sum-$values[$i][0]*$this->ratio, $sum, $this->setColorParts($values[$i][1], $values[$i][2], $values[$i][3]));
  132. }
  133.  
  134. imagepng($this->image);
  135. }
  136. }
  137. class Diagram extends Graph{
  138. /**
  139.  * @var string
  140.  * @access private
  141.  * It sets a font.
  142.  */
  143. private $font;
  144. /**
  145.  * @var string
  146.  * @access private
  147.  * It sets a color of a font.
  148.  */
  149. private $fontColor;
  150. /**
  151.  * @var int
  152.  * @access private
  153.  * It sets size of a font.
  154.  */
  155. private $fontSize;
  156. /**
  157.  * @access private
  158.  * @param int r
  159.  * @param int g
  160.  * @param int b
  161.  * It sets a color column.
  162.  */
  163. /**
  164.  * @var float
  165.  * @access private
  166.  * It sets a ratio.
  167.  */
  168. private $ratio;
  169. /**
  170.  * @var float
  171.  * @access private
  172.  * It sets a max value.
  173.  */
  174. private $maxValue;
  175. /**
  176.  * @var string
  177.  * @access private
  178.  * It sets a color graph.
  179.  */
  180. private $colorGraph;
  181. /**
  182.  * @access public
  183.  * @param int r
  184.  * @param int g
  185.  * @param int b
  186.  * It sets a color graph.
  187.  */
  188. public function setColorGraph($r, $g, $b) {
  189. if($r>=0 && $r<=255 && $g>=0 && $b<=255 && $b>=0 && $b<=255) {
  190. $this->colorGraph=imagecolorallocate($this->image, $r, $g, $b);
  191. } else {
  192. $this->colorGraph=imagecolorallocate($this->image, 0, 0, 0);
  193. }
  194. }
  195. /**
  196.  * @access private
  197.  * @param int r
  198.  * @param int g
  199.  * @param int b
  200.  * It sets a color columns of a diagram.
  201.  */
  202. private function setColorColumn($r, $g, $b) {
  203. return imagecolorallocate($this->image, $r, $g, $b);
  204. }
  205. /**
  206.  * @access private
  207.  * It sets ratio.
  208.  */
  209. private function setRatio() {
  210. $this->ratio=($this->height-$this->fontSize)/$this->maxValue;
  211. }
  212. /**
  213.  * @access public
  214.  * @param string font
  215.  * @param int size
  216.  * @param array int rgb
  217.  * It sets a font.
  218.  */
  219. public function setFont($font, $size, $rgb) {
  220. if($rgb[0]>=0 && $rgb[0]<=255 && $rgb[1]>=0 && $rgb[1]<=255 && $rgb[2]>=0 && $rgb[2]<=255) {
  221. $this->fontColor=imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2]);
  222. } else {
  223. $this->fontColor=imagecolorallocate($this->image, 0, 0, 0);
  224. }
  225. if(is_int($size)) {
  226. $this->fontSize=$size;
  227. } else {
  228. $this->fontSize=12;
  229. }
  230. $this->font=$font;
  231. }
  232. /**
  233.  * @access public
  234.  * It sets a max value.
  235.  */
  236. public function setMaxValue($value) {
  237. $this->maxValue=$value;
  238. }
  239. /**
  240.  * @access private
  241.  * @param array array
  242.  * It gets a max value from an array 2D.
  243.  */
  244. private function getMaxValueFromArray($array) {
  245. for($i=0;$i<=sizeof($array)-1;$i++) {
  246. if($max<$array[$i][0]) {
  247. $max=$array[$i][0];
  248. }
  249. }
  250. return $max;
  251. }
  252. /**
  253.  * @access private
  254.  * It draws XY.
  255.  */
  256. private function drawXY() {
  257. imageline($this->image, 30, 0, 30, $this->height-$this->fontSize-10, $this->colorGraph);
  258. imageline($this->image, 30, $this->height-$this->fontSize-10, $this->width, $this->height-$this->fontSize-10, $this->colorGraph);
  259. }
  260. /**
  261.  * @access private
  262.  * @param int scale
  263.  * It generates a scale.
  264.  */
  265. private function generateScale($scale) {
  266. imagettftext($this->image, $this->fontSize, 0, 0, $this->height-$this->fontSize-10, $this->fontColor, $this->font, '0');
  267. while($i<=$this->maxValue) {
  268. $i+=$scale;
  269. imagettftext($this->image, $this->fontSize, 0, 0, $this->height-($i*$this->ratio), $this->fontColor, $this->font, $i);
  270. imageline($this->image, 25, $this->height-($i*$this->ratio)-($this->fontSize/2.5), 35, $this->height-($i*$this->ratio)-($this->fontSize/2.5), $this->colorGraph);
  271. }
  272. }
  273. /**
  274.  * @access private
  275.  * @param int x
  276.  * @param int width
  277.  * @param int height
  278.  * @param string color
  279.  * It draws a column.
  280.  */
  281. private function drawColumn($x, $width, $height, $color) {
  282. imagefilledrectangle($this->image, $x, $this->height-$this->fontSize-10, $width, ($this->height-$this->fontSize)-($height*$this->ratio)+($this->fontSize/2), $color);
  283. }
  284. /**
  285.  * @access private
  286.  * @param string text
  287.  * It generates a scale.
  288.  */
  289. private function createTitleColumn($x, $text) {
  290. imagettftext($this->image, $this->fontSize, 0, $x, $this->height-$this->fontSize/2, $this->fontColor, $this->font, $text);
  291.  
  292. }
  293. /**
  294.  * @access public
  295.  * @param int scale
  296.  * @param array 2D values (value, title, r, g, b)
  297.  * It shows a diagram.
  298.  */
  299. public function showDiagram($scale, $values) {
  300. header("Content-type: image/png");
  301. $this->setMaxValue($this->getMaxValueFromArray($values));
  302. $this->setRatio();
  303. $this->createBackground();
  304. $this->drawXY();
  305. $this->generateScale($scale);
  306. $columnWidth=($this->width-40)/sizeof($values);
  307. $columnX=0;
  308. for($i=0;$i<=sizeof($values)-1;$i++) {
  309. if($i==0) {
  310. $this->drawColumn(40, $columnWidth, $values[$i][0], $this->setColorColumn($values[$i][2], $values[$i][3], $values[$i][4]));
  311. $this->createTitleColumn(40, $values[$i][1]);
  312. } else {
  313. $columnX=$columnX+$columnWidth+10;
  314. $this->drawColumn($columnX, $columnWidth+$columnX, $values[$i][0], $this->setColorColumn($values[$i][2], $values[$i][3], $values[$i][4]));
  315. $this->createTitleColumn($columnX, $values[$i][1]);
  316. }
  317. }
  318. imagepng($this->image);
  319. }
  320. }
  321. /*
  322.  * example which draws a chart
  323. $chart=new Chart;
  324. $chart->setSizeImage(400,400);
  325. $chart->setColorBackground(255, 255, 255);
  326. $chart->showChart(array(array(50, 0, 0, 0), array(100, 0, 255, 0), array(50, 255, 255, 0)));
  327. */
  328. // example which draws a diagram
  329. $diagram=new Diagram;
  330. $diagram->setSizeImage(600,500);
  331. $diagram->setColorBackground(255, 255, 255);
  332. $diagram->setColorGraph(0, 0, 0);
  333. $diagram->setFont('fonts/Georgia.ttf', 12, array(0, 0, 0));
  334. $diagram->showDiagram(1, array(array(6, 'title 1', 0, 0, 0), array(3, 'Piłka nożna', 222, 0, 0), array(5, 'title3', 0, 0, 0), array(1, 'title4', 0, 0, 0), array(2, 'title5', 0, 0, 0)));
  335. ?>


Ps. Da się jakoś zwinąć kod?