I have a question regarding my construct, I don't feel it.
Code: Select all
class Graph
{
function __construct($width, $height, $data='')
{
// Data to display
$this->data = $data;
if(!empty($this->data))
{
// Amount of data to display on x axis.
$this->d_count = count($this->data);
}
// Image Specs
$this->w = empty($width) ? 600 : $width;
$this->h = empty($height) ? 300 : $height;
// Create Image Stream
$this->im = $this->createSource();
// Interval
$this->interval = 10;
// Starting position for Content
$this->x_pos = 40;
$this->y_pos = $this->h - 15;
// Unit Value
$this->y_unit = $this->h / $this->interval;
$this->x_unit = ($this->w - $this->x_pos) / $this->d_count;
}
private function setColor($color='')
{
switch($color)
{
case "white":
return imagecolorallocate($this->im, 255, 255, 255);
break;
case "red":
return imagecolorallocate($this->im, 255, 0, 0);
break;
case "grey":
return imagecolorallocate($this->im, 162, 162, 161);
case "blue":
return imagecolorallocate($this->im, 0, 0, 255);
break;
case "green":
return imagecolorallocate($this->im, 50, 255, 0);
break;
default:
case "black":
return imagecolorallocate($this->im, 0, 0, 0);
}
}
private function createSource()
{
if($im = @imagecreatetruecolor($this->w, $this->h))
{
return $im;
} else return false;
}
private function ImageHeader()
{
return header ("Content-type: image/png");
}
// Display Image
public function outputImage()
{
$this->ImageHeader();
$this->setBG("white");
$this->displayGrid();
$this->XValues();
$this->YValues();
$this->displayData("budget", "red");
$this->displayData("actual", "blue");
$this->displayData("PYA", "green");
imagepng($this->im);
imagedestroy($this->im);
}
// Image BG color
private function setBG($color)
{
return imagefill($this->im, 0, 0, $this->setColor($color));
}
private function displayGrid()
{
$x_pos = $this->x_pos; // Default X position
$y_pos = 0; // Default Y position
// Y
for($i=0; $i<$this->interval; $i++)
{
imageline($this->im, $this->x_pos, $y_pos, $this->w, $y_pos, $this->setColor("grey"));
$y_pos += $this->y_unit;
}
// X
for($j=0; $j<$this->d_count; $j++)
{
imageline($this->im, $x_pos, 0, $x_pos, $this->y_pos, $this->setColor("grey"));
$x_pos += $this->x_unit;
}
}
//etc