Apologies, thought there may be some common issues.
Here is an example of where the class is called.
include("./class/graph2.class.php");
include './dbcon.php';
$sql = "SELECT r.region_name, m.monthly_return_turnover";
$sql .= " FROM monthly_return m INNER JOIN region r ON ";
$sql .= "r.region_id = m.monthly_return_region_id WHERE m.monthly_return_month = 3 ORDER BY m.monthly_return_turnover DESC";
$rs = $db->Execute($sql);
if (!$rs)
{
print $db->ErrorMsg();
echo $sql;
}
$numRows = $rs->RecordCount();
for ($i=0; $i <= $numRows; $i++)
{
$datax[$i] = $rs->fields[1];
$datatxt[$i] = $rs->fields[0];
$rs->moveNext();
}
$g = new BarGraph();
$g->setHeightWidth(600,800);
$g->init();
$g->loadData($datax);
$g->setMax(round(max($datax))); //maximum data possible in the data set
$g->setBarWidth(10);
$g->setBarPadding(4);
$g->setBarHeight(0); //Set to 0 if you want height to be auto calculated.
$g->setBarColor(255,130,130);
$g->setBgColor(204,204,204);
$g->setAxisColor(0,0,0);
$g->setTxt(0,0,0);
$g->drawGraph();
$g->drawXaxis(500,400); //Xlen, YLen.
$g->drawYaxis(500,400);
$g->drawYtxt($datatxt);
$g->drawXtxt(10000); // x axis increment
$g->drawTitle("Summery of Group Turnover - March 2009");
$g->renderImage();
and here is the class
class BarGraph
{
var $barWidth;
var $barHeight;
var $imgHeight=400;
var $imgWidth=600;
var $bgColor,$barColor;
var $barPadding;
var $data,$rangeMax=10;
var $im;
var $xyOffset =50;
function init() /* initializes the image */
{
$this->im=imagecreatetruecolor($this->imgWidth,$this->imgHeight);
}
function setHeightWidth($h,$w)
{
$this->imgHeight=$h;
$this->imgWidth=$w;
}
function setBarWidth($width) /* sets the bar width */
{
$this->barWidth=$width;
}
function setBarPadding($padding) /* sets the bar padding */
{
$this->barPadding=$padding;
}
function setMax($max) /* sets the maximum posible value in the data set */
{
$this->rangeMax=$max;
}
function setBarHeight($height)
{
if($height == 0)
{
$this->barHeight = (($this->imgHeight-($this->xyOffset*2)-($this->barPadding * count($this->data))) / (count($this->data)-1));
}
else
{
$this->barHeight=$height;
}
}
function loadData($data) /* load data, the input shud be an array */
{
$this->data=$data;
}
function setBgColor($r,$g,$b) /* sets the background color of the image */
{
$this->bgColor=imagecolorallocate($this->im,$r,$g,$b);
}
function setBarColor($r,$g,$b) /* sets the bar color of the image */
{
$this->barColor=imagecolorallocate($this->im,$r,$g,$b);
}
function setAxisColor($r,$g,$b) /* sets the bar color of the image */
{
$this->AxisColor=imagecolorallocate($this->im,$r,$g,$b);
}
function setTxt($r,$g,$b)
{
$this->txtColor = imagecolorallocate($this->im,$r,$g,$b);
}
function drawXaxis($xLen,$yLen)
{
imageline ($this->im,$this->xyOffset,($this->imgHeight - $this->xyOffset),($this->imgWidth-$this->xyOffset),($this->imgHeight - $this->xyOffset),$this->AxisColor);
}
function drawYaxis($xLen,$yLen)
{
imageline ($this->im,$this->xyOffset,$this->xyOffset,($this->xyOffset),($this->imgHeight - $this->xyOffset),$this->AxisColor);
}
function drawGraph($flag=0) /* to draw graphs on the image */
{
if($flag) /* flag set to 1 to draw the second bar **/
{
$t=$this->barWidth+$this->barPadding;
}
else /* else draws the first bar set ?? is background?? */
{
imagefilledrectangle($this->im,0,0,$this->imgWidth,$this->imgHeight,$this->bgColor);
$t=0;
}
for ($mon = 0; $mon < (count($this->data)-1); $mon ++)
{
$yAxis = $this->imgHeight - ($this->xyOffset*2);
$xAxis = $this->imgWidth - ($this->xyOffset*2);
if ($mon == 0 )
{
$padding = 0;
}
else
{
$padding = $this->barPadding * $mon;
}
$X = $this->xyOffset;
$Y = ($this->barHeight * $mon) + $this->xyOffset + $padding + $t;
$X1 = ($this->data[$mon] * (($this->imgWidth - ($this->xyOffset * 2)) / $this->rangeMax) +$this->xyOffset);
$Y1 = ($Y + $this->barHeight);
imagefilledrectangle($this->im,$X,$Y,$X1,$Y1,$this->barColor);
}
}
function drawYtxt($data)
{
for ($i=0; $i < (count($data)-1); $i++)
{
$X = 10;
$Y = $this->xyOffset +($this->barHeight *$i) + ($this->barHeight / 2) + $this->barPadding * $i;
imagestring($this->im,1,$X,$Y,$data[$i],$this->txtColor);
imageline ($this->im,$this->xyOffset, $Y ,$this->xyOffset - 5,$Y, $this->AxisColor);
}
}
function drawXtxt($num)
{
$numTicks = $this->rangeMax / $num;
$increment = ($this->imgWidth - ($this->xyOffset*2)) / $numTicks;
for ($i=0; $i <= $numTicks; $i++)
{
$X = ($this->xyOffset + ($increment * $i));
$Y = ($this->imgHeight - $this->xyOffset);
$X1 = $X;
$Y1 = $Y + 5;
imageline ($this->im,$X, $Y ,$X1,$Y1, $this->AxisColor);
imagestring($this->im,1,$X-10,$Y+5,($num * $i),$this->txtColor);
}
}
function drawTitle($title)
{
imagestring($this->im,3,($this->imgWidth / 2),25,$title,$this->txtColor);
}
function renderImage() /* creates the image & sends in to the browser */
{
header("Content-Type: image/png");
imagepng($this->im);
}
}
Cheers
Nick