Graphs
|
|
|
|
<?php
/**
* This class converts data (sorted) into image/graph
*
* @author Rochak Chauhan
* @uses PHP 5.x
*/
class DataToGraph {
private $x_axis_text = '';
private $y_axis_text= '';
private $x_dataArray = array();
private $y_dataArray = array();
private $width = 800;
private $height = 600;
private $black = '';
private $red = '';
private $image = '';
/**
* Contructor function
*
* @param string $x_axis
* @param string $y_axis
* @param array $x_dataArray
* @param array $y_dataArray
*
* @return string
*/
public function DataToGraph($x_axis, $y_axis, $x_dataArray, $y_dataArray) {
if (trim($x_axis) == '') {
throw new Exception("Please enter a name for x-axis");
}
if (trim($y_axis) == '') {
throw new Exception("Please enter a name for y-axis");
}
if (trim($x_axis) == '') {
throw new Exception("Please enter a name for x-axis");
}
if (count($x_dataArray) > count($y_dataArray)) {
throw new Exception("The elements of X-axis exceeds the elements of Y-axis");
}
if (count($x_dataArray) < count($y_dataArray)) {
throw new Exception("The elements of Y-axis exceeds the elements of X-axis");
}
foreach ($x_dataArray as $xdata) {
if (!is_numeric($xdata)) {
throw new Exception("All the elements of X-axis array should be numeric");
}
}
foreach ($y_dataArray as $ydata) {
if (!is_numeric($ydata)) {
throw new Exception("All the elements of X-axis array should be numeric");
}
}
$this->x_axis_text = $x_axis;
$this->y_axis_text = $y_axis;
$this->x_dataArray = $x_dataArray;
$this->y_dataArray = $y_dataArray;
}
/**
* This function displays the generated image
*
* @param integer $imageWidth
*
* @return string
*/
public function displayImage($imageWidth, $imageHeight){
if (trim($imageWidth) == '') {
throw new Exception("Image width cannot be blank");
}
if (!is_numeric($imageWidth)) {
throw new Exception("Image width can only be anumeric value");
}
// initialise graph constants
$this->margin = $this->width*(0.04);
$this->width = $imageWidth;
$this->height = $imageHeight;
$this->drawCanvas();
$im = $this->image;
if (!is_resource($im)) {
throw new Exception("<br />No image canvas drawn. Please call drawCanvas function before assigning image resourse.");
}
// create short lines on X and Y Axis
$this->createScaleOnY_axis();
$this->createScaleOnX_axis();
// get all X and Y coordinates
$xlineLength = ($this->width-(2*$this->margin));
$ylineLength = ($this->height-(2*$this->margin));
$xScalingFactor = $xlineLength / $this->getMaximumLimit($this->x_dataArray) ;
$yScalingFactor = $ylineLength / $this->getMaximumLimit($this->y_dataArray) ;
for ($i=0; $i< count($this->x_dataArray); $i++){
$coordinatesArray[$i] = array (
'x' => ($this->x_dataArray[$i]*$xScalingFactor),
'y' => ($this->y_dataArray[$i]*$yScalingFactor)
);
}
// plot points on X and Y Axis
$this->plotPoints($coordinatesArray);
imagejpeg($im, "graph.jpg");
return '<br /><img src="graph.jpg" />';
}
/**
* This function
*
*
* @return void
*
*/
private function plotPoints($coordinatesArray) {
foreach ($coordinatesArray as $coordinates) {
imagefilledrectangle ($this->image, $coordinates['x']-5, $this->height-$coordinates['y']-5, $coordinates['x'], $this->height-($this->margin), $this->red );
}
}
/**
* This function creats scale on X-axis
*
* @return void
*/
private function createScaleOnY_axis() {
$lineLength = ($this->height-(2*$this->margin));
$scale = ( $lineLength/10 );
for ($i = 1; $i <= 10 ; $i++) {
imageline($this->image, ($this->margin-5), (($this->height-($this->margin)) - $i*$scale), ($this->margin+5), (($this->height-($this->margin)) - $i*$scale), $this->black );
}
}
/**
* This function creats scale on Y-axis
*
* @return void
*/
private function createScaleOnX_axis() {
$lineLength = ($this->width-(2*$this->margin));
$scale = ( $lineLength/10 );
for ($i = 1; $i <= 10 ; $i++) {
imageline($this->image, ($this->margin + $i*$scale), (($this->height-($this->margin)) - 5), ($this->margin + $i*$scale), (($this->height-($this->margin)) + 5), $this->black );
}
}
/**
* This function draws the imaage canvas along with X and Y coordinated
*
* @return void
*/
private function drawCanvas() {
if ($this->width == '' || !is_numeric($this->width)) {
throw new Exception("<br />Invalid value for width: $this->width. <br />Line: ". __LINE__." <br />Function: ".__FUNCTION__." <br />File: ".__FILE__);
}
if ($this->height == '' || !is_numeric($this->height)) {
throw new Exception("<br />Invalid value for height: $this->height. <br />Line: ". __LINE__." <br />Function: ".__FUNCTION__." <br />File: ".__FILE__);
}
$fontSize = $this->width/10;
$this->image = imagecreate($this->width, $this->height);
$background_color = imagecolorallocate($this->image, 255, 200, 100);
$this->black = imagecolorallocate($this->image, 0, 0, 0);
$this->red = imagecolorallocate($this->image, 255, 0, 0);
$text_color = imagecolorallocate($this->image, 233, 14, 91);
// write x and y string
imagestring($this->image, $fontSize, $this->margin-(.5*$this->margin), 5, $this->x_axis_text, $text_color);
imagestringup($this->image, $fontSize, ($this->width-$this->margin*.5), ($this->height-$this->margin+20), $this->y_axis_text, $text_color);
// write values on Y
imagestring($this->image, 3, ($this->margin-(.5*$this->margin)), ($this->height-($this->margin)), '0', $text_color);
$xlineLength = ($this->width-(2*$this->margin));
$ylineLength = ($this->height-(2*$this->margin));
$xscale = ( $xlineLength/10 );
$yscale = ( $ylineLength/10 );
$xLimit = $this->getMaximumLimit($this->x_dataArray)/10;
$yLimit = $this->getMaximumLimit($this->y_dataArray)/10;
for ($i=1; $i<=10; $i++) {
imagestring($this->image, 3, $this->margin+($i*$xscale)-(.4*$this->margin), $this->height-($this->margin*.5), ($xLimit*$i), $this->red);
imagestring($this->image, 3, $this->margin-($this->margin), $this->height-($i*$yscale + $this->margin)-($this->margin*.2), ($yLimit*$i), $this->red);
}
imageline($this->image, $this->margin, ($this->height-$this->margin), $this->margin, $this->margin, $this->black);
imageline($this->image, $this->margin, ($this->height-$this->margin), ($this->width-$this->margin), ($this->height-$this->margin), $this->black);
}
/**
* This function returns the maximum value in the array
*
* @param array $inputArray
*
* @return int
*/
private function getMaximumLimit($inputArray) {
asort($inputArray);
$max = end($inputArray);
if ( ($max > 9) && ( ($max%100) != 0) ) {
return ( $max + (100 - ($max%100)) );
}
elseif ( ($max < 10) && ( ($max%10) != 0) ) {
return ( $max + (10 - ($max%10)) );
}
else {
return $max;
}
}
} ?>
|
|
|
Usage Example
|
<?php
/**
* Example script
*
*/
// initialize data
$x = 'Sale';
$y = "Years";
$x_dataArray = array(
"1091",
"2311",
"3491",
"9091",
"2091"
);
$y_dataArray = array(
"1960",
"1970",
"1980",
"1990",
"2000"
);
// actual code using CodeToGraph Class (PHP 5 ONLY)
try {
require_once("DataToGraph.inc.php");
$dataToGraph = new DataToGraph($x, $y, $x_dataArray, $y_dataArray);
$imageCode = $dataToGraph->displayImage(800, 600);
echo $imageCode;
}
catch (Exception $e) {
echo $e->getMessage();
}
?>
|
|
|
Rate This Script
|
|
|
|