Forum Moderators: coopster

Message Too Old, No Replies

Graph title and bar disappears

         

royden

10:07 am on Jan 23, 2009 (gmt 0)

10+ Year Member



hi everyone ,
i am trying to develop a graph class which should able to display titles and bargraph using values passed from array... i make use of two axis x nd y but for y, value of 237 or above the title nd bar will disappear from the image that is displayed... also need a auto generation of values on y-axis or y-scale..like 5,10 depending upon the max value should be auto calculated.

<?
include "graph.class.php";
$dataArr['X'] = array("Jan","Feb","Mar","Apr","May");
$dataArr['Y'] = array("10","40","80","20","236");
$width = "800";
$height = "650";
$graphObj = new graph($width,$height);
$graphObj->setColor(255,200,200);
$barColor = $graphObj->setColor(0,0,128);
$graphObj->imageType = "JPEG";

$graphObj->graphMarginLeft = "50";
$graphObj->graphMarginRight = "50";
$graphObj->graphMarginTop = "50";
$graphObj->graphMarginBottom = "50";
$graphObj->fontSize = "2";

# setGraphTitles($mainTitle, $xTitle, $yTitle)
$graphObj->setGraphTitles("Graph Title", "X-AxisValues", "Y-AxisValues");
$graphObj->loadYXaxis($dataArr);
$graphObj->drawBar(20,$dataArr);
$graphObj->drawTitles(5);
$graphObj->drawGraph();

?>

does anyone have a soltn to this problem...?the dynamic scale on y axis to need to be multiples of 5 depending upon the max value...any specific method that need to be used?

whoisgregg

12:14 am on Jan 28, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not sure how to suggest code for your class without seeing the actual class code that explains what each function does, but I'll see if I can help with the one item... If you want there to only ever be 5 y-axis labels, which change based on the max value passed, this math should help:

$dataArr['Y'] = array("10","40","80","20","236"); 
$max_value = max($dataArr['Y']);
$y_max = ceil($max_value / 5) * 5;
$y_step = ceil($y_max / 4); // change this one if you want more/fewer y-axis labels
$y_labels = range(0,$y_max, $y_step);
print_r($y_labels); // for debugging

This code isn't perfect, but it's a starting point to calculate dynamic axis labels.

royden

1:18 pm on Jan 28, 2009 (gmt 0)

10+ Year Member



thank's i will try and inform u if it worked nd post u the class too...

royden

6:54 am on Feb 9, 2009 (gmt 0)

10+ Year Member



hi i could not get it working this is the class i hv developed for bar graph nd it does not match the y-scales...also the problem with disappearance of graph titles and bar's when the values is above 236...kindly anyone do the changes nd reply plz..
<?

class graph{

function graph($width,$height){

# Graph Dimensions
$this->height = $height;
$this->width = $width;

# Image Resource
$this->imgRes = $this->createImage();

# Image Type (PNG,JPEG,GIF)
$this->imageType = "";

# Graph type (Line, Pie chart, Bar graph)
$this->type = "";

# Graph Title
$this->title = "";

# Graph Interval
$this->interval = "";

# Graph Margin
$this->graphMarginLeft = "";
$this->graphMarginTop = "";
$this->graphMarginBottom = "";
$this->graphMarginRight = "";

# X axis
$this->yHeight = "";
$this->xWidth = "";

# Y axis
$this->y_axis = "";
$this->titleY = "";

$this->drawGrid = false;
$this->showLegends = true;

$this->barWidth = "";
$this->fontSize = "";

}

# Image Rectangle
function createImageRectangle($x1,$y1,$x2,$y2,$color){
imagerectangle($this->imgRes,$x1,$y1,$x2,$y2,$color);
}

# Image Line
function createImageLine($x1,$y1,$x2,$y2,$color){
imageline($this->imgRes,$x1,$y1,$x2,$y2,$color);
}

# Image DashedLine
function createImageDashedLine($x1,$y1,$x2,$y2,$color){
imagedashedline($this->imgRes,$x1,$y1,$x2,$y2,$color);
}

# Image Ellipse
function createImageEllipse($cx,$cy,$width,$height,$color){
imageellipse ($this->imgRes,$cx,$cy,$width,$height,$color);
}

# Image String
function createImageString($font,$x,$y,$string,$color){
imagestring ( $this->imgRes,$font,$x,$y,$string,$color);
}

# Image String Vertically
function createImageStringVert($font,$x,$y,$string,$color){
imagestringup ( $this->imgRes,$font,$x,$y,$string,$color);
}
# Image Filled Rectangles
function createImageFilledRectangle($x1,$y1,$x2,$y2,$color){
imagefilledrectangle($this->imgRes,$x1,$y1,$x2,$y2,$color);
}

# Creating new image
function createImage(){
$imgSource = ImageCreate($this->width, $this->height);
return $imgSource;
imageantialias($this->imgRes, true);
}

# This is used to the set the color of any image
function setColor($red, $green, $blue) {
$this->color = ImageColorAllocate($this->imgRes, $red, $green, $blue);
}

# Function to SetTitles
function setGraphTitles($mainTitle, $xTitle, $yTitle){
$this->graphTitle = $mainTitle;
$this->graphXTitle = $xTitle;
$this->graphYTitle = $yTitle;
}

# This is used for plotting points on graphs both x and y.
function loadYXaxis($dataArr){

# Y axis
$x1 = (0 + $this->graphMarginLeft);
$y1 = (0 + $this->graphMarginTop);
$x2 = (0 + $this->graphMarginLeft);
$y2 = ($this->height - $this->graphMarginBottom);
$this->createImageLine($x1,$y1,$x2,$y2,$this->color);

# X axis

$x1 = (0 + $this->graphMarginLeft);
$y1 = ($this->height - $this->graphMarginBottom);
$x2 = ($this->width - $this->graphMarginRight);
$y2 = ($this->height - $this->graphMarginBottom);
$this->createImageLine($x1,$y1,$x2,$y2,$this->color);

$this->yHeight = $this->height - ($this->graphMarginTop + $this->graphMarginBottom);
$this->xWidth = $this->width - ($this->graphMarginLeft + $this->graphMarginRight);

foreach($dataArr as $key => $values){
unset($innerVal);
switch($key){
case "X":
$this->interval = floor($this->xWidth / (count($values)+1));

$pointY = $y2;
$pointX = $this->graphMarginLeft;

foreach($values as $innerVal){
$pointX = $pointX + $this->interval;
$this->setColor(0,0,0);
//$this->createImageEllipse($pointX,$pointY,3,3,$this->color);
//$this->createImageString($this->fontSize,$pointX-5,$pointY+10,$innerVal,$this->color);
//$this->createImageLine($pointX,$this->graphMarginBottom,$pointX,$pointY,$this->color);
//$this->createImageDashedLine($pointX,$this->graphMarginBottom,$pointX,$pointY,$this->color);
}
break;

case "Y":

$this->interval = floor($this->yHeight/(count($values)+1));

$y_gap = round($this->yHeight/$this->interval);

$pointY = $y2;
$pointX = $x1;

$max_value = max($values)+10;

$yScale = array("10","20","30","40","50","60","70","80","90","100");

$myval=0;
for($i=0;$i<count($yScale);$i++){

$g =$y_gap*$yScale[$i];
if($max_value<=$g){
$myval = $g;
break;
}
}

$num1 = $myval/$y_gap;

for($i=0;$i<$max_value;$i++){
$num = $num + $num1;
$pointY = $pointY-$this->interval;
$this->setColor(0,0,0);
$this->createImageEllipse($pointX,$pointY,3,3,$this->color);
$this->createImageString($this->fontSize,$pointX-25,$pointY-5,$num,$this->color);
$this->createImageLine($pointX,$pointY,$x2,$pointY,$this->color);
//$this->createImageDashedLine($pointX,$pointY,$x2,$pointY,$this->color);
}
break;
}
}
}

# this is used to plot bargraph

function drawBar($bar_width,$dataArr){

$this->interval = floor($this->yHeight/(count($dataArr['Y'])+1));
$ratio =($this->yHeight/$this->interval);

$total_bars = count($dataArr['X']);
$gap = floor(($this->xWidth)/ ($total_bars));

$this->setColor(0,0,128);

for($i=0;$i<$total_bars;$i++){
$x1 = $this->graphMarginLeft +$i*$gap;
$x2 = $x1 + $bar_width;
$y1 = $this->yHeight-($dataArr['Y'][$i]);
$y2 = $this->height-$this->graphMarginBottom;

$this->createImageFilledRectangle($x1,$y1,$x2,$y2,$this->color);
$this->createImageString($this->fontSize,$x1+5,$y1-15,$dataArr['Y'][$i],$this->color);
$this->createImageString($this->fontSize,$x1,$y2+10,$dataArr['X'][$i],$this->color);
}
}

# this is used to plot titles of the graph
function drawTitles($font)
{
$this->setColor(255,0,0);
$y1 = $this->height - $this->graphMarginBottom;
$y = $this->graphMarginTop;
$y2 = $this->graphMarginLeft;

$this->createImageString($font, $this->xWidth/2,$y-25, $this->graphTitle,$this->color);
$this->createImageString($font, $this->xWidth/2+strlen($this->graphXTitle),$y1+25, $this->graphXTitle,$this->color);
$this->createImageStringVert($font,$y2-40,$this->yHeight/2+strlen($this->graphYTitle)*3,$this->graphYTitle ,$this->color);
}

# Drawing final graph
function drawGraph(){

header("Content-type: image/png");

switch($this->imageType){
case "PNG":
ImagePNG($this->imgRes);
break;
case "JPEG":
ImageJPEG($this->imgRes);
break;
case "GIF":
ImageGIF($this->imgRes);
break;
default:
ImagePNG($this->imgRes);
break;
}

}

}

?>