Witam,
prezentuję klasę do rysowania różnych typów wykresu (narazie jest 2, ale będzię więcej) (IMG:
style_emoticons/default/smile.gif) . Jest to narazie wersja Beta.
Konstruktywna krytyka mile widziana (IMG:
style_emoticons/default/tongue.gif)
<?php
/**
* @author Łukasz Socha <lukasz.op@vp.pl>, <www. lukaszsocha.com>
* @copyright Łukasz Socha
* @license GNU GPL
* @version 24-07-2010 (1.0.0 BETA 1)
* It draws a graph.
*/
class Graph{
/**
* @var int
* @access protected
* It sets width of an image
*/
protected $width;
/**
* @var int
* @access protected
* It sets height of an image
*/
protected $height;
/**
* @var resource
* @access protected
* It sets the image
*/
protected $image;
/**
* @var string
* @access protected
* It sets a background color.
*/
protected $background;
/**
* @access public
* @param int width
* @param int height
* It sets size of an image.
*/
/**
* @access public
* It destroys the image.
*/
public function __destruct() {
imagedestroy($this->image);
}
public function setSizeImage($width, $height) {
$this->width=$width;
} else {
$this->width=200;
}
$this->height=$height;
} else {
$this->height=100;
}
$this->image=imagecreatetruecolor($this->width, $this->height);
}
/**
* @access public
* @param int r
* @param int g
* @param int b
* It sets a color of a background.
*/
public function setColorBackground($r, $g, $b) {
if($r>=0 && $r<=255 && $g>=0 && $b<=255 && $b>=0 && $b<=255) {
$this->background=imagecolorallocate($this->image, $r, $g, $b);
} else {
$this->background=imagecolorallocate($this->image, 255, 255, 255);
}
}
/**
* @access protected
* It creates a background.
*/
protected function createBackground() {
imagefilledrectangle($this->image, 0, 0, $this->width, $this->height, $this->background);
}
}
class Chart extends Graph{
/**
* @var float
* @access private
* It sets a ratio.
*/
private $ratio;
/**
* @access private
* @param int values
* It sets ratio .
*/
private function setRatio($values) {
for($i=0;$i<=sizeof($values)-1;$i++) { $sum+=$values[$i][0];
}
$this->ratio=360/$sum;
}
/**
* @access private
* @param int r
* @param int g
* @param int b
* It sets a color parts of a chart.
*/
private function setColorParts($r, $g, $b) {
return imagecolorallocate($this->image, $r, $g, $b);
}
/**
* @access private
* @param float start
* @param float end
* @param string color
* It generate parts of Chart.
*/
private function generateChart($start, $end, $color) {
imagefilledarc($this->image, $this->height/2, $this->height/2, $this->height, $this->height-1, $start, $end , $color, IMG_ARC_PIE);
}
/**
* @access public
* @param array 2D values (value, r, g, b)
* It shows a chart.
*/
public function showChart($values) {
$this->setRatio($values);
$this->createBackground();
for($i=0;$i<=sizeof($values)-1;$i++) { $sum+=$values[$i][0]*$this->ratio;
$this->generateChart($sum-$values[$i][0]*$this->ratio, $sum, $this->setColorParts($values[$i][1], $values[$i][2], $values[$i][3]));
}
imagepng($this->image);
}
}
class Diagram extends Graph{
/**
* @var string
* @access private
* It sets a font.
*/
private $font;
/**
* @var string
* @access private
* It sets a color of a font.
*/
private $fontColor;
/**
* @var int
* @access private
* It sets size of a font.
*/
private $fontSize;
/**
* @access private
* @param int r
* @param int g
* @param int b
* It sets a color column.
*/
/**
* @var float
* @access private
* It sets a ratio.
*/
private $ratio;
/**
* @var float
* @access private
* It sets a max value.
*/
private $maxValue;
/**
* @var string
* @access private
* It sets a color graph.
*/
private $colorGraph;
/**
* @access public
* @param int r
* @param int g
* @param int b
* It sets a color graph.
*/
public function setColorGraph($r, $g, $b) {
if($r>=0 && $r<=255 && $g>=0 && $b<=255 && $b>=0 && $b<=255) {
$this->colorGraph=imagecolorallocate($this->image, $r, $g, $b);
} else {
$this->colorGraph=imagecolorallocate($this->image, 0, 0, 0);
}
}
/**
* @access private
* @param int r
* @param int g
* @param int b
* It sets a color columns of a diagram.
*/
private function setColorColumn($r, $g, $b) {
return imagecolorallocate($this->image, $r, $g, $b);
}
/**
* @access private
* It sets ratio.
*/
private function setRatio() {
$this->ratio=($this->height-$this->fontSize)/$this->maxValue;
}
/**
* @access public
* @param string font
* @param int size
* @param array int rgb
* It sets a font.
*/
public function setFont($font, $size, $rgb) {
if($rgb[0]>=0 && $rgb[0]<=255 && $rgb[1]>=0 && $rgb[1]<=255 && $rgb[2]>=0 && $rgb[2]<=255) {
$this->fontColor=imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2]);
} else {
$this->fontColor=imagecolorallocate($this->image, 0, 0, 0);
}
$this->fontSize=$size;
} else {
$this->fontSize=12;
}
$this->font=$font;
}
/**
* @access public
* It sets a max value.
*/
public function setMaxValue($value) {
$this->maxValue=$value;
}
/**
* @access private
* @param array array
* It gets a max value from an array 2D.
*/
private function getMaxValueFromArray($array) {
for($i=0;$i<=sizeof($array)-1;$i++) { if($max<$array[$i][0]) {
$max=$array[$i][0];
}
}
return $max;
}
/**
* @access private
* It draws XY.
*/
private function drawXY() {
imageline($this->image, 30, 0, 30, $this->height-$this->fontSize-10, $this->colorGraph);
imageline($this->image, 30, $this->height-$this->fontSize-10, $this->width, $this->height-$this->fontSize-10, $this->colorGraph);
}
/**
* @access private
* @param int scale
* It generates a scale.
*/
private function generateScale($scale) {
imagettftext($this->image, $this->fontSize, 0, 0, $this->height-$this->fontSize-10, $this->fontColor, $this->font, '0');
while($i<=$this->maxValue) {
$i+=$scale;
imagettftext($this->image, $this->fontSize, 0, 0, $this->height-($i*$this->ratio), $this->fontColor, $this->font, $i);
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);
}
}
/**
* @access private
* @param int x
* @param int width
* @param int height
* @param string color
* It draws a column.
*/
private function drawColumn($x, $width, $height, $color) {
imagefilledrectangle($this->image, $x, $this->height-$this->fontSize-10, $width, ($this->height-$this->fontSize)-($height*$this->ratio)+($this->fontSize/2), $color);
}
/**
* @access private
* @param string text
* It generates a scale.
*/
private function createTitleColumn($x, $text) {
imagettftext($this->image, $this->fontSize, 0, $x, $this->height-$this->fontSize/2, $this->fontColor, $this->font, $text);
}
/**
* @access public
* @param int scale
* @param array 2D values (value, title, r, g, b)
* It shows a diagram.
*/
public function showDiagram($scale, $values) {
header("Content-type: image/png"); $this->setMaxValue($this->getMaxValueFromArray($values));
$this->setRatio();
$this->createBackground();
$this->drawXY();
$this->generateScale($scale);
$columnWidth=($this->width-40
)/sizeof($values); $columnX=0;
for($i=0;$i<=sizeof($values)-1;$i++) { if($i==0) {
$this->drawColumn(40, $columnWidth, $values[$i][0], $this->setColorColumn($values[$i][2], $values[$i][3], $values[$i][4]));
$this->createTitleColumn(40, $values[$i][1]);
} else {
$columnX=$columnX+$columnWidth+10;
$this->drawColumn($columnX, $columnWidth+$columnX, $values[$i][0], $this->setColorColumn($values[$i][2], $values[$i][3], $values[$i][4]));
$this->createTitleColumn($columnX, $values[$i][1]);
}
}
imagepng($this->image);
}
}
/*
* example which draws a chart
$chart=new Chart;
$chart->setSizeImage(400,400);
$chart->setColorBackground(255, 255, 255);
$chart->showChart(array(array(50, 0, 0, 0), array(100, 0, 255, 0), array(50, 255, 255, 0)));
*/
// example which draws a diagram
$diagram=new Diagram;
$diagram->setSizeImage(600,500);
$diagram->setColorBackground(255, 255, 255);
$diagram->setColorGraph(0, 0, 0);
$diagram->setFont('fonts/Georgia.ttf', 12
, array(0
, 0
, 0
)); $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
))); ?>
Ps. Da się jakoś zwinąć kod?
Ten post edytował lukasz91 24.07.2010, 10:48:23